Skip to content

Commit

Permalink
updating soundex dic structure
Browse files Browse the repository at this point in the history
  • Loading branch information
leolca committed Jul 1, 2020
1 parent 6f6cd96 commit 1edf7a5
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions spell/soundexspell.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ def load_soundex_dictionary(self, filename):
with open(filename) as f:
self.SOUNDEX_WORDS = json.load(f)
a_key = next(iter(self.SOUNDEX_WORDS))
the_sndx_length = len(self.SOUNDEX_WORDS[a_key])
if the_sndx_length != self.soundexlen:
if len(a_key) != self.soundexlen:
import warnings
warnings.warn("Incongruent Soundex length parameter was given. Updating it to {} (length found in the given dictionary).".format(the_sndx_length))
self.soundexlen = the_sndx_length
self.soundexlen = len(a_key)
else:
for w in self.WORDS:
self.SOUNDEX_WORDS[w] = self.soundex(w, self.soundexlen)
sndx = self.soundex(w, self.soundexlen)
if sndx in self.SOUNDEX_WORDS:
self.SOUNDEX_WORDS[sndx].append(w)
else:
self.SOUNDEX_WORDS[sndx] = [w]
if filename is not None and filename.endswith('.json') and not exists(filename):
with open(filename, 'w') as f:
json.dump(self.SOUNDEX_WORDS, f, indent=2)
with open(filename, 'w') as f:
json.dump(self.SOUNDEX_WORDS, f, indent=2)

def soundex(self, s, mlen=None):
# https://west-penwith.org.uk/misc/soundex.htm
Expand Down Expand Up @@ -69,8 +72,11 @@ def soundex(self, s, mlen=None):
def candidates(self, word):
"""Generate possible spelling corrections for word."""
sndx = self.soundex(word)
clist = [key for (key, value) in self.SOUNDEX_WORDS.items() if value == sndx]
return self.known(clist)
#clist = [key for (key, value) in self.SOUNDEX_WORDS.items() if value == sndx]
if sndx in self.SOUNDEX_WORDS:
return self.known(self.SOUNDEX_WORDS[sndx])
else:
return set()

def set_weightObjFun(self, weight):
if weight is None:
Expand Down

0 comments on commit 1edf7a5

Please sign in to comment.