def test_add_name(self): root = trie.TrieNode('*') self.assertEqual(trie.find_autocomplete(root, 'WhatsApp Messenger'), []) trie.add(root, 'WhatsApp Messenger') self.assertEqual(trie.find_autocomplete(root, 'WhatsApp Messenger'), ['WhatsApp Messenger'])
def rhyme(N, words): root = TrieNode('*') for w in words: w = w[::-1] add(root, w) def calc_minimum_num_of_unpaired_words(node): """ Calculates the minimum number of unpaired words for each node. :param node: The current node. :return: Number of unpaired words at that node. """ if len(node.children) == 0: # Got leave --> word ends here return 1 r = sum(calc_minimum_num_of_unpaired_words(c) for c in node.children) if node.word_finished: # Word finishes --> got a new word here r += 1 if node.char != '*' and r >= 2: # If we have more than two words, we can pair them and remove # them. r = r - 2 return r fv_root = calc_minimum_num_of_unpaired_words(root) result = N - fv_root return result
def create_trie(name): with open("REM_Keywords.txt", "r") as f: keyword_master = trie.TrieNode("*") for item in list(f): key = re.sub(r'\t+', "", item).rstrip() trie.add(keyword_master, key) with open(name + '.pkl', 'wb') as f: pickle.dump(keyword_master, f, pickle.HIGHEST_PROTOCOL)
def test_add_two_words_same_stem(self): root = {} expected1 = 'bat' expected2 = 'bar' self.assertFalse(trie.contains(root, expected1)) self.assertFalse(trie.contains(root, expected2)) trie.add(root, expected1) trie.add(root, expected2) self.assertTrue(trie.contains(root, expected1)) self.assertTrue(trie.contains(root, expected2))
def test_everything(self): root = {} self.assertSetEqual(set(), trie.everything(root)) expected1 = 'bat' expected2 = 'bar' self.assertFalse(trie.contains(root, expected1)) self.assertFalse(trie.contains(root, expected2)) trie.add(root, expected1) trie.add(root, expected2) self.assertSetEqual({expected1, expected2}, trie.everything(root))
def load_data(root, filename): """ Loads the data in the file specified in the filename on the trie root parameter """ try: with open(args.filename, 'r') as csv_file: for line in csv.reader(csv_file): trie.add(root, line[0]) return root except (FileNotFoundError, IOError): print("Specified file does not exist or insuficient permissions") exit(2)
class TestTrieSearchMethods(unittest.TestCase): #Test initialization root = trie.TrieNode('*') with open('test_files/190titles.csv', 'r') as csv_file: for line in csv.reader(csv_file): trie.add(root, line[0]) def test_not_found(self): self.assertEqual(trie.find_autocomplete(self.root, 'ABCD1234'), []) def test_exact_match(self): self.assertEqual( trie.find_autocomplete(self.root, 'WhatsApp Messenger'), ['WhatsApp Messenger']) def test_unique_match(self): self.assertEqual(trie.find_autocomplete(self.root, 'What'), ['WhatsApp Messenger']) def test_multiple_match(self): self.assertEqual(len(trie.find_autocomplete(self.root, 'W')), 4) def test_space_on_prefix(self): self.assertEqual(trie.find_autocomplete(self.root, 'Facebook L'), ['Facebook Lite'])
def test_count(self): root = {} self.assertEqual(0, trie.count(root)) trie.add(root, 'a') self.assertEqual(1, trie.count(root)) trie.add(root, 'b') self.assertEqual(2, trie.count(root)) trie.add(root, 'c') self.assertEqual(3, trie.count(root)) trie.add(root, 'd') self.assertEqual(4, trie.count(root))
def test_find_prefix_single_more(self): root = trie.TrieNode('*') trie.add(root, 'WhatsApp Messenger') trie.add(root, 'Facebook') trie.add(root, 'PayPal') self.assertEqual(trie.find_prefix(root, 'WhatsApp Messenger'), (True, 1))
def test_she_shells(self): root = {} self.assertSetEqual(set(), trie.everything(root)) expected1 = 'she' expected2 = 'sells' expected3 = 'sea' expected4 = 'shells' trie.add(root, expected1) trie.add(root, expected2) trie.add(root, expected3) trie.add(root, expected4) self.assertSetEqual({expected1, expected2, expected3, expected4}, trie.everything(root))
def test_longestprefix(self): root = {} self.assertSetEqual(set(), trie.everything(root)) expected1 = 'she' expected2 = 'sells' expected3 = 'sea' trie.add(root, expected1) trie.add(root, expected2) trie.add(root, expected3) self.assertTrue('she' == trie.longestprefix(root, 'shells'))
def test_remove(self): expected_word = 'bat' root = {} self.assertFalse(trie.contains(root, expected_word)) trie.add(root, expected_word) self.assertTrue(trie.contains(root, expected_word)) trie.remove(root, expected_word) self.assertFalse(trie.contains(root, expected_word)) trie.add(root, 'bar') trie.add(root, 'baz') trie.remove(root, 'bar') self.assertFalse(trie.contains(root, 'bar')) self.assertTrue(trie.contains(root, 'baz'))
def test_startswith(self): root = {} self.assertSetEqual(set(), trie.everything(root)) expected1 = 'foo' expected2 = 'bar' expected3 = 'baz' trie.add(root, expected1) trie.add(root, expected2) trie.add(root, expected3) self.assertSetEqual({expected2, expected3}, trie.startswith(root, 'ba')) self.assertSetEqual(set(), trie.startswith(root, 'z'))
import trie from itertools import permutations if __name__ == "__main__": root = trie.TrieNode('*') #import first names with open("./names/male-first-names.txt", "r") as first_names_file: first_names = first_names_file.readlines() for name in first_names: trie.add(root, name.lower().replace('\n', '')) while name != 'quit': name = input( "Enter the letters you know with underscores for letters you don't know:\n" ) #get indices of unknown characters indice_list = [pos for pos, char in enumerate(name) if char == '_'] for i in indice_list: name = name[:i] + 'a' + name[i + 1:] perm = permutations([ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ], len(indice_list)) name_set = set() for letters_permutation in list(perm): i = 0
cap_params = {} cap_params['im_width'], cap_params['im_height'] = video_capture.size() cap_params['num_hands_detect'] = 1 cap_params['hand_score_thresh'] = 0.35 cap_params['hand_movement_thresh'] = 40 if args.train: # If you find that certain letters are not being recognized, or if you are using a different letter set # than I am, you'll have to capture your own training data and retrain the CNN. You can capture training # data frames by pressing Enter at any time and frames will be written to images/[A-Z]/ letter_utils.train() else: predictor = trie.TrieNode('*') for word in read_from_csv('brown_words'): if len(word[0]) <= args.length: trie.add(predictor, word[0]) words = read_from_csv(args.corpus) # You can modify the default accolades and criticisms by modifying the CSV files in corpus/ accolades = read_from_csv('accolades') word_accolades = read_from_csv('word_accolades') letter_criticisms = read_from_csv('letter_criticisms') thinking = read_from_csv('thinking') player_choose_word = read_from_csv('player_choose_word') mode = 1 directory = os.path.dirname(os.path.realpath(__file__)) iterations = 0 last_letters = [] last_letter = ''
def test_find_prefix_multiple(self): root = trie.TrieNode('*') trie.add(root, 'WhatsApp Messenger') trie.add(root, 'Facebook') trie.add(root, 'Facebook Lite') self.assertEqual(trie.find_prefix(root, 'Face'), (True, 2))
def test_find_prefix_single_only(self): root = trie.TrieNode('*') trie.add(root, 'WhatsApp Messenger') self.assertEqual(trie.find_prefix(root, 'WhatsApp Messenger'), (True, 1))
def test_add(self): expected_word = 'bat' root = {} self.assertFalse(trie.contains(root, expected_word)) trie.add(root, expected_word) self.assertTrue(trie.contains(root, expected_word))
import sys import binary_search as Binary import trie as Trie # Around 61k words dictionary_file = open("usa.txt", "r") dictionary_file2 = open("usa.txt", "r") # Around 195k words # dictionary_file = open("english3.txt", "r") # dictionary_file2 = open("english3.txt", "r") root = Trie.TrieNode('*') for word in dictionary_file: word = word.strip() Trie.add(root, word) dictionary_arr = dictionary_file2.read().splitlines() dictionary_arr.sort() def trie_test(): return Trie.find_prefix(root, 'appl') def binary_test(): return Binary.binary_search(dictionary_arr, 'appl') if __name__ == "__main__": trie_times = timeit.repeat("trie_test()",
import trie root = {} trie.add(root, "foo") trie.add(root, "bar") trie.add(root, "baz") trie.add(root, "bot") print(trie.to_json(root)) print(trie.startswith(root, 'b')) print(trie.startswith(root, 'ba')) print(trie.startswith(root, 'f'))
GPIO.setup(BUTTON_1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(BUTTON_2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(BUTTON_3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(BUTTON_4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(BUTTON_5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) pindex = 0 sentence = "" word = "" map = " abcdefghijklmnopqrstuvwxyz" trie = trie.Trie() f = open("words.txt", "r") for line in f: line = line[:len(line) - 2] trie.add(line) while True: cindex = 0 if (GPIO.input(BUTTON_5) == True): cindex += 16 if (GPIO.input(BUTTON_4) == True): cindex += 8 if (GPIO.input(BUTTON_3) == True): cindex += 4 if (GPIO.input(BUTTON_2) == True): cindex += 2 if (GPIO.input(BUTTON_1) == True): cindex += 1 if (cindex == 0 and pindex != 0):
def names_trie(names): name_tree = trie.TrieNode('*') [trie.add(name_tree, name) for name in names] return name_tree
import trie wordle_202201 = "LIGHT,WRUNG,COULD,PERKY,MOUNT,WHACK,SUGAR,KNOLL,CRIMP,WINCE,PRICK,ROBOT,POINT,PROXY,SHIRE,SOLAR," \ "PANIC,TANGY,ABBEY,FAVOR,DRINK,QUERY,GORGE,CRANK,SLUMP,BANAL,TIGER,SIEGE,TRUSS,BOOST,REBUS" wordle_202202 = "CHOKE,CHANT,SPILL,VIVID,BLOKE,TROVE,THORN,OTHER,TACIT,SWILL,DODGE,SHAKE,CAULK,AROMA,CYNIC,ROBIN," \ "ULTRA,ULCER,PAUSE,HUMOR,FRAME,ELDER,SKILL,ALOFT,PLEAT,SHARD,MOIST,THOSE" wordle_202203 = "LOWLY,STOVE,SHALL,FOUND,NYMPH,EPOCH,DEPOT,CHEST,PURGE,SLOSH,THEIR,RENEW,ALLOW,SAUTE,MOVIE,CATER," \ "TEASE,SMELT,FOCUS,TODAY,WATCH,LAPSE,MONTH,SWEET,HOARD,CLOTH,BRINE,AHEAD,MOURN,NASTY,RUPEE" if __name__ == '__main__': root = {} for w in wordle_202201.split(','): trie.add(root, w.lower()) for w in wordle_202202.split(','): trie.add(root, w.lower()) for w in wordle_202203.split(','): trie.add(root, w.lower()) # print(trie.to_json(root)) # print(sorted(trie.startswith(root, 's'))) # print(trie.to_json(trie.find(root, 's'))) # print(sorted(trie.startswith(root, 'c'))) # print(trie.to_json(trie.find(root, 'c'))) # print(sorted(trie.startswith(root, 't'))) # print(trie.to_json(trie.find(root, 't'))) # print(sorted(trie.startswith(root, 'r'))) print(trie.to_json(trie.find(root, 'r'))) print(trie.count(root))