Browse Source

Added Dictionary class

cache_money
Raphael Roberts 7 years ago
parent
commit
63196643c2
  1. 46
      dictionary.py

46
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)
Loading…
Cancel
Save