def char(self, label): s = self.new_state() e = self.new_state() nfa = NFA(s) nfa.add_transition(s, label, e) nfa.add_final_state(e) return nfa
def dot(self): s = self.new_state() e = self.new_state() nfa = NFA(s) nfa.add_transition(s, ANY, e) nfa.add_final_state(e) return nfa
def epsilon(self): s = self.new_state() e = self.new_state() nfa = NFA(s) nfa.add_transition(s, EPSILON, e) nfa.add_final_state(e) return nfa
def charset(self, chars): s = self.new_state() e = self.new_state() nfa = NFA(s) for char in chars: nfa.add_transition(s, char, e) nfa.add_final_state(e) return e
def concat(self, n1, n2): s = self.new_state() m = self.new_state() e = self.new_state() nfa = NFA(s) nfa.insert(s, n1, m) nfa.insert(m, n2, e) nfa.add_final_state(e) return nfa
def choice(self, n1, n2): s = self.new_state() s1 = self.new_state() s2 = self.new_state() e1 = self.new_state() e2 = self.new_state() e = self.new_state() nfa = NFA(s) nfa.add_transition(s, EPSILON, s1) nfa.add_transition(s, EPSILON, s2) nfa.insert(s1, n1, e1) nfa.insert(s2, n2, e2) nfa.add_transition(e1, EPSILON, e) nfa.add_transition(e2, EPSILON, e) nfa.add_final_state(e) return nfa
def star(self, n): s = self.new_state() m1 = self.new_state() m2 = self.new_state() e = self.new_state() nfa = NFA(s) nfa.add_transition(s, EPSILON, m1) nfa.add_transition(s, EPSILON, e) nfa.insert(m1, n, m2) nfa.add_transition(m2, EPSILON, m1) nfa.add_transition(m2, EPSILON, e) nfa.add_final_state(e) return nfa
def glob_automaton(pattern): nfa = NFA(0) i = -1 for i, (op, arg) in enumerate(parse_glob(pattern)): if op is _LIT: nfa.add_transition(i, arg, i + 1) elif op is _STAR: nfa.add_transition(i, ANY, i + 1) nfa.add_transition(i, EPSILON, i + 1) nfa.add_transition(i + 1, EPSILON, i) elif op is _QUEST: nfa.add_transition(i, ANY, i + 1) elif op is _RANGE: for char in arg[0]: nfa.add_transition(i, char, i + 1) nfa.add_final_state(i + 1) return nfa
def levenshtein_automaton(term, k, prefix=0): nfa = NFA((0, 0)) if prefix: for i in xrange(prefix): c = term[i] nfa.add_transition((i, 0), c, (i + 1, 0)) for i in xrange(prefix, len(term)): c = term[i] for e in xrange(k + 1): # Correct character nfa.add_transition((i, e), c, (i + 1, e)) if e < k: # Deletion nfa.add_transition((i, e), ANY, (i, e + 1)) # Insertion nfa.add_transition((i, e), EPSILON, (i + 1, e + 1)) # Substitution nfa.add_transition((i, e), ANY, (i + 1, e + 1)) for e in xrange(k + 1): if e < k: nfa.add_transition((len(term), e), ANY, (len(term), e + 1)) nfa.add_final_state((len(term), e)) return nfa