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.

50 lines
1.5 KiB

  1. import spellchecker
  2. import pathlib
  3. import concurrent
  4. from concurrent.futures import ThreadPoolExecutor
  5. EXECUTOR = ThreadPoolExecutor()
  6. DEFAULT = spellchecker.WordFrequency()
  7. ALTERNATE = spellchecker.WordFrequency()
  8. package_root = pathlib.Path(spellchecker.__file__) / '..'
  9. english = package_root / 'resources' / 'en.json.gz'
  10. extra_words = pathlib.Path(__file__, '..', 'extra_words.txt')
  11. def do_operation(obj, bound_method, *args, **kwargs):
  12. bound_method(*args, **kwargs)
  13. return obj
  14. default_load_future = EXECUTOR.submit(
  15. do_operation, DEFAULT, DEFAULT.load_dictionary, english)
  16. alternate_load_future = EXECUTOR.submit(
  17. do_operation, ALTERNATE, ALTERNATE.load_text_file, english)
  18. class Dictionary:
  19. def __init__(self, loader_future: concurrent.futures._base.Future):
  20. self.future = loader_future
  21. self._word_frequency = None
  22. @property
  23. def word_frequency(self) -> spellchecker.WordFrequency:
  24. if self._word_frequency is None:
  25. self._word_frequency = self.future.result()
  26. return self._word_frequency
  27. def filter(self, wordlist, excludes=None):
  28. """Finds words that are in the dictionary"""
  29. if excludes is None:
  30. def filter_func(word): return word in self.word_frequency
  31. else:
  32. def filter_func(
  33. word): return word not in excludes and word in self.word_frequency
  34. return set(filter(filter_func, wordlist))
  35. DEFAULT = Dictionary(default_load_future)
  36. ALTERNATE = Dictionary(alternate_load_future)