def test_prefix(self): obj = DTrie() actual_words = ['abx','abc','abcd','bbc'] [obj.add(w) for w in actual_words] for w in actual_words: self.assertTrue(obj.isWord(w)) self.assertEqual(len(obj.getAllWords()),4) for w in actual_words: self.assertFalse( obj.isWord( w+'NA' ) ) self.assertFalse( obj.isWord( w+'DA' ) ) for pfx in ["ab","bb","bd"]: self.assertFalse( obj.isWord( pfx ) ) for pfx in ['a','ab','b','bb']: self.assertTrue( obj.hasWordPrefix(pfx) ) val = [] for pfx in ['c','ac','ad','cb','z']: #print("===> last/test ==> %s"%pfx) val.append( obj.hasWordPrefix(pfx) ) self.assertFalse( any(val) ) return
def test_prefix(self): obj = DTrie() actual_words = ["abx", "abc", "abcd", "bbc"] [obj.add(w) for w in actual_words] for w in actual_words: self.assertTrue(obj.isWord(w)) self.assertEqual(len(obj.getAllWords()), 4) for w in actual_words: self.assertFalse(obj.isWord(w + "NA")) self.assertFalse(obj.isWord(w + "DA")) for pfx in ["ab", "bb", "bd"]: self.assertFalse(obj.isWord(pfx)) for pfx in ["a", "ab", "b", "bb"]: self.assertTrue(obj.hasWordPrefix(pfx)) val = [] for pfx in ["c", "ac", "ad", "cb", "z"]: # print("===> last/test ==> %s"%pfx) val.append(obj.hasWordPrefix(pfx)) self.assertFalse(any(val)) return
def test_stuff_3letter(self): obj = DTrie() self.assertFalse( obj.isWord('apple') ) try: obj.add('') except AssertionError as exp: pass actual_words = ['a','ab','abc','bbc'] [obj.add(w) for w in actual_words] for w in actual_words: self.assertTrue( obj.isWord(w) ) self.assertEqual( sorted(obj.getAllWords()),sorted(actual_words)) self.assertEqual( obj.getAllWordsPrefix('ab'), ['ab','abc'] ) return
def test_stuff_3letter(self): obj = DTrie() self.assertFalse(obj.isWord("apple")) try: obj.add("") except AssertionError as exp: pass actual_words = ["a", "ab", "abc", "bbc"] [obj.add(w) for w in actual_words] for w in actual_words: self.assertTrue(obj.isWord(w)) self.assertEqual(sorted(obj.getAllWords()), sorted(actual_words)) self.assertEqual(obj.getAllWordsPrefix("ab"), ["ab", "abc"]) return
def test_pattiyal(self): obj = DTrie() in_words = u"டைட்டானிக் படத்தில் வரும் ஜேக் மற்றும் ரோஸ் போன்று தன் காதலை வெளிப்படுத்தும் இரு தவளைகள்".split() list(map( obj.add, in_words )) # Python 2-3 all_words_and_reverse = copy.copy(in_words) all_words_and_reverse.extend( [utf8.reverse_word( word) for word in in_words] ) actual = [obj.isWord(word) for word in all_words_and_reverse] expected = [i<len(in_words) for i in range(0,2*len(in_words))] self.assertEqual( actual, expected )
def test_trie_counts_and_prefix(self): obj = DTrie() actual_words = ['a','ab','abc','abc','bbc'] [obj.add(w) for w in actual_words] for w in actual_words: self.assertTrue(obj.isWord(w)) self.assertEqual(len(obj.getAllWords()),4) self.assertEqual( obj.getAllWordsPrefix('ab'),['ab','abc'] ) self.assertEqual(obj.getWordCount('abc'),2) obj = DTrie() list(map(obj.add,['foo','bar','bar','baz'])) self.assertEqual((obj.getWordCount('bar'),\ obj.getWordCount('baz'),\ obj.getWordCount('foo')),(2,1,1))
def test_trie_counts_and_prefix(self): obj = DTrie() actual_words = ["a", "ab", "abc", "abc", "bbc"] [obj.add(w) for w in actual_words] for w in actual_words: self.assertTrue(obj.isWord(w)) self.assertEqual(len(obj.getAllWords()), 4) self.assertEqual(obj.getAllWordsPrefix("ab"), ["ab", "abc"]) self.assertEqual(obj.getWordCount("abc"), 2) obj = DTrie() list(map(obj.add, ["foo", "bar", "bar", "baz"])) self.assertEqual( (obj.getWordCount("bar"), obj.getWordCount("baz"), obj.getWordCount("foo")), (2, 1, 1), )
def test_trie_neg(self): obj = DTrie() self.assertEqual( obj.getAllWords(), [] ) self.assertEqual( obj.getAllWordsPrefix('none'), [] ) self.assertFalse( obj.isWord('fubar',True)[0] ) self.assertTrue( obj.isWord('fubar',True)[1] )