| 137 | #3.extract ref |
| 138 | #4. get score |
| 139 | def process_raw_text(raw_text): |
| 140 | if not raw_text: |
| 141 | return "" |
| 142 | |
| 143 | # 第一步:统一处理原始文本格式(列表转字符串) |
| 144 | if isinstance(raw_text, List): |
| 145 | pro_text = ' '.join(raw_text) |
| 146 | elif isinstance(raw_text, str): |
| 147 | pro_text = raw_text |
| 148 | else: |
| 149 | return "" # 非字符串/列表类型直接返回空 |
| 150 | |
| 151 | # 第二步:还原 JSON 转义字符(关键!处理 \\", \\', \\n 等) |
| 152 | pro_text = pro_text.replace('\\\\', '\\') # 先还原双反斜杠为单反斜杠 |
| 153 | pro_text = pro_text.replace('\\n', '\n') # 还原换行符 |
| 154 | pro_text = pro_text.replace('\\"', '"') # 还原转义双引号 |
| 155 | pro_text = pro_text.replace("\\'", "'") # 还原转义单引号 |
| 156 | |
| 157 | # 第三步:重新统计引号数量(含转义后还原的引号) |
| 158 | quote_chars = ["\"", "“", "‘", "'", "〝"] |
| 159 | quote_count = sum(pro_text.count(char) for char in quote_chars) |
| 160 | |
| 161 | if quote_count >= 2: |
| 162 | # 引号匹配模式:优化对混合引号和转义的处理 |
| 163 | result = [] |
| 164 | in_quote = False |
| 165 | current_quote = None # 记录当前打开的引号类型 |
| 166 | close_quote_mapping = {"\"": "\"", "“": "”", "‘": "’", "'": "'", "〝": "〞"} |
| 167 | |
| 168 | for char in pro_text: |
| 169 | if char in close_quote_mapping.keys(): |
| 170 | # 遇到左引号:如果不在引号内,标记为当前引号 |
| 171 | if not in_quote: |
| 172 | in_quote = True |
| 173 | current_quote = char |
| 174 | # 遇到右引号:如果与当前引号匹配,关闭引号 |
| 175 | elif char == close_quote_mapping.get(current_quote): |
| 176 | in_quote = False |
| 177 | current_quote = None |
| 178 | # 不匹配的引号:视为普通字符加入结果 |
| 179 | else: |
| 180 | result.append(char) |
| 181 | elif char in [",", "。", "!", "?", ",", " "] and not in_quote: |
| 182 | # 非引号内的标点和空格统一转为空格 |
| 183 | result.append(' ') |
| 184 | else: |
| 185 | # 其他字符直接加入(包括引号内的标点) |
| 186 | result.append(char) |
| 187 | |
| 188 | # 合并结果并去除连续空格 |
| 189 | pro_text = ' '.join(''.join(result).split()) |
| 190 | else: |
| 191 | # 无引号或引号不足时,直接处理标点 |
| 192 | pro_text = re.sub(r'[,。!?,]+', ' ', pro_text) # 标点转空格 |
| 193 | pro_text = ' '.join(pro_text.split()) # 去除连续空格 |
| 194 | |
| 195 | return pro_text |
| 196 | |