このnfaで略語を使用して、ある状態から別の状態に移行しようとしています。
NFAは次のように定義されます
nfa = NFA(
start = 0,
finals = [6],
abrs = {1:5, 3:5},
edges=[
(0,'h', 1),
(1,'a', 2),
(2,'z', 3),
(3,'a', 4),
(4,'r', 5),
(5,'d', 6)
])
文字列を処理するときにこの内部メソッドを作成しました。
char = ":"
elif char is tape[index]:
print " abreviation found!!"
#goto to state
gotoState = set([e[2] for e in nfa.abrs
if e[0] in nfa.edges and
tape[index+1] == e[1]
])
index += 1
test print =>を試してみるとprint nrec("h:d", nfa, 1)
falseが返されます。これは、状態を作成していないということです。-signが表示されたら、これらすべての状態を与える必要がある:
例からの状態を作成したい=> 、このタスクを実行するためにこのメソッドを変更するにはどうすればよいですか?これは、構造を作成した後に文字列を処理する定義済みのメソッドです。("h:z")
('h'->'a'->'z'->'a'->'r'->'d')
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]:
print " abreviation found!!"
#skip to state
gotoState = set([e[2] for e in nfa.abrs
if e[0] in nfa.edges and
tape[index+1] == e[1]
])
index += 1
# 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
省略形を正しく機能させるにはどうすればよいですか?それはtrueを返しますか?