def preprocessPattern(self, pattern): # start at the root node currentNode = self.root # if we mismatch at the root, advance one position on the target currentNode.targetShift = 1 # add a failure link to the root itself currentNode.setFailureLink(self.root) for c in pattern: # make a new node newNode = ExactMatcherNode() # add a failure link to the root newNode.setFailureLink(self.root) # if we mismatch at the new node, we matched the current node # so shift at the target one more position to the left newNode.targetShift = currentNode.targetShift - 1 # add the new node as a child indexed by character currentNode.setChild(c, newNode) # make the new node the current node currentNode = newNode
def preprocessPattern(self, pattern): currentNode = self.root currentNode.targetShift = 1 currentNode.setFailureLink(self.root) for i in xrange(len(pattern)): c = pattern[i] newNode = ExactMatcherNode() failNode = self.findFailNode(currentNode, c) newNode.setFailureLink(failNode) newNode.targetShift = 0 currentNode.setChild(c, newNode) currentNode = newNode # optimize failure links # implements algorithm SP'(P) in Section 3.3.4 of book currentNode = self.root while not currentNode.isLeaf(): nextNode = currentNode.getChild(0) v = currentNode.failureLink x = '$' if nextNode.isLeaf() else nextNode.getChild(0) if v.isMatch(x): nextNode.setFailureLink(v.failureLink) currentNode = nextNode