-
-
Save capjamesg/334a6385f1a20c484400843b264e6c06 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 | |
words = ["IndieWeb", "person mention"] | |
def generate_index(): | |
""" | |
Generate a dictionary of all possible variants of a word. | |
:param words: A list of words for which to generate variants. | |
:return: A dictionary of all possible variants of a word. | |
:rtype: dict | |
""" | |
all_words = {} | |
for w in words: | |
for i in range(len(w) + 1): | |
for c in string.ascii_lowercase: | |
new_word = w[:i] + c + w[i + 1 :] | |
if new_word not in all_words: | |
all_words[new_word.lower()] = w.lower() | |
# remove a letter | |
new_word = w[:i] + w[i + 1 :] | |
if new_word not in all_words: | |
all_words[new_word.lower()] = w.lower() | |
# swap letter | |
if i < len(w) - 1: | |
new_word = w[:i] + w[i + 1] + w[i] + w[i + 2 :] | |
if new_word not in all_words: | |
all_words[new_word.lower()] = w.lower() | |
# add a character | |
for c in string.ascii_lowercase: | |
new_word = w[:i] + c + w[i:] | |
if new_word not in all_words: | |
all_words[new_word.lower()] = w.lower() | |
return all_words | |
dictionary = generate_index() | |
def test_cases(): | |
words = [ | |
"IndieWeb", | |
"person mention", | |
"personmention", | |
"person mentioni", | |
"pperson mention", | |
"iniieweb" | |
] | |
for word in words: | |
word = word.lower() | |
assert word == dictionary[word] or word in dictionary | |
print(f"{word} -> {dictionary[word]}") | |
test_cases() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment