Files
gpt2_parfind_en_zh_equal/preprocessing.py
ModelHub XC 1ae14a5e92 初始化项目,由ModelHub XC社区提供模型
Model: NeTSlab/gpt2_parfind_en_zh_equal
Source: Original Platform
2026-07-18 01:20:11 +08:00

26 lines
971 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# preprocessing.py
import re
class Preprocessor:
def __init__(self, lowercase=False, separate_apostrophes=True, separate_digits=True, separate_punctuation=True):
self.lowercase = lowercase
self.separate_apostrophes = separate_apostrophes
self.separate_punctuation = separate_punctuation
self.separate_digits = separate_digits
def preprocess(self, line: str) -> str:
if self.lowercase:
line = line.lower()
if self.separate_apostrophes:
# Add spaces around apostrophes
line = re.sub(r"(['`])", r" \1 ", line)
# Add spaces around punctuation (except alphanumeric and apostrophes)
if self.separate_punctuation:
line = re.sub(r"([^A-Za-z0-9\s'`])", r" \1 ", line)
if self.separate_digits:
line = re.sub(r"(\d)", r" \1 ", line)
# Normalize whitespace
line = re.sub(r"\s+", " ", line)
return line.strip()