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_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 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 create_trie(self, data: Dict): trie_node = trie.TrieNode() for key, value in data.items(): trie.insert(trie_node, [key, value]) return trie_node
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 generateTrie(self, csv): self.trie_data = trie.TrieNode() words = self.loadCSV(csv) print "Number of words loaded from csv: ", len(words) # have to start counter at 1 cause of None evaluating to <= 0 counter = 1 for word in words: self.trie_data.add(word, counter) counter += 1 self.num_words += 1 return self.trie_data
class TestTrieSearchMethods(unittest.TestCase): #Test initialization root = trie.TrieNode('*') def test_empty_trie(self): root = trie.TrieNode('*') self.assertEqual(trie.find_autocomplete(root, ''), []) 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 test_no_duplicates(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']) trie.add(root, 'WhatsApp Messenger') self.assertEqual(trie.find_autocomplete(root, 'WhatsApp Messenger'), ['WhatsApp Messenger']) 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_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_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 names_trie(names): name_tree = trie.TrieNode('*') [trie.add(name_tree, name) for name in names] return name_tree
if not video_capture: raise Exception("Camera with device id " + args.input + "could not be initialized") 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__))
def __init__(self): # init an empty trie with this object self.trie_data = trie.TrieNode() self.num_words = 0
def test_empty_trie(self): root = trie.TrieNode('*') self.assertEqual(trie.find_autocomplete(root, ''), [])
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))
import timeit 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__":
from argparse import ArgumentParser from flask import Flask, request import csv import trie import json root = trie.TrieNode('*') app = Flask(__name__) 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) def read_input(): """ Reads input parameters """ parser = ArgumentParser() parser.add_argument("-i",