187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
def suggest (self, sWord):
"returns a set of similar words"
# first, we check for similar words
#return self._suggestWithCrushedUselessChars(cp.clearWord(sWord))
aSugg = self._suggest(sWord)
if not aSugg:
aSugg.update(self._suggest(sWord[1:]))
aSugg.update(self._suggest(sWord[:-1]))
aSugg.update(self._suggest(sWord[1:-1]))
if not aSugg:
aSugg.update(self._suggestWithCrushedUselessChars(cp.clearWord(sWord)))
return aSugg
def _suggest (self, sWord, nDeep=0, iAddr=0, sNewWord="", bAvoidLoop=False):
# recursive function
aSugg = set()
if not sWord:
if int.from_bytes(self.byDic[iAddr:iAddr+self.nBytesArc], byteorder='big') & self._finalNodeMask:
#show(nDeep, "___" + sNewWord + "___")
aSugg.add(sNewWord)
for sTail in self._getTails(iAddr):
|
<
<
>
|
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
def suggest (self, sWord):
"returns a set of similar words"
# first, we check for similar words
#return self._suggestWithCrushedUselessChars(cp.clearWord(sWord))
aSugg = self._suggest(sWord)
if not aSugg:
aSugg.update(self._suggest(sWord[1:]))
if not aSugg:
aSugg.update(self._suggestWithCrushedUselessChars(cp.clearWord(sWord)))
return aSugg
def _suggest (self, sWord, nDeep=0, iAddr=0, sNewWord="", bAvoidLoop=False):
"returns a set of suggestions for <sWord>"
# recursive function
aSugg = set()
if not sWord:
if int.from_bytes(self.byDic[iAddr:iAddr+self.nBytesArc], byteorder='big') & self._finalNodeMask:
#show(nDeep, "___" + sNewWord + "___")
aSugg.add(sNewWord)
for sTail in self._getTails(iAddr):
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
if len(sWord) == 2:
for sRepl in cp.dFinal2.get(sWord, ()):
#show(nDeep, sRepl)
aSugg.update(self._suggest(sRepl, nDeep+1, iAddr, sNewWord, True))
elif len(sWord) == 1:
#show(nDeep, ":end of word:")
# end of word
for sRepl in cp.dFinal1.get(sWord, ()):
#show(nDeep, sRepl)
aSugg.update(self._suggest(sRepl, nDeep+1, iAddr, sNewWord, True))
return aSugg
def _getSimilarArcs (self, cChar, iAddr):
"generator: yield similar char of <cChar> and address of the following node"
for c in cp.d1to1.get(cChar, [cChar]):
if c in self.dChar:
jAddr = self._lookupArcNode(self.dChar[c], iAddr)
if jAddr:
yield (c, jAddr)
def _getTails (self, iAddr, sTail="", n=2):
"return a list of suffixes ending at a distance of <n> from <iAddr>"
aTails = set()
for nVal, jAddr in self._getArcs(iAddr):
if nVal < self.nChar:
if int.from_bytes(self.byDic[jAddr:jAddr+self.nBytesArc], byteorder='big') & self._finalNodeMask:
aTails.add(sTail + self.dCharVal[nVal])
if n:
aTails.update(self._getTails(jAddr, sTail+self.dCharVal[nVal], n-1))
return aTails
def _suggestWithCrushedUselessChars (self, sWord, nDeep=0, iAddr=0, sNewWord="", bAvoidLoop=False):
aSugg = set()
if not sWord:
if int.from_bytes(self.byDic[iAddr:iAddr+self.nBytesArc], byteorder='big') & self._finalNodeMask:
|
>
|
|
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
if len(sWord) == 2:
for sRepl in cp.dFinal2.get(sWord, ()):
#show(nDeep, sRepl)
aSugg.update(self._suggest(sRepl, nDeep+1, iAddr, sNewWord, True))
elif len(sWord) == 1:
#show(nDeep, ":end of word:")
# end of word
aSugg.update(self._suggest("", nDeep+1, iAddr, sNewWord, True))
for sRepl in cp.dFinal1.get(sWord, ()):
#show(nDeep, sRepl)
aSugg.update(self._suggest(sRepl, nDeep+1, iAddr, sNewWord, True))
return aSugg
def _getSimilarArcs (self, cChar, iAddr):
"generator: yield similar char of <cChar> and address of the following node"
for c in cp.d1to1.get(cChar, [cChar]):
if c in self.dChar:
jAddr = self._lookupArcNode(self.dChar[c], iAddr)
if jAddr:
yield (c, jAddr)
def _getTails (self, iAddr, sTail="", n=2):
"return a list of suffixes ending at a distance of <n> from <iAddr>"
aTails = set()
for nVal, jAddr in self._getArcs(iAddr):
if nVal < self.nChar:
if int.from_bytes(self.byDic[jAddr:jAddr+self.nBytesArc], byteorder='big') & self._finalNodeMask:
aTails.add(sTail + self.dCharVal[nVal])
if n and not aTails:
aTails.update(self._getTails(jAddr, sTail+self.dCharVal[nVal], n-1))
return aTails
def _suggestWithCrushedUselessChars (self, sWord, nDeep=0, iAddr=0, sNewWord="", bAvoidLoop=False):
aSugg = set()
if not sWord:
if int.from_bytes(self.byDic[iAddr:iAddr+self.nBytesArc], byteorder='big') & self._finalNodeMask:
|
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
return aSugg
def _getSimilarArcsAndCrushedChars (self, cChar, iAddr):
"generator: yield similar char of <cChar> and address of the following node"
for nVal, jAddr in self._getArcs(iAddr):
if self.dCharVal.get(nVal, None) in cp.aUselessChar:
yield (self.dCharVal[nVal], jAddr)
for c in cp.d1to1.get(cChar, [cChar]):
if c in self.dChar:
jAddr = self._lookupArcNode(self.dChar[c], iAddr)
if jAddr:
yield (c, jAddr)
def drawPath (self, sWord, iAddr=0):
cChar = sWord[0:1] if sWord else " "
iPos = -1
n = 0
print(cChar + ": ", end="")
for nVal, jAddr in self._getArcs(iAddr):
|
|
<
<
<
<
|
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
return aSugg
def _getSimilarArcsAndCrushedChars (self, cChar, iAddr):
"generator: yield similar char of <cChar> and address of the following node"
for nVal, jAddr in self._getArcs(iAddr):
if self.dCharVal.get(nVal, None) in cp.aUselessChar:
yield (self.dCharVal[nVal], jAddr)
yield from self._getSimilarArcs(cChar, iAddr)
def drawPath (self, sWord, iAddr=0):
cChar = sWord[0:1] if sWord else " "
iPos = -1
n = 0
print(cChar + ": ", end="")
for nVal, jAddr in self._getArcs(iAddr):
|