def test_delete_word_with_no_common_first_letters(self): trie = Trie() first_noun = Noun({'Entry': 'Hund'}) second_noun = Noun({'Entry': 'Katze'}) trie.add_word(first_noun) trie.add_word(second_noun) trie.delete_word('Hund') self.assertEqual(trie.search_for_word('Hund'), None) self.assertEqual(trie.search_for_word('Katze'), second_noun)
def test_delete_word_that_has_common_first_letters(self): trie = Trie() first_noun = Noun({'Entry': 'Baum'}) second_noun = Noun({'Entry': 'Bart'}) trie.add_word(first_noun) trie.add_word(second_noun) trie.delete_word('Bart') self.assertEqual(trie.search_for_word('Baum'), first_noun) self.assertEqual(trie.search_for_word('Bart'), None) self.assertEqual(trie.search_for_word('Ba'), None)
def test_add_single_word(self): trie = Trie() test_noun = Noun({'Entry': 'Hund'}) self.assertEqual(trie.search_for_word('Hund'), None) trie.add_word(test_noun) self.assertEqual(trie.search_for_word('Hund'), test_noun) self.assertEqual(trie.search_for_word('Hun'), None) self.assertEqual(trie.search_for_word('Hu'), None) self.assertEqual(trie.search_for_word('H'), None)
def initialize_trie(self): self.trie = Trie() all_words = [] for part_of_speech in ['Nouns', 'Verbs', 'Adjectives']: all_words_from_one_pos = DatabaseHandler.\ extract_parts_of_speech([part_of_speech], self.database) class_name = getattr(sys.modules[__name__], part_of_speech[:-1]) for word_hash in all_words_from_one_pos: all_words.append(class_name(word_hash)) for word in all_words: self.trie.add_word(word)
def test_add_word_with_common_first_letters(self): trie = Trie() first_noun = Noun({'Entry': 'Hund'}) second_noun = Noun({'Entry': 'Hundert'}) trie.add_word(first_noun) trie.add_word(second_noun) self.assertEqual(trie.search_for_word('Hund'), first_noun) self.assertEqual(trie.search_for_word('Hundert'), second_noun)
class Dictionary: def __init__(self, database, trie=False): self.database = database self.trie_on = trie if trie: self.initialize_trie() def initialize_trie(self): self.trie = Trie() all_words = [] for part_of_speech in ['Nouns', 'Verbs', 'Adjectives']: all_words_from_one_pos = DatabaseHandler.\ extract_parts_of_speech([part_of_speech], self.database) class_name = getattr(sys.modules[__name__], part_of_speech[:-1]) for word_hash in all_words_from_one_pos: all_words.append(class_name(word_hash)) for word in all_words: self.trie.add_word(word) def extract_entry(self, word): try: found_entry = self.trie.search_for_word(word) if found_entry: return found_entry raise DatabaseError except AttributeError: entry_data = DatabaseHandler.extract_entry(word, self.database) class_name = getattr(sys.modules[__name__], entry_data[1][:-1]) return class_name(entry_data[0]) def add_entry(self, word): word.add_entry(self.database) try: self.trie.add_word(word) except AttributeError: return def exists_entry(self, word): try: return bool(self.trie.search_for_word(word)) except AttributeError: return DatabaseHandler.exists_entry(word, self.database) def delete_entry(self, word): DatabaseHandler.delete_entry(word, self.database) try: self.trie.delete_word(word) except AttributeError: return def extract_entries_with_meaning(self, meaning): found_entries_data = DatabaseHandler.\ extract_with_meaning(meaning, self.database) found_entries = [] for word_type in found_entries_data: for entry_data, part_of_speech in word_type: class_name = getattr(sys.modules[__name__], part_of_speech) found_entries.append(class_name(entry_data)) return found_entries def edit_entry(self, entry, field, new_value): DatabaseHandler.edit_entry(entry, field, new_value, self.database) try: word_in_trie = self.trie.search_for_word(entry) if field == 'Entry': self.trie.delete_word(word_in_trie.word_hash['Entry']) word_in_trie.word_hash['Entry'] = new_value self.trie.add_word(word_in_trie) else: word_in_trie.word_hash[field] = new_value except AttributeError: return def toggle_trie(self): self.trie_on = not self.trie_on if self.trie_on: self.trie = self.initialize_trie() else: self.trie = None
def test_delete_word_that_contains_another_as_substring(self): trie = Trie() first_noun = Noun({'Entry': 'Hund'}) second_noun = Noun({'Entry': 'Hundert'}) trie.add_word(first_noun) trie.add_word(second_noun) trie.delete_word('Hundert') self.assertEqual(trie.search_for_word('Hund'), first_noun) self.assertEqual(trie.search_for_word('Hundert'), None) self.assertEqual(trie.search_for_word('Hunde'), None)
def test_delete_single_word(self): trie = Trie() test_noun = Noun({'Entry': 'Hund'}) trie.add_word(test_noun) trie.delete_word('Hund') self.assertEqual(trie.search_for_word('Hund'), None)