Datasets:
Download metrics/edit_sim.py from bzantium/LongBench: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/datasets/bzantium/LongBench/resolve/main/metrics/edit_sim.py
- Command line
-
hf download hf://datasets/bzantium/LongBench/metrics/edit_sim.py
-
curl -L -o edit_sim.py https://huggingface.co/datasets/bzantium/LongBench/resolve/main/metrics/edit_sim.py
1.26 kB
| from functools import lru_cache | |
| def lev_dist(prediction, ground_truth): | |
| # for memorization | |
| def min_dist(s1, s2): | |
| if s1 == len(prediction) or s2 == len(ground_truth): | |
| return len(prediction) - s1 + len(ground_truth) - s2 | |
| # no change required | |
| if prediction[s1] == ground_truth[s2]: | |
| return min_dist(s1 + 1, s2 + 1) | |
| return 1 + min( | |
| min_dist(s1, s2 + 1), # insert character | |
| min_dist(s1 + 1, s2), # delete character | |
| min_dist(s1 + 1, s2 + 1), # replace character | |
| ) | |
| return min_dist(0, 0) | |
| def edit_sim_score(a, b): | |
| return 1 - lev_dist(a, b) / max(len(a), len(b)) | |
| def metric_max_over_ground_truths(metric_fn, prediction, ground_truths): | |
| scores_for_ground_truths = [] | |
| for ground_truth in ground_truths: | |
| score = metric_fn(prediction, ground_truth) | |
| scores_for_ground_truths.append(score) | |
| return max(scores_for_ground_truths) | |
| def compute_edit_sim(predictions, references): | |
| edit_sim = 0 | |
| for prediction, ground_truths in zip(predictions, references): | |
| edit_sim += metric_max_over_ground_truths(edit_sim_score, prediction, ground_truths) | |
| return 100.0 * edit_sim / len(predictions) |