def testsys_single_trie_match_works(self): from trienode import TrieNode N = TrieNode() N.add("jim", lambda: "jim") p = parser.Parser(lambda player: [N]) try: p.parse("jim", "player") except Match, m: self.assert_(m.callback(), "jim")
def testsys_single_trie_match_works(self): from trienode import TrieNode N = TrieNode() N.add("jim", lambda : "jim" ) p = parser.Parser( lambda player: [N] ) try: p.parse("jim", "player") except Match, m: self.assert_(m.callback(), "jim")
def add_word(self, word): # deal with invalid input if not word: pass else: #set current node at root of trie current_node = self.root # loop through letters in word for letter in word: # deal with case where letter does not yet exist at this level repeated_letter_count = current_node.repeated_letter if letter not in current_node.children: # case where two consecutive letters are the same if letter == current_node.letter: repeated_letter_count += 1 # create new node current_node.children[letter] = TrieNode( letter, repeated_letter_count) # deal with case where letter already exists at this level current_node = current_node.children[letter] #after last letter, mark end of word current_node.word_end = True
def insert(self, text): p = self.root for i in range(len(text)): index = ord(text[i]) - ord("a") if not p.children[index]: p.children[index] = TrieNode(text[i]) p = p.children[index] p.isEnding = True
def __init__(self, dictionary: str): """ Constructor: initialize a dictionary object from a text file The root of our dictionary (trie) is an empty string trie node. :param dictionary: text file containing dictionary words """ self.__dictionary = dictionary self.__root = TrieNode('') self.__load_dictionary()
def testsys_multi_trie_match_works(self): from trienode import TrieNode N = TrieNode() N.add("jim", lambda: "jim") Q = TrieNode() Q.add("fred quasar", lambda: "q") p = parser.Parser(lambda player: [N, Q]) try: p.parse("f q", "player") except Match, m: self.assert_(m.callback(), "q")
def longestWord(self, words): words = sorted(words) longest_word = None root = TrieNode("/") for i in range(len(words)): word = words[i] if not longest_word: longest_word = word p = root length_of_word = len(word) word_is_valid = True for j in range(length_of_word): index = ord(word[j]) - ord("a") if not p.children[index] and j == length_of_word - 1: p.children[index] = TrieNode(word[j]) elif not p.children[index]: word_is_valid = False break p = p.children[index] if len(word) > len( longest_word) and word > longest_word and word_is_valid: longest_word = word return longest_word
def testsys_multi_trie_match_works(self): from trienode import TrieNode N = TrieNode() N.add("jim", lambda : "jim" ) Q = TrieNode() Q.add("fred quasar", lambda : "q" ) p = parser.Parser( lambda player: [N, Q] ) try: p.parse("f q", "player") except Match, m: self.assert_(m.callback(), "q")
def add_string_helper(self, node, string, index): if string is None or index >= len(string): return None # terminating string or root if node is None: node = TrieNode('*') character = string[index] children = node.get_children() if character not in node.get_children(): #print("adding new node", character, "root node", node.get_char()) children[character] = TrieNode(character) new_node = self.add_string_helper(children[character], string, index+1) if new_node is not None: node.add_children(character, new_node) return node
#Constructs a pattern matching trie and prints list of nodes from __future__ import print_function from trienode import TrieNode import sys file_object = open(sys.argv[1]) seqs = [] node_num = 2 #Loads seqs for line in file_object: seqs.append(list(line.rstrip())) #Initializes root node root = TrieNode("R", 1) #Trie construction for seq in seqs: #Resets each new seq to start at root node current_node = root for bp in seq: #Checks if node already exists as a child for current node if all(child.bp != bp for child in current_node.children): new_node = TrieNode(bp, node_num) current_node.addChild(new_node) current_node = new_node node_num += 1
def __init__(self): self.root = TrieNode('')
def put_dictionary(): t = TrieNode("", True) t.append_all(dictionary) return t