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.

46 lines
1.3 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. import time
  14. time.sleep(60)
  15. return obj
  16. default_load_future = EXECUTOR.submit(
  17. do_operation, DEFAULT, DEFAULT.load_dictionary, english)
  18. alternate_load_future = EXECUTOR.submit(
  19. do_operation, ALTERNATE, ALTERNATE.load_text_file, english)
  20. class Dictionary:
  21. def __init__(self, loader_future: concurrent.futures._base.Future):
  22. self.future = loader_future
  23. self._word_frequency = None
  24. @property
  25. def word_frequency(self) -> spellchecker.WordFrequency:
  26. if self._word_frequency is None:
  27. self._word_frequency = self.future.result()
  28. return self._word_frequency
  29. def filter(self, wordlist):
  30. """Finds words that are in the dictionary"""
  31. return set(filter(wordlist, lambda word: word in self.word_frequency))
  32. DEFAULT = Dictionary(default_load_future)
  33. ALTERNATE = Dictionary(alternate_load_future)