You've already forked DataMate
init datamate
This commit is contained in:
6
runtime/ops/mapper/legend_cleaner/__init__.py
Normal file
6
runtime/ops/mapper/legend_cleaner/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from datamate.core.base_op import OPERATORS
|
||||
|
||||
OPERATORS.register_module(module_name='LegendCleaner',
|
||||
module_path="ops.mapper.legend_cleaner.process")
|
||||
16
runtime/ops/mapper/legend_cleaner/metadata.yml
Normal file
16
runtime/ops/mapper/legend_cleaner/metadata.yml
Normal file
@@ -0,0 +1,16 @@
|
||||
name: '图注表注去除'
|
||||
name_en: 'Figure and Table Description Removal'
|
||||
description: '去除文档中的图注、表注等内容。'
|
||||
description_en: 'Removes figure and table description from documents.'
|
||||
language: 'python'
|
||||
vendor: 'huawei'
|
||||
raw_id: 'LegendCleaner'
|
||||
version: '1.0.0'
|
||||
types:
|
||||
- 'cleanse'
|
||||
modal: 'text'
|
||||
effect:
|
||||
before: '图1.1.1 图注名称'
|
||||
after: ''
|
||||
inputs: 'text'
|
||||
outputs: 'text'
|
||||
41
runtime/ops/mapper/legend_cleaner/process.py
Normal file
41
runtime/ops/mapper/legend_cleaner/process.py
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/user/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Description: 图注表注去除
|
||||
Create: 2024/12/5 15:43
|
||||
"""
|
||||
import re
|
||||
import time
|
||||
from typing import Dict, Any
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from datamate.core.base_op import Mapper
|
||||
|
||||
|
||||
class LegendCleaner(Mapper):
|
||||
@staticmethod
|
||||
def _get_legend_re_compile():
|
||||
chinese_legend_prefix = r"(图|表|图片|表格)"
|
||||
chinese_legend_number = r"(\d+((\.|-)\d+)*|[a-zA-Z]{1,2}((\.|-)\d+)*)"
|
||||
chinese_legend_pattern = r"(?<=\n)" + chinese_legend_prefix + "( )*" + chinese_legend_number + " +.*\n"
|
||||
english_legend_pattern = r"(Figure|Table|Fig\.?)"
|
||||
english_legend_number = r"(S?\d+((\.|-)\d+)*|[a-zA-Z]{1,2}\d?((\.|-)\d+)*)"
|
||||
english_legend_pattern = (r"(?<=\n)" + english_legend_pattern + "( )*"
|
||||
+ english_legend_number + r"(\.|:)? +.*\n")
|
||||
legend_re_compile = re.compile('|'.join([chinese_legend_pattern, english_legend_pattern]), re.IGNORECASE)
|
||||
return legend_re_compile
|
||||
|
||||
@classmethod
|
||||
def _clean_html_tag(cls, input_data: str):
|
||||
"""移除文档中图注表注等"""
|
||||
input_data = ''.join(['\n', input_data, '\n'])
|
||||
text = cls._get_legend_re_compile().sub("", input_data)
|
||||
return text[1:-1]
|
||||
|
||||
def execute(self, sample: Dict[str, Any]) -> Dict[str, Any]:
|
||||
start = time.time()
|
||||
sample[self.text_key] = self._clean_html_tag(sample[self.text_key])
|
||||
logger.info(f"fileName: {sample[self.filename_key]}, method: LegendCleaner costs {time.time() - start:6f} s")
|
||||
return sample
|
||||
Reference in New Issue
Block a user