You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1000 B

  1. #!/usr/bin/python
  2. import re
  3. import itertools
  4. from spellchecker import SpellChecker
  5. SPELL = SpellChecker()
  6. def canidates(letters, min=2, max=5):
  7. pos = []
  8. for length in range(min, max+1):
  9. for comb in itertools.combinations(letters, length):
  10. for perm in itertools.permutations(comb):
  11. word = ''.join(perm)
  12. pos.append(word)
  13. return SPELL.known(pos)
  14. def filter_pos(pos, regex):
  15. pat = re.compile(regex + '$')
  16. return list(filter(pat.match, pos))
  17. while True:
  18. letters = input('Enter letters: ')
  19. if letters == ":quit:":
  20. break
  21. pos = canidates(letters)
  22. inp = None
  23. while inp not in (":quit:", ":new:"):
  24. if inp is not None:
  25. for word in filter_pos(pos, inp):
  26. print('>>>', word)
  27. print('Letters: ', ','.join(letters))
  28. prompt = """Enter pattern, ':new:' for new letters,
  29. or ':quit:' to exit: """
  30. inp = input(prompt)
  31. if inp == ":quit:":
  32. break