import re
import jieba
import jieba.analyse
import spacy
from concurrent.futures import ThreadPoolExecutor

class TextSeparationEngine:
    def __init__(self):
        # 初始化英文NLP模型
        self.nlp_en = spacy.load("en_core_web_sm")
        # 初始化中文分词器
        jieba.initialize()
        
        # 配置中英文停用词（需要根据实际情况补充）
        self.stopwords = {
            'zh': set(['的', '了', '在', '是', '我']),
            'en': set(['the', 'a', 'an', 'is', 'are'])
        }
    def _split_blocks(self, text):
        """结构分离模块：将文本分割为中英文交替的文本块"""
        pattern = re.compile(
            r'([\u4e00-\u9fff\u3000-\u303f\uff00-\uffef]+)|([a-zA-Z0-9\s\,\.\:\;\!\?\'\"]+)',
            re.UNICODE
        )
        blocks = []
        for match in pattern.finditer(text):
            chn, eng = match.groups()
            if chn:
                blocks.append(('zh', chn.strip()))
            elif eng:
                blocks.append(('en', eng.strip()))
        return blocks
    
    def _process_chinese(self, text):
        """中文处理通道：提取中文术语"""
        # 去除标点
        text = re.sub(r'[^\u4e00-\u9fff\w]+', ' ', text)
        # 使用TextRank提取关键词
        keywords = jieba.analyse.textrank(
            text, 
            topK=5,
            withWeight=False,
            allowPOS=('n', 'vn', 'v')
        )
        # 过滤停用词
        return [kw for kw in keywords if kw not in self.stopwords['zh']]
    def _process_english(self, text):
        """英文处理通道：提取英文术语"""
        # 使用spaCy提取名词短语
        doc = self.nlp_en(text)
        terms = []
        for chunk in doc.noun_chunks:
            term = chunk.text.lower()
            if term not in self.stopwords['en']:
                terms.append(term)
        return list(set(terms))
    
    def _extract_pairs(self, text):
        """术语对齐模块：提取显式术语对"""
        pairs = []
        # 匹配中文（英文）格式的术语对
        pattern = re.compile(
            r'([\u4e00-\u9fff]+)\s*[\(（]([a-zA-Z0-9\s&\.]+)[\)）]',
            re.UNICODE
        )
        for match in pattern.finditer(text):
            zh_term = match.group(1)
            en_term = match.group(2)
            pairs.append((zh_term, en_term))
        return pairs
    
    def process(self, text):
        """双通道并行处理主函数"""
        # 结构分离
        blocks = self._split_blocks(text)
        
        # 并行处理
        with ThreadPoolExecutor() as executor:
            futures = []
            term_pairs = self._extract_pairs(text)
            
            # 创建处理任务
            tasks = {'zh': [], 'en': []}
            for lang, content in blocks:
                tasks[lang].append(content)
            
            # 提交并行任务
            zh_terms = list(executor.map(self._process_chinese, tasks['zh']))
            en_terms = list(executor.map(self._process_english, tasks['en']))
            
            # 合并结果
            zh_terms = [term for sublist in zh_terms for term in sublist]
            en_terms = [term for sublist in en_terms for term in sublist]

        return {
            "structure": blocks,
            "zh_terms": list(set(zh_terms)),
            "en_terms": list(set(en_terms)),
            "term_pairs": term_pairs
        }
    
# 使用示例
if __name__ == "__main__":
    engine = TextSeparationEngine()
    sample_text = """
        人工智能（AI）是当今最具前景的技术方向之一。深度学习（Deep Learning）作为其核心分支，
        在计算机视觉（CV）和自然语言处理（NLP）领域取得了显著进展。我们需要使用PyTorch等框架
        来构建神经网络（Neural Network）。
    """
    
    result = engine.process(sample_text)
    
    print("结构分离结果：")
    for idx, (lang, text) in enumerate(result["structure"]):
        print(f"块{idx+1}: [{lang}] {text}")
    
    print("\n提取的中文术语：", result["zh_terms"])
    print("提取的英文术语：", result["en_terms"])
    print("\n关键术语对：", result["term_pairs"])


