'hm'
This commit is contained in:
parent
662d6e8780
commit
4f253b4866
40
TaskB01/run
40
TaskB01/run
@ -4,55 +4,39 @@ import re
|
|||||||
|
|
||||||
|
|
||||||
class automata:
|
class automata:
|
||||||
# class variables init
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# dictionary of connections between nodes
|
self.storage = []
|
||||||
self.storage = {}
|
|
||||||
# list of accepting states
|
|
||||||
self.accepting_states = []
|
self.accepting_states = []
|
||||||
# list of current states
|
self.state = '0'
|
||||||
self.state = ['0']
|
|
||||||
|
|
||||||
# print for debug purposes
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return('%s\n\n%s\n\n%s\n\n' % (self.storage, 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
|
|
||||||
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.storage:
|
self.storage.append([[node[0], node[2]], node[1]])
|
||||||
# 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]}]
|
|
||||||
elif len(node) == 1:
|
elif len(node) == 1:
|
||||||
# add accepting state
|
|
||||||
self.accepting_states.append(node[0])
|
self.accepting_states.append(node[0])
|
||||||
|
|
||||||
# check if string is accepted by automate
|
|
||||||
def test_string(self, text):
|
def test_string(self, text):
|
||||||
self.state = ['0']
|
self.state = '0'
|
||||||
text = text.replace('\n', '')
|
text = text.replace('\n', '')
|
||||||
for i in text:
|
for i in text:
|
||||||
for q in self.state:
|
self.state = self.get_node_transition([self.state, i])
|
||||||
self.state = self.get_node_transition(q, i)
|
if self.state == None:
|
||||||
if not self.state:
|
return 'FALSE %s' % text
|
||||||
return False
|
|
||||||
return self.check_if_accepted()
|
|
||||||
|
|
||||||
def check_if_accepted(self):
|
if self.state in self.accepting_states:
|
||||||
return not set(self.state).isdisjoint(self.accepting_states)
|
return 'TRUE %s' % text
|
||||||
|
else:
|
||||||
|
return 'FALSE %s' % text
|
||||||
|
|
||||||
def get_node_transition(self, query):
|
def get_node_transition(self, query):
|
||||||
for i in self.storage:
|
for i in self.storage:
|
||||||
if i[0] == query:
|
if i[0] == query:
|
||||||
return i[1]
|
return i[1]
|
||||||
return []
|
return None
|
||||||
|
|
||||||
def sort(self):
|
|
||||||
self.storage = sorted(self.storage, key = lambda x: (x[0][0], x[0][1]))
|
|
||||||
|
|
||||||
|
|
||||||
auto = automata()
|
auto = automata()
|
||||||
|
Loading…
Reference in New Issue
Block a user