2019-11-24 21:22:07 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
2019-11-24 21:27:57 +01:00
|
|
|
class automata:
|
2019-11-25 16:39:57 +01:00
|
|
|
# class variables init
|
2019-11-24 21:27:57 +01:00
|
|
|
def __init__(self):
|
2019-11-25 16:39:57 +01:00
|
|
|
# dictionary of connections between nodes
|
|
|
|
self.storage = {}
|
|
|
|
# list of accepting states
|
2019-11-24 21:27:57 +01:00
|
|
|
self.accepting_states = []
|
2019-11-25 16:39:57 +01:00
|
|
|
# list of current states
|
|
|
|
self.state = ['0']
|
|
|
|
|
|
|
|
# print for debug purposes
|
2019-11-24 21:49:36 +01:00
|
|
|
def __repr__(self):
|
2019-11-24 21:50:57 +01:00
|
|
|
return('%s\n\n%s\n\n%s\n\n' % (self.storage, self.accepting_states, self.state))
|
2019-11-25 16:39:57 +01:00
|
|
|
|
|
|
|
# add node in open fst format
|
2019-11-24 21:22:07 +01:00
|
|
|
def add_node(self, line):
|
2019-11-24 21:52:39 +01:00
|
|
|
node = line.replace('\n', '').split(' ')
|
2019-11-24 21:22:07 +01:00
|
|
|
if len(node) == 3:
|
2019-11-25 16:39:57 +01:00
|
|
|
if node[0] in self.storage:
|
|
|
|
# add value to existing node
|
|
|
|
self.storage[node[0]].append({node[2]: node[1]})
|
|
|
|
else:
|
|
|
|
# create new node
|
|
|
|
self.storage[node[0]] = [{node[2]: node[1]}]
|
2019-11-24 21:22:07 +01:00
|
|
|
elif len(node) == 1:
|
2019-11-25 16:39:57 +01:00
|
|
|
# add accepting state
|
2019-11-24 21:55:19 +01:00
|
|
|
self.accepting_states.append(node[0])
|
2019-11-24 21:22:07 +01:00
|
|
|
|
2019-11-25 16:39:57 +01:00
|
|
|
# check if string is accepted by automate
|
2019-11-24 21:22:07 +01:00
|
|
|
def test_string(self, text):
|
2019-11-25 16:39:57 +01:00
|
|
|
self.state = ['0']
|
2019-11-24 22:41:40 +01:00
|
|
|
text = text.replace('\n', '')
|
2019-11-25 19:10:41 +01:00
|
|
|
# for all values in text
|
2019-11-24 21:22:07 +01:00
|
|
|
for i in text:
|
2019-11-25 19:10:41 +01:00
|
|
|
# for all actual states
|
2019-11-25 16:39:57 +01:00
|
|
|
for q in self.state:
|
2019-11-25 19:10:41 +01:00
|
|
|
# move state to its transition
|
|
|
|
q = self.get_node_transition(q, i)
|
2019-11-25 16:39:57 +01:00
|
|
|
if not self.state:
|
|
|
|
return False
|
|
|
|
return self.check_if_accepted()
|
2019-11-24 21:27:57 +01:00
|
|
|
|
2019-11-25 19:10:41 +01:00
|
|
|
# check if there is common part between states of automata and accepting states
|
2019-11-25 16:39:57 +01:00
|
|
|
def check_if_accepted(self):
|
|
|
|
return not set(self.state).isdisjoint(self.accepting_states)
|
2019-11-24 21:22:07 +01:00
|
|
|
|
2019-11-25 19:05:16 +01:00
|
|
|
def get_node_transition(self, q, i):
|
2019-11-25 19:15:35 +01:00
|
|
|
return [] if not self.storage[q][i] else self.storage[q][i]
|
2019-11-24 21:22:07 +01:00
|
|
|
|
|
|
|
|
2019-11-24 21:27:57 +01:00
|
|
|
auto = automata()
|
2019-11-24 21:22:07 +01:00
|
|
|
|
|
|
|
for line in sys.stdin:
|
|
|
|
auto.add_node(line)
|
|
|
|
|
2019-11-25 16:39:57 +01:00
|
|
|
print(auto)
|
|
|
|
|
2019-11-25 19:16:19 +01:00
|
|
|
f = open(sys.argv[1], 'r')
|
2019-11-24 21:22:07 +01:00
|
|
|
|
2019-11-25 19:27:29 +01:00
|
|
|
# for line in f:
|
|
|
|
# print(auto.test_string(line))
|