-
Notifications
You must be signed in to change notification settings - Fork 1
/
so2dla.py
168 lines (113 loc) · 3.5 KB
/
so2dla.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
"""A simplex OSL_2 version of the SI2LDA of Hua & Jardine (2021).
This is for testing only and not verifiably correct in any way.
Written by Adam Jardine.
"""
from fst_object import *
from ostia import *
from helper import *
from features import *
# Helper functions
def suff_1(w):
"""Returns 1-suffix of w"""
if w == "":
return ""
else:
return w[-1]
def get_OS(T,q):
"""Gets output 1-suffixes of state q in FST T"""
incoming = { tr for tr in T.E if tr[3] == q}
outs = { tr[2] for tr in incoming}
suffs = { suff_1(w) for w in outs}
return suffs
def lncat(w,v):
"""Returns wv^-1; i.e. v removed from the end of w"""
if v == "":
return w
elif w.endswith(v):
return w[0:-len(v)]
# Main algorithm
def so2dla_ex(D,Rho,Sigma):
"""An attempt at a Simplex Output Strictly 2-Local Decomposition Learning Algorithm"""
print("Learning from "+str(D)+"\n")
T_f = ostia(D,Rho,Sigma)
print("Initial hypothesis for T_f:")
print(" Q:\t"+str(T_f.Q))
print(" E:\t"+str(T_f.E))
print(" q0:\t"+str(T_f.qe))
print(" stout:\t"+str(T_f.stout)+"\n")
q1 = T_f.Q[0]
q2 = T_f.Q[1]
OS = { q1 : get_OS(T_f,q1) | {""},
q2 : get_OS(T_f,q2)
}
print("OSs for T_f:\t"+str(OS))
#*** construct_T_g
Q_g = ["qe","qd"]
corr = {}
rroc = {}
if len(OS[q1]) < len(OS[q2]):
corr["qe"] = q1
corr["qd"] = q2
rroc[q1] = "qe"
rroc[q2] = "qd"
else:
corr["qe"] = q2
corr["qd"] = q1
rroc[q2] = "qe"
rroc[q1] = "qd"
print("corr:\t\t"+str(corr))
o_g = {}
for s in Sigma:
o_g[("qd",s)] = s
d_trs = [ tr for tr in T_f.E if tr[0] == corr["qd"] and tr[2][0] == s ]
found = False
for t in d_trs:
w_trs = [ tr for tr in T_f.E if tr[0] == corr["qe"] and tr[1] == t[1]]
if w_trs != []:
found = True
w_d = t[2]
w_e = w_trs[0][2]
print("w_d for "+s+":\t"+w_d)
print("w_e for "+s+":\t"+w_e+"\n")
w_s = lncat(w_e,w_d[1:])
o_g[("qe",s)] = w_s
if s != w_s: #This is lns 2-3 from Alg 3
tau = s
w_tau = w_s
if found == False:
o_g[("qe",s)] = s
d_g = {}
for q in Q_g:
for s in Sigma:
for r in Q_g:
if suff_1(o_g[(q,s)]) in OS[corr[r]]:
d_g[(q,s)] = r
# print("d_g:\t"+str(d_g))
# Translate transitions into fst_object FST format
E_g = []
for (q,s) in d_g.keys():
E_g.append((q,s,o_g[(q,s)],d_g[(q,s)]))
# print("E_g:\t"+str(E_g))
T_g = FST(Rho,Sigma)
T_g.Q = Q_g
T_g.E = E_g
T_g.qe = rroc[q1]
T_g.stout = { "qe" : T_f.stout[corr["qe"]], "qd" : T_f.stout[corr["qd"]] }
print("Hypothesis for T_g:")
print(" Q:\t"+str(T_g.Q))
print(" E:\t"+str(T_g.E))
print(" q0:\t"+str(T_g.qe))
print(" stout:\t"+str(T_g.stout)+"\n")
#*** modify_T_f
T_f.E = [ d for d in T_f.E if not d[0]==corr["qe"]]
print("E_f after deletions: "+str(T_f.E)+"\n")
print("Merging...\n")
T_f.Q = [corr["qd"]]
T_f.qe = corr["qd"]
T_f.stout = {corr["qd"]:T_f.stout[corr["qe"]]}
print("Final hypothesis for T_f:")
print(" Q:\t"+str(T_f.Q))
print(" E:\t"+str(T_f.E))
print(" q0:\t"+str(T_f.qe))
print(" stout:\t"+str(T_f.stout)+"\n")
return (T_f,T_g)