def rouge_filescores(hyp_path,ref_path): ''' :param hyp_path: 实验结果文本 :param ref_path: 标准 :return: 实验结果文本相对于标准文本的分数 ''' files_rouge = rouge.FilesRouge(hyp_path, ref_path) scores = files_rouge.get_scores() return scores
def setUp(self): self.hyp_path = './tests/hyp.txt' self.ref_path = './tests/ref.txt' self.data_path = './tests/data.json' with open(self.data_path) as f: self.data = json.load(f) self.rouge = rouge.Rouge() self.files_rouge = rouge.FilesRouge()
def rouge_filescores(hyp_path, ref_path): """ :param hyp_path: 实验结果文本 :param ref_path: 标准文本 :return: 实验结果文本相对于标准文本的分数 use the rouge matrix calculate """ files_rouge = rouge.FilesRouge(hyp_path, ref_path) scores = files_rouge.get_scores() return scores
def setUp(self): self.hyp_path = 'predicted_sentences_task1_ref0_2.txt' self.ref_path = 'task1_ref0_2.txt' self.data_path = 'data2.json' with open(self.data_path) as f: self.data = json.load(f) self.rouge = rouge.Rouge() self.files_rouge = rouge.FilesRouge()
def main(): sys.setrecursionlimit(2000 * 500 + 1000) for model in [ "ast_cleaned", "transformer_no_ast", "raw_python", "unprocessed_ast" ]: fr = rouge.FilesRouge("hyps/" + model + "_hyp.txt", "refs/" + model + "_ref.test") print(model) print(json.dumps(fr.get_scores(avg=True), indent=2))
def run_red(reference_fn: str, candidate_fn: str) -> ROUGEResults: r = rouge.FilesRouge(candidate_fn, reference_fn) scores = r.get_scores(avg=True) return { 'rouge1': { 'precision': scores['rouge-1']['p'], 'recall': scores['rouge-1']['r'], 'f1': scores['rouge-1']['f'], }, 'rouge2': { 'precision': scores['rouge-2']['p'], 'recall': scores['rouge-2']['r'], 'f1': scores['rouge-2']['f'], }, 'rougeL': { 'precision': scores['rouge-l']['p'], 'recall': scores['rouge-l']['r'], 'f1': scores['rouge-l']['f'], }, }
os.makedirs(save_path) pred_test_file_name = os.path.join(save_path, f'test_{pred_file_name_suffix}.txt') ref_test_file_name = os.path.join(save_path, f'test_{ref_file_name_suffix}.txt') if model_name == 'textrank': summarizer = GensimSummarizer(model_params, file_to_process, pred_test_file_name, ref_test_file_name, preprocessor) summarizer.summarize() if model_name == 'first_sentence': summarizer = ExtractFirstFullSentence(model_params, file_to_process, pred_test_file_name, ref_test_file_name, preprocessor) summarizer.summarize() else: raise NotImplementedError(f'Model {model_name} not yet implemented') r = rouge.FilesRouge(pred_test_file_name, ref_test_file_name) scores = r.get_scores(avg=True) print(scores) with open(os.path.join(save_path, 'scores.json'), 'w') as f: json.dump(scores, f, indent=2) with open(os.path.join(save_path, 'config.json'), 'w') as f: json.dump(config, f, indent=2)
# references = ['the quick brown fox jumped over the lazy dog', 'test'] # hypotheses = ['the fast brown fox jumped over the lazy dog', 'test'] references = [ 'Man on ocean beach flying several kites on windy day', 'Two people standing next to a river holding surfboards', 'test' ] hypotheses = [ 'a person on a beach flying a kite', 'a man holding a surfboard on a beach', 'test' ] rouge_eval = rouge.Rouge() test_r_eval = rouge.FilesRouge("data/example/hyp.txt", "data/example/ref.txt") b = test_r_eval.get_scores() print(b) scores_rouge_dict_list = rouge_eval.get_scores(hypotheses, references) print(scores_rouge_dict_list) rouge_1_scores = [0.0] * 3 rouge_2_scores = [0.0] * 3 rouge_l_scores = [0.0] * 3 for i, s in enumerate(scores_rouge_dict_list): if isinstance(s, dict): for metric, vals in s.items(): print(metric) print(vals) if metric == 'rouge-1':
def setUp(self): self.hyp_path = './hyp.txt' self.ref_path = './ref.txt' self.rouge = rouge.Rouge() self.files_rouge = rouge.FilesRouge(self.hyp_path, self.ref_path)