From f87af480d1cd4f93abd47d717ca4b6b0fe6630b3 Mon Sep 17 00:00:00 2001 From: fifimajster Date: Thu, 29 Aug 2019 20:03:24 +0200 Subject: [PATCH 1/2] optimize by using generators ignore .pyc files --- .gitignore | 1 + autocorrect/word.py | 38 ++++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e99e36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc \ No newline at end of file diff --git a/autocorrect/word.py b/autocorrect/word.py index 8d501ae..7630498 100644 --- a/autocorrect/word.py +++ b/autocorrect/word.py @@ -41,52 +41,58 @@ def __init__(self, word): def _deletes(self): """th""" - return {concat(a, b[1:]) - for a, b in self.slices[:-1]} + return (concat(a, b[1:]) + for a, b in self.slices[:-1]) def _transposes(self): """teh""" - return {concat(a, reversed(b[:2]), b[2:]) - for a, b in self.slices[:-2]} + return (concat(a, reversed(b[:2]), b[2:]) + for a, b in self.slices[:-2]) def _replaces(self): """tge""" - return {concat(a, c, b[1:]) + return (concat(a, c, b[1:]) for a, b in self.slices[:-1] - for c in ALPHABET} + for c in ALPHABET) def _inserts(self): """thwe""" - return {concat(a, c, b) + return (concat(a, c, b) for a, b in self.slices - for c in ALPHABET} + for c in ALPHABET) def typos(self): """letter combinations one typo away from word""" - return (self._deletes() | self._transposes() | - self._replaces() | self._inserts()) + yield from self._deletes() + yield from self._transposes() + yield from self._replaces() + yield from self._inserts() def double_typos(self): """letter combinations two typos away from word""" - return {e2 for e1 in self.typos() - for e2 in Word(e1).typos()} + return (e2 for e1 in self.typos() + for e2 in Word(e1).typos()) def common(words): """{'the', 'teh'} => {'the'}""" - return set(words) & NLP_WORDS + return set(word for word in words + if word in NLP_WORDS) def exact(words): """{'Snog', 'snog', 'Snoddy'} => {'Snoddy'}""" - return set(words) & MIXED_CASE + return set(word for word in words + if word in MIXED_CASE) def known(words): """{'Gazpacho', 'gazzpacho'} => {'gazpacho'}""" - return {w.lower() for w in words} & KNOWN_WORDS + return set(word.lower() for word in words + if word.lower() in KNOWN_WORDS) def known_as_lower(words): """{'Natasha', 'Bob'} => {'bob'}""" - return {w.lower() for w in words} & LOWERCASE + return set(word.lower() for word in words + if word.lower() in LOWERCASE) def get_case(word, correction): """ From 9fc62c505af07aba008959894906c2085ed38ab0 Mon Sep 17 00:00:00 2001 From: fifimajster Date: Sun, 15 Sep 2019 20:24:43 +0200 Subject: [PATCH 2/2] more pythonic --- autocorrect/word.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/autocorrect/word.py b/autocorrect/word.py index 7630498..249ac5b 100644 --- a/autocorrect/word.py +++ b/autocorrect/word.py @@ -41,25 +41,25 @@ def __init__(self, word): def _deletes(self): """th""" - return (concat(a, b[1:]) - for a, b in self.slices[:-1]) + for a, b in self.slices[:-1]: + yield concat(a, b[1:]) def _transposes(self): """teh""" - return (concat(a, reversed(b[:2]), b[2:]) - for a, b in self.slices[:-2]) + for a, b in self.slices[:-2]: + yield concat(a, reversed(b[:2]), b[2:]) def _replaces(self): """tge""" - return (concat(a, c, b[1:]) - for a, b in self.slices[:-1] - for c in ALPHABET) + for a, b in self.slices[:-1]: + for c in ALPHABET: + yield concat(a, c, b[1:]) def _inserts(self): """thwe""" - return (concat(a, c, b) - for a, b in self.slices - for c in ALPHABET) + for a, b in self.slices: + for c in ALPHABET: + yield concat(a, c, b) def typos(self): """letter combinations one typo away from word""" @@ -70,8 +70,9 @@ def typos(self): def double_typos(self): """letter combinations two typos away from word""" - return (e2 for e1 in self.typos() - for e2 in Word(e1).typos()) + for e1 in self.typos(): + for e2 in Word(e1).typos(): + yield e2 def common(words):