-
Notifications
You must be signed in to change notification settings - Fork 0
/
swipe_poc.py
executable file
·51 lines (38 loc) · 1.04 KB
/
swipe_poc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /usr/bin/env python3
from functools import lru_cache
import sys
if len(sys.argv) != 2:
print("Usage:",sys.argv[0],"<ideal file>")
exit(1)
IDEAL_FILE = sys.argv[1]
with open(IDEAL_FILE, "r") as f:
WORDS = [a.split() for a in f.read().splitlines()]
@lru_cache(maxsize=None)
def levenshtein(s, t):
MISMATCH_COST = 3
ADD_TO_S_COST = 1
ADD_TO_T_COST = 1
if s == "":
return len(t) * ADD_TO_T_COST
if t == "":
return len(s) * ADD_TO_S_COST
mismatch = 0
if s[-1] != t[-1]:
mismatch = MISMATCH_COST
res = min([levenshtein(s[:-1], t)+ADD_TO_S_COST,
levenshtein(s, t[:-1])+ADD_TO_T_COST,
levenshtein(s[:-1], t[:-1])+ mismatch])
return res
def predict_word(a):
if len(a) == 0: return ""
best = 100000
best_w = ""
for w, i in WORDS:
if w[0] != a[0] or w[-1] != a[-1]: continue
l = levenshtein(a, i)
if l < best:
best = l
best_w = w
return best_w
while True:
print(predict_word(input()))