def score_sentence(self, ref, out): if self.case_insensitive: ref = corpus_utils.lower(ref) out = corpus_utils.lower(out) if self._stemmer: ref = [self._stemmer.stem(x) if len(x) > 3 else x for x in ref] out = [self._stemmer.stem(x) if len(x) > 3 else x for x in out] if self.rouge_type == 'rougeL': scores = rouge_scorer._score_lcs(ref, out) elif re.match(r"rouge[0-9]$", self.rouge_type): n = int(self.rouge_type[5:]) if n <= 0: raise ValueError( f"rougen requires positive n: {self.rouge_type}") ref_ngrams = rouge_scorer._create_ngrams(ref, n) out_ngrams = rouge_scorer._create_ngrams(out, n) scores = rouge_scorer._score_ngrams(ref_ngrams, out_ngrams) else: raise ValueError(f"Invalid rouge type: {self.rouge_type}") if self.score_type == 'fmeasure': return scores.fmeasure, None elif self.score_type == 'precision': return scores.precision, None elif self.score_type == 'recall': return scores.recall, None else: raise ValueError(f"Invalid score type: {self.score_type}")
def score_sentence(self, ref, out, src=None): if self.case_insensitive: ref = corpus_utils.lower(ref) out = corpus_utils.lower(out) if self._stemmer: ref = [self._stemmer.stem(x) if len(x) > 3 else x for x in ref] out = [self._stemmer.stem(x) if len(x) > 3 else x for x in out] if self.rouge_type == 'rougeL': ref, out = self.tokenize(" ".join(ref)), self.tokenize( " ".join(out)) scores = rouge_scorer._score_lcs(ref, out) elif self.rouge_type == 'rougeLsum': refs = [self.tokenize(s) for s in self.get_sents(ref)] outs = [self.tokenize(s) for s in self.get_sents(out)] scores = rouge_scorer._summary_level_lcs(refs, outs) elif re.match(r"rouge[0-9]$", self.rouge_type): ref, out = self.tokenize(" ".join(ref)), self.tokenize( " ".join(out)) n = int(self.rouge_type[5:]) if n <= 0: raise ValueError( f"rougen requires positive n: {self.rouge_type}") ref_ngrams = rouge_scorer._create_ngrams(ref, n) out_ngrams = rouge_scorer._create_ngrams(out, n) scores = rouge_scorer._score_ngrams(ref_ngrams, out_ngrams) else: raise ValueError(f"Invalid rouge type: {self.rouge_type}") if self.score_type == 'fmeasure': score_value = scores.fmeasure elif self.score_type == 'precision': score_value = scores.precision elif self.score_type == 'recall': score_value = scores.recall else: raise ValueError(f"Invalid score type: {self.score_type}") return self.scale * score_value, None