mirror of
https://github.com/status-im/spiff-arena.git
synced 2025-01-28 10:45:07 +00:00
7c219fd731
git-subtree-dir: SpiffWorkflow git-subtree-split: 63db3e45947ec66b8d0efc2c74064004f8ff482c
22 lines
565 B
Python
22 lines
565 B
Python
from difflib import ndiff
|
|
|
|
|
|
def distance(str1, str2):
|
|
counter = {"+": 0, "-": 0}
|
|
distance = 0
|
|
for edit_code, *_ in ndiff(str1, str2):
|
|
if edit_code == " ":
|
|
distance += max(counter.values())
|
|
counter = {"+": 0, "-": 0}
|
|
else:
|
|
counter[edit_code] += 1
|
|
distance += max(counter.values())
|
|
return distance
|
|
|
|
|
|
def most_similar(value, item_list, limit):
|
|
distances = [(key, distance(value, key)) for key in item_list]
|
|
distances.sort(key=lambda x: x[1])
|
|
return [x[0] for x in distances[:limit]]
|
|
|