def test_search_match_with_different_case(): root_node = Trie() root_node.insert("dimitar") expected = False output = root_node.search("DIMITAR") assert output == expected
def test_search_match_of_suffix(): root_node = Trie() root_node.insert("TESTED") expected = True output = root_node.search("TEST") assert output == expected
def test_insert(self): t = Trie() t.insert('alexandru') t.insert('alexandra') expected = {'a': {'l': {'e': {'x': {'a': {'n': {'d': {'r': {'u': {END: None}, 'a': {END: None}}}}}}}}}} self.assertEqual(t.root, expected, 'should build the correct tree')
def test_insert_with_a_key_value_pair(self): t = Trie() t.insert('alexandru', True) t.insert('alexandra', False) expected = {'a': {'l': {'e': {'x': {'a': {'n': {'d': {'r': {'u': {END: True}, 'a': {END: False}}}}}}}}}} self.assertEqual(t.root, expected, 'should build the correct tree')
def test_contains(self): # insert a word into trie t = Trie() t.insert("abba") self.assertTrue(t.contains("abba")) self.assertFalse(t.contains("hen")) self.assertFalse(t.contains("ab"))
def test_search_word_longer_than_match(): root_node = Trie() root_node.insert("TEST") expected = False output = root_node.search("TESTED") assert output == expected
def test_insert_one_word(): root_node = Trie() root_node.insert("Dimitar") expected = ['Dimitar'] output = root_node.get_words(root_node) assert output == expected
def test_insert_same_word_different_case(): root_node = Trie() root_node.insert("dimitar") root_node.insert("DIMITAR") expected = ['dimitar', 'DIMITAR'] output = root_node.get_words(root_node) assert output == expected
def simple_trie(scope="function"): x = Trie() x.insert("Norton") x.insert("North") x.insert("Nortron") x.insert("Nord") x.insert("No") return x
def test_insert_with_two_different_words_with_same_prefix(self): t = Trie() t.insert('alexandru') t.insert('alexandra') t.insert('alexandrul') expected = {'a': {'l': {'e': {'x': {'a': {'n': {'d': {'r': {'a': {END: None}, 'u': {END: None, 'l': {END: None}}}}}}}}}}} self.assertEqual(t.root, expected, 'should build the correct tree')
def test_with_prefix(self): t = Trie() for (k, v) in [('foo', 1), ('bar', 2), ('baz', 3), ('bat', 4)]: t.insert(k, v) actual = t.with_prefix('ba') expected = [('bar', 2), ('baz', 3), ('bat', 4)] self.assertItemsEqual(actual, expected, 'should export the correct values')
def test_contains(self): t = Trie() t.insert('home') t.insert('homework') self.assertFalse(t.contains('ho'), 'prefix does not count as a key') self.assertTrue(t.contains('home'), 'should have found the key') self.assertTrue(t.contains('homework'), 'should have found the key') self.assertFalse(t.contains('homeworker'), 'should have found the key')
class ReverseDNS(object): def __init__(self): self.storage = Trie() def insert(self, ip, url): self.storage.insert(ip, url) def lookup(self, ip): return self.storage.lookup(ip)
def test_lookup(self): t = Trie() for (k, v) in [('bar', 1), ('baz', 2), ('buf', 3), ('buz', 4)]: t.insert(k, v) self.assertEqual(t.lookup('bar'), 1, 'should find the correct key') self.assertIsNone(t.lookup('bus'), 'detect when a key is not present') self.assertIsNone(t.lookup('foo'), 'detect when a key is not present') self.assertIsNone(t.lookup('bazar'), 'key prefix is present but not key') self.assertIsNone(t.lookup('ba'), 'prefix of existing key does not have a value')
def test_traverse_prefix(self): my_trie = Trie() my_trie.insert('bark') my_trie.insert('bar') trie_node = my_trie.traverse_prefix('bar') self.assertEqual(trie_node.char, 'r') self.assertEqual(trie_node.val, 1) arr = [None] * 27 arr[10] = Node(10) self.assertEqual(trie_node.children[10].char, arr[10].char) self.assertEqual(trie_node.children[10].val, 1)
def test_traverse(self): keys = ['foo', 'bar', 'baz', 'barz'] t = Trie() map(lambda k: t.insert(k), keys) actual = t.traverse() expected = [('barz', None), ('bar', None), ('baz', None), ('foo', None)] self.assertEqual(expected, actual, 'should return the keys in a sorted lexicographical order')
def test__trie_auto_complete_query(self): my_trie = Trie() my_trie.insert('bark') my_trie.insert('bar') my_trie.insert('bark') results = my_trie.auto_complete_query('ba', 2) self.assertEqual(results, [(1,'bar'), (2, 'bark')])
def testTrie(): ''' Here we test algorithms for a trie tree We test insert, search, and start-with functions Each node of the trie tree is an object of the TrieNode class, which has a. children hash that stores characters as hash keys b. endOfWord boolean indicator ''' print('Create an empty trie') t = Trie() print('apple exists? ' + str(t.search('apple'))) print('\"\" exists? ' + str(t.search(''))) print('\"\" prefix exists? ' + str(t.startsWith(''))) print('Insert apple') t.insert('apple') print('apple exists? ' + str(t.search('apple'))) print('app exists? ' + str(t.search('app'))) print('app prefix exists? ' + str(t.startsWith('app'))) print('appe prefix exists? ' + str(t.startsWith('appe'))) print('Insert app') t.insert('app') print('app exists? ' + str(t.search('app'))) print('Insert @pple') t.insert('@pple') print('@pple exists? ' + str(t.search('@pple')))
class BaseSegment: def __init__(self): self.trie = Trie() def cut(self, sentence: str): pass def load_dict(self, file: str): if not os.path.exists(file): print('%s 不存在!' % file) return with open(file, 'r', encoding='utf-8') as f: for line in f: line = line.strip() line = line.split() if len(line) == 3: self.trie.insert(line[0], int(line[1]), line[2]) elif len(line) == 2: self.trie.insert(line[0], int(line[1])) else: self.trie.insert(line[0]) f.close() print('词典加载完成!')
def test_search_basic_with_multiple_words(): root_node = Trie() root_node.insert('BEAR') root_node.insert('BELL') root_node.insert('BID') root_node.insert('BULL') root_node.insert('BUY') root_node.insert('SELL') root_node.insert('STOCK') root_node.insert('STOP') expected = True output = root_node.search('BULL') assert output == expected
def test_insert_multiple_words(): root_node = Trie() root_node.insert('BEAR') root_node.insert('BELL') root_node.insert('BID') root_node.insert('BULL') root_node.insert('BUY') root_node.insert('SELL') root_node.insert('STOCK') root_node.insert('STOP') expected = ['BEAR', 'BELL', 'BID', 'BULL', 'BUY', 'SELL', 'STOCK', 'STOP'] output = root_node.get_words(root_node) assert output == expected