Skip to content

Commit

Permalink
added edit score (#6)
Browse files Browse the repository at this point in the history
* add edit score

* change metrics name
  • Loading branch information
shahules786 authored May 12, 2023
1 parent 6a8fcee commit c93cbe3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions belar/metrics/simple.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from Levenshtein import distance, ratio
import typing as t
from dataclasses import dataclass

Expand Down Expand Up @@ -38,6 +39,28 @@ def score(self, ground_truth: str, generated_text: str):
return score.fmeasure


class EditScore(Metric):
measure: t.Literal["distance", "ratio"] = "ratio"

@property
def name(self) -> str:
return f"edit_{self.measure}"

@property
def is_batchable(self):
return True

def score(self, ground_truth: t.List[str], generated_text: t.List[str]):
if self.measure == "distance":
score = [distance(s1, s2) for s1, s2 in zip(ground_truth, generated_text)]
elif self.measure == "ratio":
score = [ratio(s1, s2) for s1, s2 in zip(ground_truth, generated_text)]
else:
raise ValueError(f"Unkown measure {self.measure}")

return score


Rouge1 = ROUGE("rouge1")
Rouge2 = ROUGE("rouge2")
RougeL = ROUGE("rougeL")
Expand Down

0 comments on commit c93cbe3

Please sign in to comment.