Calculate rouge using rouge_scorer package. Args: pred_lns: list of summaries generated by model tgt_lns: list of groundtruth summaries (e.g. contents of val.target) use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes to impr
(
pred_lns: List[str],
tgt_lns: List[str],
use_stemmer=True,
rouge_keys=ROUGE_KEYS,
return_precision_and_recall=False,
bootstrap_aggregation=True,
newline_sep=True,
)
| 494 | |
| 495 | |
| 496 | def calculate_rouge( |
| 497 | pred_lns: List[str], |
| 498 | tgt_lns: List[str], |
| 499 | use_stemmer=True, |
| 500 | rouge_keys=ROUGE_KEYS, |
| 501 | return_precision_and_recall=False, |
| 502 | bootstrap_aggregation=True, |
| 503 | newline_sep=True, |
| 504 | ) -> Dict: |
| 505 | """Calculate rouge using rouge_scorer package. |
| 506 | Args: |
| 507 | pred_lns: list of summaries generated by model |
| 508 | tgt_lns: list of groundtruth summaries (e.g. contents of val.target) |
| 509 | use_stemmer: Bool indicating whether Porter stemmer should be used to |
| 510 | strip word suffixes to improve matching. |
| 511 | rouge_keys: which metrics to compute, defaults to rouge1, rouge2, rougeL, rougeLsum |
| 512 | return_precision_and_recall: (False) whether to also return precision and recall. |
| 513 | bootstrap_aggregation: whether to do the typical bootstrap resampling of scores. Defaults to True, if False |
| 514 | this function returns a collections.defaultdict[metric: list of values for each observation for each subscore]`` |
| 515 | newline_sep:(default=True) whether to add newline between sentences. This is essential for calculation rougeL |
| 516 | on multi sentence summaries (CNN/DM dataset). |
| 517 | Returns: |
| 518 | Dict[score: value] if aggregate else defaultdict(list) keyed by rouge_keys |
| 519 | """ |
| 520 | scorer = rouge_scorer.RougeScorer(rouge_keys, use_stemmer=use_stemmer) |
| 521 | aggregator = scoring.BootstrapAggregator() |
| 522 | for pred, tgt in zip(tgt_lns, pred_lns): |
| 523 | # rougeLsum expects "\n" separated sentences within a summary |
| 524 | if newline_sep: |
| 525 | pred = add_newline_to_end_of_each_sentence(pred) |
| 526 | tgt = add_newline_to_end_of_each_sentence(tgt) |
| 527 | scores = scorer.score(pred, tgt) |
| 528 | aggregator.add_scores(scores) |
| 529 | |
| 530 | if bootstrap_aggregation: |
| 531 | result = aggregator.aggregate() |
| 532 | if return_precision_and_recall: |
| 533 | return extract_rouge_mid_statistics(result) # here we return dict |
| 534 | else: |
| 535 | return {k: round(v.mid.fmeasure * 100, 4) for k, v in result.items()} |
| 536 | |
| 537 | else: |
| 538 | return aggregator._scores # here we return defaultdict(list) |
| 539 | |
| 540 | |
| 541 | # Utilities for freezing parameters and checking whether they are frozen |
no test coverage detected