From 63196643c24acb2b6d81fccaf7df20a547ef939b Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Wed, 22 May 2019 20:10:05 -0500 Subject: [PATCH] Added Dictionary class --- dictionary.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 dictionary.py diff --git a/dictionary.py b/dictionary.py new file mode 100644 index 0000000..7d98bad --- /dev/null +++ b/dictionary.py @@ -0,0 +1,46 @@ +import spellchecker +import pathlib +import concurrent +from concurrent.futures import ThreadPoolExecutor +EXECUTOR = ThreadPoolExecutor() +DEFAULT = spellchecker.WordFrequency() +ALTERNATE = spellchecker.WordFrequency() + + +package_root = pathlib.Path(spellchecker.__file__) / '..' +english = package_root / 'resources' / 'en.json.gz' + +extra_words = pathlib.Path(__file__, '..', 'extra_words.txt') + + +def do_operation(obj, bound_method, *args, **kwargs): + bound_method(*args, **kwargs) + import time + time.sleep(60) + return obj + + +default_load_future = EXECUTOR.submit( + do_operation, DEFAULT, DEFAULT.load_dictionary, english) +alternate_load_future = EXECUTOR.submit( + do_operation, ALTERNATE, ALTERNATE.load_text_file, english) + + +class Dictionary: + def __init__(self, loader_future: concurrent.futures._base.Future): + self.future = loader_future + self._word_frequency = None + + @property + def word_frequency(self) -> spellchecker.WordFrequency: + if self._word_frequency is None: + self._word_frequency = self.future.result() + return self._word_frequency + + def filter(self, wordlist): + """Finds words that are in the dictionary""" + return set(filter(wordlist, lambda word: word in self.word_frequency)) + + +DEFAULT = Dictionary(default_load_future) +ALTERNATE = Dictionary(alternate_load_future)