-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmealy_trie.py
206 lines (150 loc) · 6.84 KB
/
mealy_trie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from copy import deepcopy
#from utils import merging_checking
class TrieTransition:
"""A transition in the trie structure"""
def __init__(self, src, tgt, input=None, output=None, id=-1):
self.src = src
self.tgt = tgt
self._input = input
self._output = output
self._id = id
def getCharOutput(self):
return self._output
def getStateOutput(self):
return self.tgt
def serialize(self):
return (self.src, self._input, self._output, self.tgt)
class TrieNode:
"""A node in the trie structure"""
def __init__(self, id, char="", label=None):
self.id = id
self._outTr = {}
self._inTr = {}
self.children = {}
self.char = char
self.label = label
def addOutTr(self, transition:TrieTransition) :
if not(transition.getInput() in self._outTr.keys()) :
self._outTr[transition.getInput()] = {}
if not(transition.getOutput() in self._outTr[transition.getInput()].keys()) :
self._outTr[transition.getInput()][transition.getOutput()] = []
self._outTr[transition.getInput()][transition.getOutput()].append(transition)
def addInTr(self, transition:TrieTransition) :
if not(transition.getInput() in self._inTr.keys()) :
self._inTr[transition.getInput()] = {}
if not(transition.getOutput() in self._inTr[transition.getInput()].keys()) :
self._inTr[transition.getInput()][transition.getOutput()] = []
self._inTr[transition.getInput()][transition.getOutput()].append(transition)
class Trie(object):
def __init__(self, corpus, labels):
"""
The trie has at least the root node.
The root node does not store any character
"""
self.root = TrieNode(0)
self.state_count = 0
self.transition_count= 0
self.transitions = []
self.inputVocabulary = []
self.outputVocabulary = []
self.arcs = []
self.states = {0: self.root} # The initial state is the state 0
# self.states = {0: node0, 1: node1, ...}
# self.transitions = {transition0, transition1, ...}
for i , (word, label) in enumerate(zip(corpus, labels)):
self.insert(word, label, i)
print(f'\nThe number of states of the Trie: {len(self.states)}\n')
self.dfs(self.root)
self.print()
#self.states = [[i, False] for i in range(self.count + 1)]
#self.dfs(self.root)
def insert(self, word, label, i=0):
"""Insert a word into the trie"""
node = self.root
# Loop through each character in the word
# Check if there is no child containing the character, create a new child for the current node
for i, char in enumerate(word):
new_transition = False
if char not in self.inputVocabulary:
self.inputVocabulary.append(char)
if label[i] not in self.outputVocabulary:
self.outputVocabulary.append(label[i])
"""
if char in node.children:
node = node.children[char]
else:
self.count += 1
new_node = TrieNode(self.count, char)
node.children[char] = new_node
node = new_node
"""
if char not in node._outTr.keys():
node._outTr[char] = {}
new_transition = True
if label[i] not in node._outTr[char].keys():
self.state_count += 1
new_node = TrieNode(self.state_count, char, label[i])
node._outTr[char][label[i]] = []
new_node._inTr[char] = {}
#new_node._inTr[char][label[i]] = [node.id]
#node._outTr[char][label[i]].append(new_node.id)
node.children[char] = (label[i], new_node.id)
self.states[new_node.id] = new_node
else:
new_node = self.states[node.children[char][1]]
if new_transition:
transition = TrieTransition(node, new_node, char, label[i], self.transition_count)
self.transitions.append(transition)
self.transition_count += 1
node = new_node
# Store all trie transition in our Tree
def dfs(self, node):
"""Depth-first transversal of the trie"""
for i in node.children.keys():
self.arcs.append((node.id, self.states[node.children[i][1]].char, self.states[node.children[i][1]].label, node.children[i][1]))
self.dfs(self.states[node.children[i][1]])
# Print the details of our Tree
def print(self):
print("\n********************* Prefix Tree of Mealy Machine corpus **********************\n\n")
print(f'Number of states: {self.state_count + 1}')
print(f'Number of transitions: {self.transition_count}')
print(f'Initial state: {self.root.id}')
print(f'Input vocabulary: {str(self.inputVocabulary)}')
print(f'Output vocabulary: {str(self.outputVocabulary)}\n')
"""print("Different states of the Tree: ")
for i in list(self.states.keys()):
print(f'ID: {self.states[i].id}')"""
if len(self.transitions) <= 10:
print(f"\nFirst {len(self.transitions)} transitions of the Tree: ")
for transition in self.transitions:
print(f'-> {transition.src.id} --> {transition._input}/{transition._output} --> {transition.tgt.id}')
else:
print(f"\nFirst 10 over {len(self.transitions)} transitions of the Tree: ")
for i, transition in enumerate(self.transitions):
print(f'-> {transition.src.id} --> {transition._input}/{transition._output} --> {transition.tgt.id}')
if i == 9:
break
print("\n********************************************************************************\n\n")
"""def merging_state(self, k):
# k is the similarity treshold
for i in len(self.states.keys()):
for j in len(self.states.keys()):
if i == j:
continue
merge = merging_checking(self.states[i], self.states[j], k) # check if the two states are mergable
if not merge:
continue"""
def return_states(self, word):
node = self.root
trace = [node]
for char in word:
node = self.states[node.children[char][1]]
trace.append(node)
return trace
def produceOutput(self, input):
node = self.root
output = []
for char in input:
output.append(self.states[node.children[char][0]])
node = self.states[node.children[char][1]]
return output