Index: graphspell-js/ibdawg.js ================================================================== --- graphspell-js/ibdawg.js +++ graphspell-js/ibdawg.js @@ -57,21 +57,19 @@ } } getSuggestions (nSuggLimit=10) { // return a list of suggestions - if (this.dSugg.get(0).length > 1) { - // we sort the better results with the original word - this.dSugg.get(0).sort((a, b) => { return str_transform.distanceDamerauLevenshtein(this.sWord, a) - str_transform.distanceDamerauLevenshtein(this.sWord, b); }); - } - else if (this.dSugg.get(1).length > 1) { - this.dSugg.get(1).sort((a, b) => { return str_transform.distanceDamerauLevenshtein(this.sWord, a) - str_transform.distanceDamerauLevenshtein(this.sWord, b); }); - } let lRes = []; + let bFirstListSorted = false; for (let [nDist, lSugg] of this.dSugg.entries()) { if (nDist > this.nDistLimit) { break; + } + if (!bFirstListSorted && lSugg.length > 1) { + lRes.sort((a, b) => { return str_transform.distanceDamerauLevenshtein(this.sWord, a) - str_transform.distanceDamerauLevenshtein(this.sWord, b); }); + bFirstListSorted = true; } lRes.push(...lSugg); if (lRes.length > nSuggLimit) { break; } Index: graphspell/ibdawg.py ================================================================== --- graphspell/ibdawg.py +++ graphspell/ibdawg.py @@ -71,20 +71,21 @@ self.nDistLimit = min(self.nDistLimit, self.nMinDist+1) def getSuggestions (self, nSuggLimit=10): "return a list of suggestions" # we sort the better results with the original word - if len(self.dSugg[0]) > 1: - self.dSugg[0].sort(key=lambda sSugg: st.distanceDamerauLevenshtein(self.sWord, sSugg)) - elif len(self.dSugg[1]) > 1: - self.dSugg[1].sort(key=lambda sSugg: st.distanceDamerauLevenshtein(self.sWord, sSugg)) - lRes = self.dSugg.pop(0) + lRes = [] + bFirstListSorted = False for nDist, lSugg in self.dSugg.items(): - if nDist <= self.nDistLimit: - lRes.extend(lSugg) - if len(lRes) > nSuggLimit: - break + if nDist > self.nDistLimit: + break + if not bFirstListSorted and len(lSugg) > 1: + lSugg.sort(key=lambda sSugg: st.distanceDamerauLevenshtein(self.sWord, sSugg)) + bFirstListSorted = True + lRes.extend(lSugg) + if len(lRes) > nSuggLimit: + break if self.sWord.isupper(): lRes = list(OrderedDict.fromkeys(map(lambda sSugg: sSugg.upper(), lRes))) # use dict, when Python 3.6+ elif self.sWord[0:1].isupper(): # dont’ use <.istitle> lRes = list(OrderedDict.fromkeys(map(lambda sSugg: sSugg[0:1].upper()+sSugg[1:], lRes))) # use dict, when Python 3.6+