Skip to content

Instantly share code, notes, and snippets.

@capjamesg
Last active March 22, 2023 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capjamesg/334a6385f1a20c484400843b264e6c06 to your computer and use it in GitHub Desktop.
Save capjamesg/334a6385f1a20c484400843b264e6c06 to your computer and use it in GitHub Desktop.
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