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.

52 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(bound_method, *args, **kwargs):
  12. bound_method(*args, **kwargs)
  13. obj = bound_method.__self__
  14. return obj
  15. default_load_future = EXECUTOR.submit(
  16. do_operation, DEFAULT.load_dictionary, english)
  17. alternate_load_future = EXECUTOR.submit(
  18. do_operation, ALTERNATE.load_text_file, english)
  19. class Dictionary:
  20. def __init__(self, loader_future: concurrent.futures._base.Future):
  21. self.future = loader_future
  22. self._word_frequency = None
  23. @property
  24. def word_frequency(self) -> spellchecker.WordFrequency:
  25. if self._word_frequency is None:
  26. self._word_frequency = self.future.result()
  27. return self._word_frequency
  28. def filter(self, wordlist, excludes=None):
  29. """Finds words that are in the dictionary"""
  30. if excludes is None:
  31. def filter_func(word): return word in self.word_frequency
  32. else:
  33. def filter_func(
  34. word):
  35. return word not in excludes and word in self.word_frequency
  36. return set(filter(filter_func, wordlist))
  37. DEFAULT = Dictionary(default_load_future)
  38. ALTERNATE = Dictionary(alternate_load_future)