Index: gc_core/py/ibdawg.py ================================================================== --- gc_core/py/ibdawg.py +++ gc_core/py/ibdawg.py @@ -39,36 +39,37 @@ self.aSugg = set() self.dSugg = { 0: [], 1: [] } def addSugg (self, sSugg, nDeep=0): "add a suggestion" - #print(sSugg) + #logging.info((nDeep * " ") + "__" + sSugg + "__") if sSugg not in self.aSugg: nDist = st.distanceDamerauLevenshtein(self.sCleanWord, cp.cleanWord(sSugg)) if nDist <= self.nDistLimit: if nDist not in self.dSugg: self.dSugg[nDist] = [] self.dSugg[nDist].append(sSugg) self.aSugg.add(sSugg) - #logging.info((nDeep * " ") + "__" + sSugg + "__") if nDist < self.nMinDist: self.nMinDist = nDist self.nDistLimit = min(self.nDistLimit, self.nMinDist+2) def getSuggestions (self, nSuggLimit=10, nDistLimit=-1): "return a list of suggestions" lRes = [] - #if self.dSugg[0]: - # # we sort the better results with the original word - # self.dSugg[0].sort(key=lambda sSugg: cp.distanceDamerauLevenshtein(self.sWord, sSugg)) + if self.dSugg[0]: + # we sort the better results with the original word + self.dSugg[0].sort(key=lambda sSugg: st.distanceDamerauLevenshtein(self.sWord, sSugg)) for lSugg in self.dSugg.values(): lRes.extend(lSugg) if len(lRes) > nSuggLimit: break lRes = list(cp.filterSugg(lRes)) if self.sWord.istitle(): lRes = list(map(lambda sSugg: sSugg.title(), lRes)) + elif self.sWord.isupper(): + lRes = list(map(lambda sSugg: sSugg.upper(), lRes)) return lRes[:nSuggLimit] def reset (self): self.aSugg.clear() self.dSugg.clear() @@ -267,18 +268,16 @@ def _suggest (self, oSuggResult, sRemain, nMaxDel=0, nMaxHardRepl=0, nDeep=0, iAddr=0, sNewWord="", sAction="", bAvoidLoop=False): # recursive function #logging.info((nDeep * " ") + sNewWord + ":" + sRemain + " ยท " + sAction) if not sRemain: if int.from_bytes(self.byDic[iAddr:iAddr+self.nBytesArc], byteorder='big') & self._finalNodeMask: - #logging.info((nDeep * " ") + "__" + sNewWord + "__") oSuggResult.addSugg(sNewWord) for sTail in self._getTails(iAddr): - #logging.info((nDeep * " ") + "__" + sNewWord+sTail + "__") oSuggResult.addSugg(sNewWord+sTail) return cCurrent = sRemain[0:1] - for cChar, jAddr in self._getSimilarArcs(cCurrent, iAddr): + for cChar, jAddr in self._getSimilarCharArcs(cCurrent, iAddr): self._suggest(oSuggResult, sRemain[1:], nMaxDel, nMaxHardRepl, nDeep+1, jAddr, sNewWord+cChar, "*") if not bAvoidLoop: # avoid infinite loop if cCurrent == sRemain[1:2]: # same char, we remove 1 char without adding 1 to self._suggest(oSuggResult, sRemain[1:], nMaxDel, nMaxHardRepl, nDeep+1, iAddr, sNewWord, cCurrent+"/2") @@ -321,10 +320,11 @@ # we add what we removed return list(map(lambda sSug: sPfx + sSug + sSfx, aSugg)) return aSugg def _suggest2 (self, oSuggResult, nDeep=0, iAddr=0, sNewWord=""): + # recursive function #logging.info((nDeep * " ") + sNewWord) if nDeep >= oSuggResult.nDistLimit: sCleanNewWord = cp.cleanWord(sNewWord) if st.distanceSift4(oSuggResult.sCleanWord[:len(sCleanNewWord)], sCleanNewWord) > oSuggResult.nDistLimit: return @@ -339,11 +339,11 @@ "generator: yield all chars and addresses from node at address " for nVal, jAddr in self._getArcs(iAddr): if nVal < self.nChar: yield (self.dCharVal[nVal], jAddr) - def _getSimilarArcs (self, cChar, iAddr): + def _getSimilarCharArcs (self, cChar, iAddr): "generator: yield similar char of 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: @@ -360,21 +360,20 @@ aTails.update(self._getTails(jAddr, sTail+self.dCharVal[nVal], n-1)) return aTails def drawPath (self, sWord, iAddr=0): "show the path taken by in the graph" - cChar = sWord[0:1] if sWord else " " + c1 = sWord[0:1] if sWord else " " iPos = -1 n = 0 - print(cChar + ": ", end="") - for nVal, jAddr in self._getArcs(iAddr): - if nVal in self.dCharVal: - print(self.dCharVal[nVal], end="") - if self.dCharVal[nVal] == sWord[0:1]: - iNextNodeAddr = jAddr - iPos = n - n += 1 + print(c1 + ": ", end="") + for c2, jAddr in self._getCharArcs(iAddr): + print(c2, end="") + if c2 == sWord[0:1]: + iNextNodeAddr = jAddr + iPos = n + n += 1 if not sWord: return if iPos >= 0: print("\n "+ " " * iPos + "|") self.drawPath(sWord[1:], iNextNodeAddr)