|
|
|
|
|
import nltk |
|
from rouge_score import rouge_scorer, scoring |
|
|
|
|
|
def compute_rouge(predictions, references, rouge_types=None, use_aggregator=True, use_stemmer=False): |
|
if rouge_types is None: |
|
rouge_types = ["rouge1", "rouge2", "rougeL", "rougeLsum"] |
|
|
|
scorer = rouge_scorer.RougeScorer(rouge_types=rouge_types, use_stemmer=use_stemmer) |
|
if use_aggregator: |
|
aggregator = scoring.BootstrapAggregator() |
|
else: |
|
scores = [] |
|
|
|
for ref, pred in zip(references, predictions): |
|
score = scorer.score(ref, pred) |
|
if use_aggregator: |
|
aggregator.add_scores(score) |
|
else: |
|
scores.append(score) |
|
|
|
if use_aggregator: |
|
result = aggregator.aggregate() |
|
else: |
|
result = {} |
|
for key in scores[0]: |
|
result[key] = list(score[key] for score in scores) |
|
|
|
return result |
|
|
|
|
|
|
|
|
|
def rouge_postprocess_text(text): |
|
|
|
return "\n".join(nltk.sent_tokenize(text)) |
|
|