Add a function to guess the closest word for a miss input
This commit is contained in:
parent
ec9481e65b
commit
9686f36522
2 changed files with 15 additions and 1 deletions
|
|
@ -57,3 +57,13 @@ def word_distance(str1, str2):
|
|||
)
|
||||
|
||||
return d[len(str1)][len(str2)]
|
||||
|
||||
|
||||
def guess(pattern, expect):
|
||||
if len(expect):
|
||||
se = sorted([(e, word_distance(pattern, e)) for e in expect], key=lambda x: x[1])
|
||||
_, m = se[0]
|
||||
for e, wd in se:
|
||||
if wd > m or wd > 1 + len(pattern) / 4:
|
||||
break
|
||||
yield e
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue