示例#1
0
    def __init_goto_list(self):
        newstate = 0

        def enter(word):
            assert type(word) == str, "word must be a string"
            nonlocal newstate
            state = 0
            j = 0

            # check if a prefix of word exists
            tmp = self.goto_list[state].findval(word[j])
            while j < len(word) and self.goto_list[state].findval(
                    word[j]) != -1:
                state = self.goto_list[state].findval(word[j])[1]
                j = j + 1
            # create states for all alphabets, that are not presented in the automata
            if j < len(word):
                for p in range(j, len(word)):
                    newstate += 1
                    self.goto_list[state].insert(word[p], newstate)
                    self.goto_list.append(BinarySearchTree.Node(word[p]))
                    self.output_list.append(None)
                    state = newstate
            # initialize output function for the inserted word
            self.output_list[state] = LinkedList.LinkedList(word)

        self.goto_list.append(BinarySearchTree.Node(chr(0)))
        self.output_list.append(None)
        for i in range(len(self.keywords)):
            enter(self.keywords[i])