diff --git a/fast_autocomplete/dwg.py b/fast_autocomplete/dwg.py index 9c0f7e0..fda3669 100644 --- a/fast_autocomplete/dwg.py +++ b/fast_autocomplete/dwg.py @@ -201,7 +201,12 @@ def insert_word_branch(self, word, leaf_node=None, add_word=True, original_key=N count=count, insert_count=self.SHOULD_INCLUDE_COUNT ) - temp_leaf_node.children[word[-1]] = leaf_node + # It already has children + if temp_leaf_node.children and word[-1] in temp_leaf_node.children: + temp_leaf_node.children[word[-1]].word = leaf_node.word + # otherwise merge into the leaf node + else: + temp_leaf_node.children[word[-1]] = leaf_node else: leaf_node = self._dwg.insert( word, diff --git a/tests/test_autocomplete.py b/tests/test_autocomplete.py index 7943198..88e068c 100644 --- a/tests/test_autocomplete.py +++ b/tests/test_autocomplete.py @@ -101,6 +101,13 @@ def test_search_unicode_without_synonyms(self, word, max_cost, size, expected_re print_results(locals()) assert expected_results == results + def test_autocomplete_synonym_part_of_another_word(self): + words = {'cartoon': {}, 'vehicle': {}} + synonyms = {'vehicle': ['car']} + autocomplete = AutoComplete(words=words, synonyms=synonyms) + result = autocomplete.search(word='ca') + assert [['vehicle'], ['cartoon']] == result + STEP_DESCENDANTS_ONLY = [FindStep.descendants_only] STEP_FUZZY_FOUND = [FindStep.fuzzy_try, FindStep.fuzzy_found]