peacock-data-public-datasets-idc-cronscript
/
venv
/lib
/python3.10
/site-packages
/nltk
/test
/unit
/test_distance.py
from typing import Tuple | |
import pytest | |
from nltk.metrics.distance import edit_distance | |
class TestEditDistance: | |
def test_with_transpositions( | |
self, left: str, right: str, substitution_cost: int, expecteds: Tuple[int, int] | |
): | |
""" | |
Test `edit_distance` between two strings, given some `substitution_cost`, | |
and whether transpositions are allowed. | |
:param str left: First input string to `edit_distance`. | |
:param str right: Second input string to `edit_distance`. | |
:param int substitution_cost: The cost of a substitution action in `edit_distance`. | |
:param Tuple[int, int] expecteds: A tuple of expected outputs, such that `expecteds[0]` is | |
the expected output with `transpositions=True`, and `expecteds[1]` is | |
the expected output with `transpositions=False`. | |
""" | |
# Test the input strings in both orderings | |
for s1, s2 in ((left, right), (right, left)): | |
# zip with [True, False] to get the transpositions value | |
for expected, transpositions in zip(expecteds, [True, False]): | |
predicted = edit_distance( | |
s1, | |
s2, | |
substitution_cost=substitution_cost, | |
transpositions=transpositions, | |
) | |
assert predicted == expected | |