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

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(bound_method, *args, **kwargs):
bound_method(*args, **kwargs)
obj = bound_method.__self__
return obj
default_load_future = EXECUTOR.submit(
do_operation, DEFAULT.load_dictionary, english)
alternate_load_future = EXECUTOR.submit(
do_operation, 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, excludes=None):
"""Finds words that are in the dictionary"""
if excludes is None:
def filter_func(word): return word in self.word_frequency
else:
def filter_func(
word):
return word not in excludes and word in self.word_frequency
return set(filter(filter_func, wordlist))
DEFAULT = Dictionary(default_load_future)
ALTERNATE = Dictionary(alternate_load_future)