我正在尝试实现一种识别单词的方法。我已经编写了以下代码,并尝试在纸上遵循我的代码,并使用示例输入逐步执行它,但是我找不到我的代码没有按照我希望他做的那样的原因。有人看到这个缺陷吗?我看不到它,我对为什么它不起作用感到困惑。
from collections import defaultdict
class NFA:
def __init__(self, initial, trns, final):
self.initial = initial
self.final = set(final)
self.trns = defaultdict(set)
for (src, char, tgt) in trns:
self.trns[src, char].add(tgt)
def recognizewords(self, strng):
strang = [char for char in strng]
strang.reverse()
visited = set()
agenda = [self.initial]
while strang and agenda:
currentletter = strang.pop()
current = agenda.pop()
visited.add(current)
if (current, currentletter) in self.trns.keys():
state = self.trns[(current, currentletter)]
for st in state:
if strang == [] and state in self.final:
return True
for i in self.trns[(current, currentletter)]:
agenda.append(i)
return False
exampleO = NFA(0, [(0,'o',1), (1,'k',2), (2,'i',1), (2,'!',3)], [3])
print(exampleO.recognizewords("ok!"))
它应该返回True,因为在某一时刻我的列表“ strang”将为空(当我将currentletter分配给“!”时),同时3在self.final中,因为self.final为[3]对于我的对象示例O ....
答案 0 :(得分:0)
这并不是代码的固定解决方案,因为使用递归而不是显式堆栈时,这种类型问题的解决方案更容易可视化(至少对我而言)。正如我在评论中提到的那样,NFA实际上允许在给定的输入字符(尤其是空字符串)上转换为多个状态。因此,我修改了输入规范,以允许为每个转换指定新状态列表。在这里,状态0在空字符串上转换为状态1或状态5(可识别OK
)。状态2可以在k
上转换为状态3或4。
from collections import defaultdict
class NFA:
def __init__(self, initial, trns, final):
self.initial = initial
self.final = set(final)
self.trns = defaultdict(set)
self.epsilon_states = set()
for (src, char, tgt) in trns:
if char == '':
self.epsilon_states.add(src)
for state in tgt:
self.trns[src, char].add(state)
def recognize_next_char(self, state, strng, index):
ch = '' if state in self.epsilon_states else strng[index]
if (state, ch) in self.trns.keys():
next_states = self.trns[(state, ch)]
if ch != '':
index += 1
for next_state in next_states:
if index == len(strng):
if next_state in self.final:
return True
elif self.recognize_next_char(next_state, strng, index):
return True
return False
else:
return False
def recognizewords(self, strng):
if len(strng) == 0:
if self.initial in self.final:
return True
if self.initial not in self.epsilon_states:
return false
return self.recognize_next_char(self.initial, strng, 0)
exampleO = NFA(0, [(0,'',(1,5)), (1,'o',(2,)), (2,'k',(3,4)), (3,'i',(2,)), (4,'!',(99,)), (5,'O', (6,)), (6,'K',(99,))], [99])
print(exampleO.recognizewords("okikik!"))
print(exampleO.recognizewords("ok!"))
print(exampleO.recognizewords("OK"))
print(exampleO.recognizewords("ok"))
print(exampleO.recognizewords("oki"))
print(exampleO.recognizewords("okx"))
打印:
True
True
True
False
False
False
使用Epsilon转换识别ab(cd|ef)gh
的示例
答案 1 :(得分:0)
因此,事实证明,非递归解决方案比您正确执行回溯操作要复杂一些(它需要更多堆栈)。我更改了一些变量名,对我来说,这更合逻辑。下面有两个版本。第二个修改了第一个,仅作了一些细微的更改以支持对空字符串的转换:
from collections import defaultdict
class NFA:
def __init__(self, initial, trns, final):
self.initial = initial
self.final = set(final)
self.trns = defaultdict(set)
for (src, char, tgt) in trns:
self.trns[src, char].add(tgt)
def recognizewords(self, strng):
strlen = len(strng)
if strlen == 0:
return self.initial in self.final
index = 0
next_states = [self.initial]
next_states_stack = []
index_stack = []
while index < strlen:
current_letter = strng[index]
if next_states:
state = next_states.pop()
if (state, current_letter) in self.trns.keys():
new_next_states = self.trns[(state, current_letter)]
if new_next_states & self.final:
# did we use up all the characters?
return index == strlen - 1
next_states_stack.append(next_states)
index_stack.append(index)
next_states = list(new_next_states)
index += 1
elif next_states_stack:
next_states = next_states_stack.pop()
index = index_stack.pop()
else:
return False
return False
# ab(d|e)
exampleO = NFA(0, [(0,'a',1), (1,'b',2), (1,'b',3), (2,'d',4), (3,'e',4)], [4])
print(exampleO.recognizewords("abd"))
print(exampleO.recognizewords("abe"))
打印:
True
True
支持空字符串转换的变体
from collections import defaultdict
class NFA_Epsilon:
def __init__(self, initial, trns, final):
self.initial = initial
self.final = set(final)
self.trns = defaultdict(set)
self.epsilon_states = set()
for (src, char, tgt) in trns:
if char == '':
self.epsilon_states.add(src)
self.trns[src, char].add(tgt)
def recognizewords(self, strng):
strlen = len(strng)
if strlen == 0:
return self.initial in self.final
index = 0
next_states = [self.initial]
next_states_stack = []
index_stack = []
while index < strlen:
if next_states:
state = next_states.pop()
current_letter = '' if state in self.epsilon_states else strng[index]
if (state, current_letter) in self.trns.keys():
new_next_states = self.trns[(state, current_letter)]
if new_next_states & self.final:
# did we use up all the characters?
return index == strlen - 1
next_states_stack.append(next_states)
index_stack.append(index)
next_states = list(new_next_states)
if current_letter != '':
index += 1
elif next_states_stack:
next_states = next_states_stack.pop()
index = index_stack.pop()
else:
return False
return False
# ab(cd|ef)gh
example1 = NFA_Epsilon(0, [
(0,'a',1),
(1,'b',2),
(2,'',3),
(2,'',6),
(3,'c',4),
(4,'d',5),
(5,'',9),
(6,'e',7),
(7,'f',8),
(8,'',9),
(9,'g',10),
(10,'h',11)
],[11])
print(example1.recognizewords('abcdgh'))
print(example1.recognizewords('abefgh'))
打印:
True
True