init datamate

This commit is contained in:
Dallas98
2025-10-21 23:00:48 +08:00
commit 1c97afed7d
692 changed files with 135442 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from datamate.core.base_op import OPERATORS
OPERATORS.register_module(module_name='AnonymizedUrlCleaner',
module_path="ops.mapper.url_cleaner.process")

View File

@@ -0,0 +1,16 @@
name: 'URL网址匿名化'
name_en: 'URL Anonymization'
description: '将文档中的url网址匿名化。'
description_en: 'Anonymizes URLs in documents.'
language: 'python'
vendor: 'huawei'
raw_id: 'AnonymizedUrlCleaner'
version: '1.0.0'
types:
- 'cleanse'
modal: 'text'
effect:
before: '需要被屏蔽的url:https://www.huawei.com'
after: '需要被屏蔽的url:<url>'
inputs: 'text'
outputs: 'text'

View File

@@ -0,0 +1,36 @@
#!/user/bin/python
# -*- coding: utf-8 -*-
"""
Description: URL网址匿名化
Create: 2024/12/26 15:43
"""
import re
import time
from typing import Dict, Any
from loguru import logger
from datamate.core.base_op import Mapper
class AnonymizedUrlCleaner(Mapper):
"""将文档中的网址匿名化"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.url_pattern = r'((?:(?:https?|ftp|file)://|(?<![a-zA-Z\-\.])www\.)' \
r'[\-A-Za-z0-9\+&@\(\)#/%\?=\^~_|!:\,\.\;]+[\-A-Za-z0-9\+&@#/%=\~_\|])' \
r'(?![\-A-Za-z0-9\+&@#/%=\~_\|])'
self.url_re_compile = re.compile(self.url_pattern, re.MULTILINE)
def execute(self, sample: Dict[str, Any]) -> Dict[str, Any]:
start = time.time()
sample[self.text_key] = self._url_filter(sample[self.text_key])
logger.info(f"fileName: {sample[self.filename_key]}, method: UrlCleaner costs {time.time() - start:6f} s")
return sample
def _url_filter(self, input_data: str):
input_data = ''.join(['', input_data, ''])
text = self.url_re_compile.sub("<url>", input_data)
return text[1:-1]