| 1 | from KVCOMM.utils.log import logger |
| 2 | |
| 3 | class Accuracy: |
| 4 | def __init__(self): |
| 5 | self._num_correct = 0 |
| 6 | self._num_total = 0 |
| 7 | |
| 8 | def update(self, predicted: str, target: str) -> None: |
| 9 | is_correct = target in predicted |
| 10 | self._num_correct += int(is_correct) |
| 11 | self._num_total += 1 |
| 12 | |
| 13 | def get(self) -> float: |
| 14 | return self._num_correct / self._num_total |
| 15 | |
| 16 | def print(self): |
| 17 | accuracy = self.get() |
| 18 | logger.opt(colors=True).info( |
| 19 | f"<blue>[ACCURACY]</blue> {accuracy:%} ({self._num_correct}/{self._num_total})" |
| 20 | ) |