Skip to content

Commit

Permalink
Merge pull request #1 from wearefair/dev
Browse files Browse the repository at this point in the history
Demo can do multiple modules
  • Loading branch information
seperman authored Dec 8, 2018
2 parents 2e8cd1a + e610ce2 commit 2c3b744
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Fast Autocomplete 0.1.3
# Fast Autocomplete 0.1.4

Fast autocomplete using Directed Acyclic Word Graph (DAWG) and Levenshtein Edit Distance.

Expand Down
2 changes: 1 addition & 1 deletion fast_autocomplete/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# flake8: noqa
__version__ = '0.1.3'
__version__ = '0.1.4'
import sys
pyversion = float(sys.version[:3])
if pyversion < 3.6:
Expand Down
2 changes: 2 additions & 0 deletions fast_autocomplete/dawg.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ class _DawgNode:
set of words.
"""

__slots__ = ("word", "original_key", "children")

def __init__(self):
self.word = None
self.original_key = None
Expand Down
8 changes: 6 additions & 2 deletions fast_autocomplete/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
from fast_autocomplete.misc import read_single_keypress


def demo(autocomplete, max_cost, size):
def demo(running_modules, max_cost, size):
"""
Gets an Autocomplete instance that has already data in it and you can then run search on it in real time
"""

word_list = []

running_modules = running_modules if isinstance(running_modules, dict) else {running_modules.__class__.__name__: running_modules}

print('FAST AUTOCOMPLETE DEMO')
print('Press any key to search for. Press ctrl+c to exit')

Expand All @@ -25,6 +27,8 @@ def demo(autocomplete, max_cost, size):
joined = ''.join(word_list)
print(chr(27) + "[2J")
print(joined)
results = autocomplete.search(word=joined, max_cost=max_cost, size=size)
results = {}
for module_name, module in running_modules.items():
results[module_name] = module.search(word=joined, max_cost=max_cost, size=size)
pprint(results)
print('')
4 changes: 4 additions & 0 deletions tests/fixtures/makes_models_from_wikipedia.csv
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ Honda,Ballade
Honda,Brio
Honda,City
Honda,Civic
Honda,Civic Type R
Honda,Clarity
Honda,Crider
Honda,Elysion
Expand All @@ -335,6 +336,9 @@ Honda,StepWGN
Honda,Avancier
Honda,Vamos
Honda,Vezel
Honda,Type R
Jaguar,F-Type
Jaguar,Type
2007,2007
2017,2017
2018,2018
19 changes: 11 additions & 8 deletions tests/test_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Info(NamedTuple):
model: 'Info' = None
original_key: 'Info' = None

def get(self, key):
return getattr(self, key)
def get(self, key, default=None):
return getattr(self, key, default)

__get__ = get

Expand Down Expand Up @@ -75,12 +75,7 @@ class TestAutocomplete:
@pytest.mark.parametrize("word, max_cost, size, expected_results", [
('bmw', 2, 3, {0: [['bmw']], 1: [['bmw e9'], ['bmw e3'], ['bmw m1'], ['bmw z1']]}),
('beemer', 2, 3, {}),
('honda covic', 2, 3, {0: [['honda']],
1: [['honda', 'civic'],
['honda city'],
['honda civic'],
['honda crider'],
['honda clarity']]}),
('honda covic', 2, 3, {0: [['honda']], 1: [['honda', 'civic'], ['honda', 'civic type r']]}),
])
def test_search_without_synonyms(self, word, max_cost, size, expected_results):
auto_complete = AutoComplete(words=WIKIPEDIA_WORDS)
Expand Down Expand Up @@ -264,6 +259,13 @@ def test_search_without_synonyms(self, word, max_cost, size, expected_results):
'expected_steps': STEP_DESCENDANTS_ONLY,
'expected_find_and_sort_results': [['toyota', 'toyota camry', '2018']],
},
{'word': 'type r',
'max_cost': 3,
'size': 5,
'expected_find_results': {0: [['type', 'type r']]},
'expected_steps': STEP_DESCENDANTS_ONLY,
'expected_find_and_sort_results': [['type', 'type r']],
},
]


Expand Down Expand Up @@ -342,6 +344,7 @@ class TestPrefixAndDescendants:
('200 chrysler', '', '', ['200', 'chrysler'], 'c,h,r,y,s,l,e,r'),
('200 chrysler 200', '', '', ['200', 'chrysler', 'chrysler 200'], 'c,h,r,y,s,l,e,r, ,2,0,0'),
('chrysler 2007', '', '', ['chrysler', '2007'], '2,0,0,7'),
('type r', '', '', ['type', 'type r'], 't,y,p,e, ,r'),
])
def test_prefix_autofill(self, word, expected_matched_prefix_of_last_word,
expected_rest_of_word, expected_matched_words, expected_node_path):
Expand Down

0 comments on commit 2c3b744

Please sign in to comment.