我正在构建一个有限状态接受器(FSA ......基本上是一棵树)。我有一个FSA
班级和一个State
班级,FSA
应该由许多States
. 但是,当FSA
最初创建 an 时,我想创建它的开头State
和结尾State
,并将它们连接起来。这是代码:
class State (object):
__slots__ = "chars","children"
def __init__(self,chars,children=[]):
self.chars = chars
self.children = children
class FSA (object):
__slots__ = "vocab"
def __init__(self,vocab):
self.vocab = vocab
self.start = State("0")
self.finish = State("1")
self.start.children.append(self.finish)
self.finish.children.append(self.start)
但是,当我运行时fsa = FSA("vocab.small")
,这会引发错误:
AttributeError: 'FSA' object has no attribute 'start'
有没有办法可以启动FSA
并States
为它创建两个?