Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a tokenizer option to rouge #258

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions metrics/rouge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ At minimum, this metric takes as input a list of predictions and a list of refer
{'rouge1': 1.0, 'rouge2': 1.0, 'rougeL': 1.0, 'rougeLsum': 1.0}
```

One can also pass a custom tokenizer which is especially useful for non-latin languages.
```python
>>> results = rouge.compute(predictions=predictions,
... references=references,
tokenizer=lambda x: x.split())
>>> print(results)
{'rouge1': 1.0, 'rouge2': 1.0, 'rougeL': 1.0, 'rougeLsum': 1.0}
```

It can also deal with lists of references for each predictions:
```python
>>> rouge = evaluate.load('rouge')
Expand Down
19 changes: 17 additions & 2 deletions metrics/rouge/rouge.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@
"""


class Tokenizer:
"""Helper class to wrap a callable into a class with a `tokenize` method as used by rouge-score."""

def __init__(self, tokenizer_func):
self.tokenizer_func = tokenizer_func

def tokenize(self, text):
return self.tokenizer_func(text)


@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class Rouge(evaluate.Metric):
def _info(self):
Expand Down Expand Up @@ -108,13 +118,18 @@ def _info(self):
],
)

def _compute(self, predictions, references, rouge_types=None, use_aggregator=True, use_stemmer=False):
def _compute(
self, predictions, references, rouge_types=None, use_aggregator=True, use_stemmer=False, tokenizer=None
):
if rouge_types is None:
rouge_types = ["rouge1", "rouge2", "rougeL", "rougeLsum"]

multi_ref = isinstance(references[0], list)

scorer = rouge_scorer.RougeScorer(rouge_types=rouge_types, use_stemmer=use_stemmer)
if tokenizer is not None:
tokenizer = Tokenizer(tokenizer)

scorer = rouge_scorer.RougeScorer(rouge_types=rouge_types, use_stemmer=use_stemmer, tokenizer=tokenizer)
if use_aggregator:
aggregator = scoring.BootstrapAggregator()
else:
Expand Down