我正在尝试创建一种方法,因为疏忽从一个点跳到另一个点。
我创建了一个具有当前边缘的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 ()得到了一个意外的关键字参数'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.
明智的选择还是更好的解决方案?
答案 0 :(得分:0)
错误是由__init__
不接受定义的abrs
关键字参数引起的。
def __init__(self,start=None, finals=None, edges=None):
您需要abrs=None
(或其他值)才能使其成为关键字参数或abrs
才能使其成为必需参数。