Skip to content

Instantly share code, notes, and snippets.

@agateau
Created November 5, 2016 18:40
Show Gist options
  • Save agateau/a519dfc8f3c6d8362b5049e4093cf96e to your computer and use it in GitHub Desktop.
Save agateau/a519dfc8f3c6d8362b5049e4093cf96e to your computer and use it in GitHub Desktop.
Given a set of letters and a length, finds all words known to hunspell dictionary
#!/usr/bin/env python3
import argparse
import os
import sys
from itertools import permutations
import hunspell
DESCRIPTION = """
Given a set of letters and a length, finds all words known to hunspell
dictionary.
"""
DICT_DIR = '/usr/share/hunspell'
def main():
parser = argparse.ArgumentParser()
parser.description = DESCRIPTION
parser.add_argument('-d', '--dict', default='fr',
help='Dictionary to use, defaults to "fr"')
parser.add_argument('letters', help='Letters allowed in the word')
parser.add_argument('length', type=int,
help='Number of letters in the word to find')
args = parser.parse_args()
hobj = hunspell.HunSpell(os.path.join(DICT_DIR, args.dict + '.dic'),
os.path.join(DICT_DIR, args.dict + '.aff'))
words = set()
for lst in permutations(sorted(args.letters), args.length):
word = ''.join(lst)
if word in words:
continue
if hobj.spell(word):
words.add(word)
print(word)
return 0
if __name__ == '__main__':
sys.exit(main())
# vi: ts=4 sw=4 et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment