This commit is contained in:
deadsmond 2019-11-25 21:29:16 +01:00
parent a43a406bf3
commit 662d6e8780

View File

@ -7,69 +7,52 @@ class automata:
# class variables init # class variables init
def __init__(self): def __init__(self):
# dictionary of connections between nodes # dictionary of connections between nodes
self.graph = {} self.storage = {}
# list of accepting states # list of accepting states
self.accepting_states = [] self.accepting_states = []
# list of current states # list of current states
self.state = ['0'] self.state = ['0']
# word variable
self.word = ''
# print for debug purposes # print for debug purposes
def __repr__(self): def __repr__(self):
return('%s\n\n%s\n\n%s\n\n' % (self.graph, self.accepting_states, self.state)) return('%s\n\n%s\n\n%s\n\n' % (self.storage, self.accepting_states, self.state))
# add node in open fst format # add node in open fst format
def add_node(self, line): def add_node(self, line):
node = line.replace('\n', '').split(' ') node = line.replace('\n', '').split(' ')
if len(node) == 3: if len(node) == 3:
if node[0] in self.graph: if node[0] in self.storage:
# add value to existing node # add value to existing node
self.graph[node[0]].append({node[2]: node[1]}) self.storage[node[0]].append({node[2]: node[1]})
else: else:
# create new node # create new node
self.graph[node[0]] = [{node[2]: node[1]}] self.storage[node[0]] = [{node[2]: node[1]}]
elif len(node) == 1: elif len(node) == 1:
# add accepting state # add accepting state
self.accepting_states.append(node[0]) self.accepting_states.append(node[0])
# check if string is accepted by automate # check if string is accepted by automate
def test_string(self, word): def test_string(self, text):
self.state = ['0'] self.state = ['0']
self.word = word.replace('\n', '') text = text.replace('\n', '')
# for all values in word for i in text:
for i in self.word:
# for all actual states
result = []
for q in self.state: for q in self.state:
# move state to its transition self.state = self.get_node_transition(q, i)
result = self.get_node_transition(q, i)
# if the list is empty, return false
if not self.state: if not self.state:
return False return False
# flatten list of states
self.state = [item for sublist in result for item in sublist]
# check if automata is in accepting state
return self.check_if_accepted() return self.check_if_accepted()
# check if there is common part between states of automata and accepting states
def check_if_accepted(self): def check_if_accepted(self):
return not set(self.state).isdisjoint(self.accepting_states) return not set(self.state).isdisjoint(self.accepting_states)
def get_node_transition(self, q, i): def get_node_transition(self, query):
result = [] for i in self.storage:
# if the node exists if i[0] == query:
try: return i[1]
# search through all its connections to find value return []
for connections in self.graph[q]:
for value in connections: def sort(self):
if value == i: self.storage = sorted(self.storage, key = lambda x: (x[0][0], x[0][1]))
# append next node
result.append(connections[value])
except KeyError:
return result
# return list of next nodes
return result
auto = automata() auto = automata()
@ -80,7 +63,4 @@ for line in sys.stdin:
f = open(sys.argv[1], 'r') f = open(sys.argv[1], 'r')
for line in f: for line in f:
if auto.test_string(line): print(auto.test_string(line))
print("TRUE %s" % auto.word)
else:
print("FALSE %s" % auto.word)