How to match words as if in a dictionary, based on len-1 or len+1? Python
Posted
by
pearbear
on Stack Overflow
See other posts from Stack Overflow
or by pearbear
Published on 2012-11-14T04:57:23Z
Indexed on
2012/11/14
4:59 UTC
Read the original article
Hit count: 148
If I have a word 'raqd', how would I use python to have a spellcheck, so to speak, to find the word 'rad' as an option in 'spellcheck'? What I've been trying to do is this:
def isbettermatch(keysplit, searchword):
i = 0
trues = 0
falses = 0
lensearchwords = len(searchword)
keysplits = copy.deepcopy(keysplit)
searchwords = copy.deepcopy(searchword)
#print keysplit, searchwords
if len(keysplits) == len(searchwords)-1:
i = 0
while i < len(keysplits):
j = 0
while j < lensearchwords:
if keysplits[i] == searchwords[j]:
trues +=1
searchwords.pop(j)
lensearchwords = len(searchwords)
elif keysplits[i] != searchwords[j]:
falses +=1
j +=1
i +=1
if trues >= len(searchwords)-1:
#print "-------------------------------------------------------", keysplits
return True
keysplit
is a list like ['s', 'p', 'o', 'i', 'l']
for example, and the searchword
would be a list ['r', 'a', 'q', 'd']
.
If the function returns True
, then it would print the keyword that matches. Ex. 'rad', for the searchword
'raqd'.
I need to find all possible matches for the searchword
with a single letter addition or deletion.
so ex. 'raqd' would have an option to be 'rad', and 'poted' could be 'posted' or 'potted'.
Above is what I have tried, but it is not working well at all. Help much appreciated!
© Stack Overflow or respective owner