class TamilDTriesForward(unittest.TestCase): def setUp(self): rhymie = [ (u"மாங்குயில்", u"ல்யிகுங்மா"), (u"பூங்குயில்", u"ல்யிகுங்பூ"), (u"அல்லவா", u"வாலல்அ"), (u"செல்வாயா", u"யாவால்செ"), (u"சொல்வாயா", u"யாவால்சொ"), ] self.obj = DTrie() self.count = len(rhymie) for k, v in rhymie: self.obj.add(v) return def test_all_words(self): self.assertTrue(len(self.obj.getAllWords()), self.count) def word_n_prefix_test(self, pfx, wordlist, no): itr = 0 for word in self.obj.getAllWordsPrefix(pfx): itr = itr + 1 self.assertTrue(word in wordlist) self.assertEqual(itr, no) return def test_fwd_dictionaries(self): kuyils = [u"ல்யிகுங்பூ", u"ல்யிகுங்மா"] pfx = u"ல்யிகு" self.word_n_prefix_test(pfx, kuyils, 2) def test_fwd_dictionaries_two(self): verbie = [u"யாவால்செ", u"யாவால்சொ"] pfx = u"யாவா" self.word_n_prefix_test(pfx, verbie, 2)
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
class TamilDTriesForward(unittest.TestCase): def setUp(self): rhymie = [(u"மாங்குயில்",u"ல்யிகுங்மா"),(u"பூங்குயில்",u"ல்யிகுங்பூ"),(u"அல்லவா",u"வாலல்அ"),\ (u"செல்வாயா",u"யாவால்செ"),(u"சொல்வாயா",u"யாவால்சொ")] self.obj = DTrie() self.count = len(rhymie) for k,v in rhymie: self.obj.add(v) return def test_all_words(self): self.assertTrue(len(self.obj.getAllWords()),self.count) def word_n_prefix_test(self,pfx,wordlist,no): itr = 0 for word in self.obj.getAllWordsPrefix(pfx): itr = itr + 1 self.assertTrue(word in wordlist) self.assertEqual(itr,no) return def test_fwd_dictionaries(self): kuyils = [u"ல்யிகுங்பூ",u"ல்யிகுங்மா"] pfx = u"ல்யிகு" self.word_n_prefix_test(pfx,kuyils,2) def test_fwd_dictionaries_two(self): verbie = [u"யாவால்செ",u"யாவால்சொ"] pfx = u"யாவா" self.word_n_prefix_test(pfx,verbie,2)
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_load_dictionary(self): obj = DTrie() obj.loadWordFile(DICTIONARY_DATA_FILES['tamilvu']) self.assertEqual(len(obj.getAllWords()),63896) count = 0 for word in obj.getAllWordsIterable(): count = count + 1 self.assertEqual(count,63896) words = obj.getAllWordsPrefix(u'பெரு') print(len(words)) #for w in words: # print(w) self.assertEqual( len(words), 215 )
def test_load_dictionary(self): obj = DTrie() obj.loadWordFile(DICTIONARY_DATA_FILES['tamilvu']) self.assertEqual(len(obj.getAllWords()),63896) count = 0 for word in obj.getAllWordsIterable(): count = count + 1 self.assertEqual(count,63896) words = obj.getAllWordsPrefix(u'பெரு') print(len(words)) #for w in words: # print(w) self.assertEqual( len(words), 215 )
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_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_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_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_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] )
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] )