我试图创建一个方法,从一个点跳到另一个点。
我创建了一个具有当前边缘的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()得到了一个意外的关键字参数‘abr’“为什么我会收到该错误?
对于我想做的修改,我会做这样的事情
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.明智的选择还是更好的解决方案?
发布于 2013-02-16 21:32:42
错误是由__init__不接受定义的abrs关键字参数引起的。
def __init__(self,start=None, finals=None, edges=None):
您需要abrs=None (或其他值)使其成为关键字参数,或abrs使其成为必需的参数。
https://stackoverflow.com/questions/14915304
复制相似问题