/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
script/chrf.py
89 строк
3 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
from sacrebleu.metrics import CHRF import re from typing import List, Dict, Tuple import sys import json # Читает переводческую память из файла. # Каждая строка должна быть на отдельной строке. def read_tm(text_file: str) -> List[str]: try: with open(text_file, 'r', encoding='utf-8') as file: text = [line.strip() for line in file if line.strip()] return text except FileNotFoundError: print(f"Ошибка: Файл текста '{text_file}' не найден.") return [] except Exception as e: print(f"Ошибка при чтении текста: {e}") return [] # Читает текст из файла. # Каждая строка должна быть на отдельной строке. def read_text(text_file: str) -> List[str]: try: with open(text_file, 'r', encoding='utf-8') as file: text = [line.strip() for line in file if line.strip()] return text except FileNotFoundError: print(f"Ошибка: Файл текста '{text_file}' не найден.") return [] except Exception as e: print(f"Ошибка при чтении текста: {e}") return [] def chrf_search(tm: List[str], text: List[str], threshold: int = 70)-> List[str]: chrf = CHRF() results = [] for line in text: best_score = -1.0 best_id = 0 best_src = None #print(f"line: {line}") for row in tm: path = row.split(';') id = path[0] src = path[1] score = str(chrf.sentence_score(line.lower(), [src.lower()])) score = float(score.replace('chrF2 = ', '')) #print(f"score: {score}") if score > best_score: best_score = score best_src = src best_id = id results.append({ 'segment': line, 'source': best_src, 'score': best_score, 'id': best_id }) return results def main(): args = sys.argv; # Параметры файлов tm_file = args[1] text_file = args[2] # Чтение данных tm_list = read_tm(tm_file) text_list = read_text(text_file) #print(f"tm_list: {tm_list}") #print(f"text_list: {text_list}") # Поиск results = chrf_search(tm_list, text_list, threshold = 60) json_string = json.dumps(results, ensure_ascii = False) print(json_string) if __name__ == "__main__": main()