def measure(self, candidate_submapping): mapped = set(candidate_submapping.mapping.values()) min_dist = len(self.coded_word) for word in self.word_list: dist, missed = levenshtein.lev_dist(candidate_submapping.translate(self.coded_word), word) if mapped.intersection(missed): dist = 100 if dist < min_dist: min_dist = dist return min_dist
def getAnswer(inputString): countArray = [] countArray2= [] inputVector = inputString.split(' ') inputLength = inputVector.__len__() for i in range(int(len(knowledgeBase)/2)): keyListSize = knowledgeBase[2*i].__len__() count = 0 tempVector = [] tempVector2 = [] for j in range(keyListSize): keyVector = knowledgeBase[2*i][j].split(' ') tempCount = 0 for k in inputVector: for l in keyVector: d = lev_dist(k, l) if d<2: tempCount+=1 break tempVector.append(tempCount) tempVector2.append(tempCount/knowledgeBase[2*i][j].__len__()) maxMatch = max(tempVector) maxMatch2 = max(tempVector2) countArray.append(maxMatch) countArray2.append(maxMatch2) #print countArray #print countArray2 maxMatchInKB = max(countArray) if maxMatchInKB/inputLength <=0.3: return None if countArray.count(maxMatchInKB) >1: testList = [] testList2 = [] for m in range(int(len(knowledgeBase)/2)): if countArray[m] is maxMatchInKB: testList.append(countArray2[m]) testList2.append(m) #print testList #print testList2 nearMaxMatch = max(testList) idxTemp = testList.index(nearMaxMatch) idxActual = testList2[idxTemp] #print idxActual return knowledgeBase[2*idxActual+1] keyPos = countArray.index(maxMatchInKB) return knowledgeBase[2*keyPos+1]