本文整理汇总了Python中rouge.Rouge方法的典型用法代码示例。如果您正苦于以下问题:Python rouge.Rouge方法的具体用法?Python rouge.Rouge怎么用?Python rouge.Rouge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rouge
的用法示例。
在下文中一共展示了rouge.Rouge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calc_metrics
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def calc_metrics(refs, hyps, language, metric="all", meteor_jar=None):
metrics = dict()
metrics["count"] = len(hyps)
metrics["ref_example"] = refs[-1]
metrics["hyp_example"] = hyps[-1]
many_refs = [[r] if r is not list else r for r in refs]
if metric in ("bleu", "all"):
metrics["bleu"] = corpus_bleu(many_refs, hyps)
if metric in ("rouge", "all"):
rouge = Rouge()
scores = rouge.get_scores(hyps, refs, avg=True)
metrics.update(scores)
if metric in ("meteor", "all") and meteor_jar is not None and os.path.exists(meteor_jar):
meteor = Meteor(meteor_jar, language=language)
metrics["meteor"] = meteor.compute_score(hyps, many_refs)
if metric in ("duplicate_ngrams", "all"):
metrics["duplicate_ngrams"] = dict()
metrics["duplicate_ngrams"].update(calc_duplicate_n_grams_rate(hyps))
return metrics
示例2: _rouge
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def _rouge(guess, answers):
global rouge
"""Compute ROUGE score between guess and *any* answers. Return the best."""
if rouge is None:
return None, None, None
evaluator = rouge.Rouge(metrics=['rouge-n', 'rouge-l'], max_n=2)
try:
scores = [
evaluator.get_scores(normalize_answer(guess), normalize_answer(a))
for a in answers
]
except LookupError:
warn_once(
'ROUGE requires nltk punkt tokenizer. Please run '
'`python -c "import nltk; nltk.download(\'punkt\')`'
)
rouge = None
return None, None, None
scores_rouge1 = [score['rouge-1']['r'] for score in scores]
scores_rouge2 = [score['rouge-2']['r'] for score in scores]
scores_rougeL = [score['rouge-l']['r'] for score in scores]
return max(scores_rouge1), max(scores_rouge2), max(scores_rougeL)
示例3: calc_reward
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def calc_reward(refs, hypo, unk_id, metric):
"""
calculate the reward given hypo and refs and will return
bleu score if metric is 'bleu' or return
sum of (Rouge-1, Rouge-2, Rouge-L) if metric is 'rouge'
"""
if len(hypo) == 0 or len(refs[0]) == 0:
return 0.
for i in range(len(hypo)):
assert isinstance(hypo[i], int)
if hypo[i] == unk_id:
hypo[i] = -1
if metric == 'bleu':
return 0.01 * sentence_bleu(
references=refs, hypothesis=hypo, smooth=True)
else:
ref_str = ' '.join([str(word) for word in refs[0]])
hypo_str = ' '.join([str(word) for word in hypo])
rouge_scores = \
rouge.get_scores(hyps=[hypo_str], refs=[ref_str], avg=True)
return sum([value['f'] for key, value in rouge_scores.items()])
示例4: _get_sent_rouge
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def _get_sent_rouge(
hypothesis: List[str], references: List[List[str]], rouge_type: str,
extra_args: Optional[Dict[str, str]] = None
) -> List[float]:
assert rouge_type in {'rouge-1', 'rouge-2', 'rouge-l'}
_rouge_type = 'rouge-l' if rouge_type == 'rouge-l' else 'rouge-n'
_max_n = 1 if rouge_type == 'rouge-1' else 2
joint_references = [list(r) for r in zip(*references)]
scores = _rouge.Rouge(
metrics=[_rouge_type], max_n=_max_n, apply_avg=False
).get_scores(hypothesis, joint_references)
return [s[STATS_TYPE][0] for s in scores[rouge_type]]
示例5: eval_rouge
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def eval_rouge(instances: List[CFRInstance]):
references = []
hypotheses = []
evaluator = rouge.Rouge(metrics=['rouge-n', 'rouge-l', 'rouge-w'],
max_n=4,
limit_length=True,
length_limit=100,
length_limit_type='words',
apply_avg=True,
apply_best=False,
alpha=0.5, # Default F1_score
weight_factor=1.2,
stemming=True)
by_instance = []
for instance in instances:
_r = [_clean_text(g) for g in instance.gold_cf_endings]
_h = _clean_text(instance.predicted_ending)
references.append(_r)
hypotheses.append(_h)
try:
by_instance.append(evaluator.get_scores(_h, _r))
except:
by_instance.append({})
scores = evaluator.get_scores(hypotheses, references)
return {'rouge_all' : scores,
'rouge_by_instance': by_instance
}
示例6: compute_rouge
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def compute_rouge(predictions, targets):
predictions = [" ".join(prediction).lower() for prediction in predictions]
predictions = [prediction if prediction else "EMPTY" for prediction in predictions]
targets = [" ".join(target).lower() for target in targets]
targets = [target if target else "EMPTY" for target in targets]
rouge = Rouge()
scores = rouge.get_scores(hyps=predictions, refs=targets, avg=True)
return scores['rouge-2']['f']
示例7: rouge
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def rouge(sys, ref):
rouge = Rouge()
return rouge.get_scores(sys, ref, avg=True)
示例8: get_metric
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def get_metric(self, reset=True):
logger.info("[INFO] Hyps and Refer number is %d, %d", len(self.hyps), len(self.refers))
if len(self.hyps) == 0 or len(self.refers) == 0 :
logger.error("During testing, no hyps or refers is selected!")
return
rouge = Rouge()
scores_all = rouge.get_scores(self.hyps, self.refers, avg=True)
if reset:
self.hyps = []
self.refers = []
logger.info(scores_all)
return scores_all
示例9: rouge_eval
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def rouge_eval(hyps, refer):
rouge = Rouge()
# print(hyps)
# print(refer)
# print(rouge.get_scores(hyps, refer))
try:
score = rouge.get_scores(hyps, refer)[0]
mean_score = np.mean([score["rouge-1"]["f"], score["rouge-2"]["f"], score["rouge-l"]["f"]])
except:
mean_score = 0.0
return mean_score
示例10: rouge_all
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def rouge_all(hyps, refer):
rouge = Rouge()
score = rouge.get_scores(hyps, refer)[0]
# mean_score = np.mean([score["rouge-1"]["f"], score["rouge-2"]["f"], score["rouge-l"]["f"]])
return score
示例11: __init__
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def __init__(self, config):
"""Initialization from the configuration"""
self.mode = config.controller_mode
self.model_name = config.model_name
self.model_name_version = config.model_name + "_" + config.model_version
self.start_epoch = config.start_epoch
self.num_epoch = config.num_epoch
self.write_output = config.write_output
self.batch_size = config.batch_size
self.print_interval = config.train_print_interval
self.gpu_id = config.gpu_id
self.drop_out = config.drop_out
self.dec_start_id = config.dec_start_id
self.dec_end_id = config.dec_end_id
self.model_path = config.model_path
self.output_path = config.output_path
self.random_seed = config.random_seed
self.bow_pred_method = config.bow_pred_method
self.train_log = TrainingLog(config)
self.id2word = None
self.target_metrics = config.target_metrics
self.lm_load_path = config.lm_load_path
self.rouge_evaluator = rouge.Rouge(metrics=['rouge-n', 'rouge-l'], max_n=2)
self.save_ckpt = config.save_ckpt
self.eval_metrics_list = config.eval_metrics_list
self.log_metrics = config.log_metrics
self.gumbel_samples = config.gumbel_samples
self.is_gumbel = config.is_gumbel
return
示例12: __init__
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def __init__(
self, rouge_type: str,
name: str = "ROUGE") -> None:
check_argument_types()
super().__init__(name)
if rouge_type.lower() not in ["1", "2", "l"]:
raise ValueError(
("Invalid type of rouge metric '{}', "
"must be '1', '2' or 'L'").format(rouge_type))
self.rouge_type = rouge_type.lower()
self.rouge = rouge.Rouge()
示例13: setUp
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
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()
示例14: cal_ROUGE
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def cal_ROUGE(refer, candidate):
if len(candidate) == 0:
candidate = ['<unk>']
elif len(candidate) == 1:
candidate.append('<unk>')
if len(refer) == 0:
refer = ['<unk>']
elif len(refer) == 1:
refer.append('<unk>')
rouge = Rouge()
scores = rouge.get_scores(' '.join(candidate), ' '.join(refer))
return scores[0]['rouge-2']['f']
示例15: build_oracle_records
# 需要导入模块: import rouge [as 别名]
# 或者: from rouge import Rouge [as 别名]
def build_oracle_records(records, nrows=None, lower=True):
references = []
predictions = []
rouge = Rouge()
new_records = []
for i, record in enumerate(records):
if nrows is not None and i >= nrows:
break
summary = record["summary"]
summary = summary if not lower else summary.lower()
references.append(summary)
text = record["text"]
calc_score = lambda x, y: calc_single_score(x, y, rouge)
predicted_summary, sentences, oracle_indices = build_oracle_summary_greedy(text, summary, calc_score=calc_score)
predictions.append(predicted_summary)
oracle_indices = [1 if i in oracle_indices else 0 for i in range(len(sentences))]
new_record = copy.copy(record)
new_record["sentences"] = sentences
new_record["oracle"] = oracle_indices
new_records.append(new_record)
print_metrics(references, predictions)
return new_records