def correct(words): item = words.split() word = item[len(item) -1] item.pop(len(item) -1) candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word] tag = " ".join(x for x in item) #..P(phrase|word_typed) = P(word_typed|phrase) P(phrase) / P(word_typed) suggestion = suggestion_lst() store = filter(lambda x: tag in x, suggestion) resp = [tag +" " + max(candidates, key=NWORDS.get)] + store[:4] return {"term":resp}
def correct(words): from prefixtree import PrefixDict words = words.strip().lower() item = words.split() suggestion = suggestion_lst() if len(item) != 1: word = item[len(item) -1] item.pop(len(item) -1) candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word] tag = " ".join(x.strip() for x in item) store = sorted([x for x in suggestion.keys() if tag.strip() in x and get_cosine(text_to_vector(tag.strip()), text_to_vector(x.strip())) > 40]) #store = sorted([x for x in set(suggestion.get(tag[:1])) if tag in x and get_cosine(text_to_vector(tag.strip()), text_to_vector(x.strip())) > 50]) resp = list(set([(tag +" " + max(candidates, key=NWORDS.get)).strip()] + store[:5])) return {"term":resp[::-1]} else: word = item[0] candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word] tag = " ".join(x.strip() for x in item) store = sorted([x for x in suggestion.keys() if tag.strip() in x and get_cosine(text_to_vector(tag.strip()), text_to_vector(x.strip())) > 40]) #store = sorted([x for x in set(suggestion.get(tag[:1])) if tag in x and get_cosine(text_to_vector(tag.strip()), text_to_vector(x.strip())) > 50]) resp = list(set([(max(candidates, key=NWORDS.get)).strip()] + store[:5])) return {"term":resp[::-1]}