我正在尝试创建一种方法是缩写从一个点跳到另一个点。
我用当前的边缘创建了一个 NFA
EDGES = [
(0, 'h', 1),
(1,'a',2),
(2,'z', 3),
(3,'a',4),
(4, 'r', 5),
(5, 'd', 6)
)]
我正在尝试完成的示例
nrec("h-rd", nfa, 1)
应该返回accept
nrec
是为 NFA 处理字符串并检查它是否接受或拒绝的方法。
def nrec(tape, nfa, trace=0):
"""Recognize in linear time similarly to transform NFA to DFA """
char = "-"
index = 0
states = [nfa.start]
while True:
if trace > 0: print " Tape:", tape[index:], " States:", states
if index == len(tape): # End of input reached
successtates = [s for s in states
if s in nfa.finals]
# If this is nonempty return True, otherwise False.
return len(successtates)> 0
elif len(states) == 0:
# Not reached end of string, but no states.
return False
elif char is tape[index]:
# the add on method to take in abreviations by sign: -
else:
# Calculate the new states.
states = set([e[2] for e in nfa.edges
if e[0] in states and
tape[index] == e[1]
])
# Move one step in the string
index += 1
我需要添加一个将缩写词纳入帐户的方法。我不太确定如何从一个州跳到另一个州。这是 NFA 类中的内容:
def __init__(self,start=None, finals=None, edges=None):
"""Read in an automaton from python shell"""
self.start = start
self.edges = edges
self.finals = finals
self.abrs = {}
我坚持使用 abrs,但是在尝试定义我自己的 abrs 时,我经常遇到错误,例如
nfa = NFA(
start = 0,
finals = [6],
abrs = {0:4, 2:5},
edges=[
(0,'h', 1),
(1,'a', 2),
(2,'z', 3),
(3,'a', 4),
(4,'r', 5),
(5,'d', 6)
])
我收到错误“TypeError: init () got an unexpected keyword argument 'abrs'” 为什么我收到该错误?
对于修改,我认为我会做这样的事情
elif char is tape[index]:
#get the next char in tape tape[index+1] so
#for loop this.char with abrs states and then continue from that point.
明智的选择还是更好的解决方案?