You've already forked DataMate
init datamate
This commit is contained in:
6
runtime/ops/mapper/content_cleaner/__init__.py
Normal file
6
runtime/ops/mapper/content_cleaner/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from datamate.core.base_op import OPERATORS
|
||||
|
||||
OPERATORS.register_module(module_name='ContentCleaner',
|
||||
module_path="ops.mapper.content_cleaner.process")
|
||||
16
runtime/ops/mapper/content_cleaner/metadata.yml
Normal file
16
runtime/ops/mapper/content_cleaner/metadata.yml
Normal file
@@ -0,0 +1,16 @@
|
||||
name: '文档目录去除'
|
||||
name_en: 'Document Contents Removal'
|
||||
description: '去除文档中的目录。'
|
||||
description_en: 'Removes tables of contents from documents.'
|
||||
language: 'python'
|
||||
vendor: 'huawei'
|
||||
raw_id: 'ContentCleaner'
|
||||
version: '1.0.0'
|
||||
types:
|
||||
- 'cleanse'
|
||||
modal: 'text'
|
||||
effect:
|
||||
before: ''
|
||||
after: ''
|
||||
inputs: 'text'
|
||||
outputs: 'text'
|
||||
64
runtime/ops/mapper/content_cleaner/process.py
Normal file
64
runtime/ops/mapper/content_cleaner/process.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/user/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Description: 文档目录去除
|
||||
Create: 2025/01/13
|
||||
"""
|
||||
import re
|
||||
import time
|
||||
from typing import Dict, Any
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from datamate.core.base_op import Mapper
|
||||
|
||||
|
||||
class ContentCleaner(Mapper):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.no_content_count = 3 # 连续不符合目录结构的行数阈值
|
||||
# 目录标题
|
||||
self.content_text_pattern = r"^ *(目 *录|CONTENT(S)?) *$"
|
||||
# 目录行 前缀格式
|
||||
self.content_preface_pattern = r"^ *(前言|About This Document|\d+(\.\d+)*|[a-zA-Z]+(\.\d+)*)"
|
||||
# 目录行 中间格式
|
||||
self.content_middle_pattern = r"\.{7,}"
|
||||
# 目录行 结尾格式
|
||||
self.content_end_pattern = r"(\d|错误!未定义书签。|[IXV]+) *$"
|
||||
self.content_pattern = self.content_preface_pattern + ".*" + self.content_end_pattern
|
||||
|
||||
def execute(self, sample: Dict[str, Any]) -> Dict[str, Any]:
|
||||
start = time.time()
|
||||
sample[self.text_key] = self._content_filter(sample[self.text_key])
|
||||
logger.info(f"fileName: {sample[self.filename_key]}, method: ContentCleaner costs {time.time() - start:6f} s")
|
||||
return sample
|
||||
|
||||
def _content_filter(self, input_data: str):
|
||||
count = 0 # 记录不符合目录结构的次数,连续3行不满足要求,则认为已经进入正文
|
||||
# 目录起始和结束索引
|
||||
content_start_index, content_end_index = -1, -1
|
||||
lines = input_data.split("\n")
|
||||
for i, line in enumerate(lines):
|
||||
if content_start_index >= 0 and count >= self.no_content_count:
|
||||
break
|
||||
# 首先匹配目录或content字眼
|
||||
if content_start_index < 0 and re.match(self.content_text_pattern, line, re.IGNORECASE):
|
||||
content_start_index = i
|
||||
content_end_index = i
|
||||
# 匹配两种形式的目录行
|
||||
# 1. 以指定格式开始、指定格式结尾;2.该行包含点数量超过7个
|
||||
elif content_start_index >= 0 and (re.match(self.content_pattern, line, re.IGNORECASE)
|
||||
or re.search(self.content_middle_pattern, line)):
|
||||
content_end_index = i
|
||||
count = 0
|
||||
elif content_start_index >= 0 and not (re.match(self.content_pattern, line, re.IGNORECASE)
|
||||
or re.search(self.content_middle_pattern, line)):
|
||||
count += 1
|
||||
|
||||
if 0 <= content_start_index < content_end_index:
|
||||
res = "\n".join(lines[:content_start_index] + lines[content_end_index + 1:])
|
||||
else:
|
||||
# 只有目录关键字时,关键字不去除;或不符合目录结构,返回原文
|
||||
res = "\n".join(lines)
|
||||
return res
|
||||
Reference in New Issue
Block a user