Created
July 10, 2020 11:07
-
-
Save popigg/75a11d12bb21f1a7b3452c26e6efd99c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
from nltk.metrics import * | |
import enchant | |
KEYWORD = 'agua' | |
NAMES = [] | |
LEV_DISTANCE = 2 | |
def permutations(start_word): | |
new_words = [] | |
for i, letter in enumerate(start_word): | |
word_as_list = list(start_word) | |
for char in string.ascii_lowercase: | |
word_as_list[i] = char | |
new_word = "".join(word_as_list); | |
for j in range(len(new_word)): | |
for char in string.ascii_lowercase: | |
new_combination = new_word[:j] + char + new_word[j:] | |
new_words.append(new_combination) | |
for h in range(i+1, len(new_combination)): | |
new_word_as_list = list(new_combination) | |
for char in string.ascii_lowercase: | |
new_word_as_list[h] = char | |
return new_words | |
perms_word = permutations(KEYWORD) | |
# dictionary | |
d = enchant.Dict("es_ES") | |
result = [] | |
for i in perms_word: | |
# check if is in the dictionary and the Levenshtein distnace | |
if (d.check(i) and edit_distance(KEYWORD, i) <= LEV_DISTANCE): | |
result.append(i) | |
print('perms words ==>', result + NAMES) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment