-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebut.py
More file actions
181 lines (133 loc) · 4.99 KB
/
debut.py
File metadata and controls
181 lines (133 loc) · 4.99 KB
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
import pandas as pd
# import numpy as np
null_transition = '-1'
phi_transition = 'phi'
automate = {
'alphabet': ['a','b'],
'states' : ['0','1','2','3'],
'initial_state' : '0',
'final_states' : ['2','3'],
'transitions': [
['0','1','b'],
['0','3','a'],
['1','1','a'],
['1','2','b'],
['3','3','a']
]
}
df_auto = pd.DataFrame(index = ['0','1','2','3'], data = {
'a': ['3','1',null_transition,'3'],
'b' : ['1','2',null_transition,null_transition],
'initial_state': [True,False,False,False],
'final_states':[False,False,True,True]
})
print(df_auto)
def dict_to_table(dico) :
states = dico['states']
df = pd.DataFrame(index= states)
for letter in dico['alphabet'] :
values = [null_transition for _ in range(len(states))]
for transition in dico['transitions'] :
start_position = -1
end_position = null_transition
if transition[2] == letter :
k = 0 #hashtable maybe
while(start_position == -1 or end_position == null_transition) :
temp = states[k]
if transition[1] == temp :
end_position = temp
if transition[0] == temp :
start_position = k
k+=1
values[start_position] = end_position
df[f'{letter}'] = values
df['initial_state'] = False
df['final_states'] = False
for i in range(len(states)) :
if dico['initial_state'] == df.index[i] :
df.loc[states[i],'initial_state'] = True
final_states = dico['final_states'].copy()
k = 0
# print(df)
while len(final_states) != 0 :
if final_states[0] == df.index[k] :
df.loc[states[k],'final_states'] = True
k=0
final_states.pop(0)
k+=1
return df
def table_to_dict(df) :
transition = []
for col in list(df.columns[:-2]) :
for row in list(df.index) :
if df[str(col)].loc[str(row)] != null_transition:
transition.append([row,df[str(col)].loc[row],col])
dict = {
'alphabet': list(df.columns[:-2]),
'states' : list(df.index),
'initial_state' : list(df[df.initial_state == True].index)[0],
'final_states' : list(df[df.final_states == True].index),
'transitions': sorted(transition)}
return dict
# print(dict_to_table(automate))
# print(table_to_dict(df_auto))
# print(table_to_dict(dict_to_table(automate)))
# print(table_to_dict(dict_to_table(automate)) == automate)
# df_test = dict_to_table(automate)
#print(df_test)
# print(list(df_test[df_test.final == True].index))
#print(table_to_dict(df_test))
# print(automate['transitions'] == sorted(automate['transitions']))
# print(sorted(automate['transitions']))
def save_automaton(automaton,file_path='Untitled.txt') :
with open(file_path,'w') as file :
file.write(str(automaton))
#save_automaton(automate,'test.txt')
def import_automaton(file_path) :
with open(file_path,'r') as file :
automaton = eval(file.read())
return automaton
#auto2 = import_automaton('test.txt')
# print(dict_to_table(auto2))
def is_word_recognized(automaton,word):
df = get_good_type(automaton,'dataFrame')
current_state = list(df[df.initial_state == True].index)[0]
w = word
while len(w) !=0 :
if df[w[0]].loc[str(current_state)] == null_transition :
return False
current_state = df[w[0]].loc[str(current_state)]
w = w[1:]
return df.final_states.loc[str(current_state)]
#print(is_word_recognized(automate,'aaaaa'))
# print(df_auto.final_states.loc[str(3)])}))
def is_complete(automaton) :
df = get_good_type(automaton,'dataFrame')
return not df[list(df_auto.columns)[:-2]].isin([null_transition]).any().any()
#print(df_auto[list(df_auto.columns)[:-2]])
#print(is_complete(automate))
def get_good_type(automaton,wanted_type) :
assert wanted_type in ['dict','dataFrame'] and type(automaton) in [dict, pd.DataFrame]
if wanted_type == 'dataFrame' :
if type(automaton) == dict :
return dict_to_table(automaton)
return automaton
if type(automaton) == dict :
return automaton
return table_to_dict(automaton)
#print(get_good_type(df_auto,'dict'))
def completing(automaton) :
df = get_good_type(automaton,'dataFrame')
data_phi = {}
for letter in df.columns :
data_phi[letter] = phi_transition
data_phi['initial_state'], data_phi['final_states'] = False, False
phi_state = pd.DataFrame( index=['phi'], data = data_phi)
df = pd.concat([df, phi_state])
df.replace(null_transition,phi_transition, inplace= True)
return df
# print([phi_transition for _ in range(len(df_auto.columns)-2)])
# print(range(len(df_auto.columns)-2))
# print(completing(automate))
#back else :
# raise TypeError(f'automaton_specified wrong type : dict or dataframe expected, got {type(automaton)}')