Skip to content

Instantly share code, notes, and snippets.

@4piu
Created January 4, 2020 08:08
Show Gist options
  • Save 4piu/6833b51b09013f2b90c2293040295c93 to your computer and use it in GitHub Desktop.
Save 4piu/6833b51b09013f2b90c2293040295c93 to your computer and use it in GitHub Desktop.
Typoman word spell helper. Find all possible combinations.
import argparse
import re
#https://github.com/first20hours/google-10000-english/blob/master/google-10000-english.txt
dictionary = "google-10000-english.txt"
parser = argparse.ArgumentParser(description='Find all words with specific letters')
parser.add_argument('letters', metavar='L', type=str, nargs='+',
help='array of letters like \'A\' \'B\' \'C2\', the number after letter limit the occurrence')
args = parser.parse_args()
def main():
array = {}
for arg in args.letters:
letter = re.search(r"([a-zA-Z])(\d*)", arg)
if not letter or letter.group(1) in array.keys():
print("Invalid argument: " + arg)
exit(1)
if letter.group(2):
array[letter.group(1)] = int(letter.group(2))
else:
array[letter.group(1)] = -1
with open(dictionary, "r") as f:
lines = f.read().splitlines()
for line in lines:
checklist = array.copy()
length = len(line)
if length < 2:
continue
for i, c in enumerate(line):
if c not in checklist.keys():
break
else:
checklist[c] -= 1
if checklist[c] == -1:
break
if i == length - 1:
print(line)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment