-
Notifications
You must be signed in to change notification settings - Fork 6
/
t.py
177 lines (117 loc) · 3.79 KB
/
t.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
print("here")
import mfst
class MyWeight(mfst.AbstractSemiringWeight):
def __init__(self, v=0):
super().__init__()
print('create', v)
self._value = v
type(self).num_weights += 1
def __add__(self, other):
print('hi from add')
return MyWeight(self._value + other._value)
def __mul__(self, other):
print('hi from mul')
return MyWeight(self._value * other._value)
def __eq__(self, other):
print('hi from equal')
return self._value == other._value
def __hash__(self, other):
print('hi from hash')
return hash(self._value)
def approx_eq(self, other, delta):
print('hi from approx eq')
return self == other
def __del__(self):
print('del', type(self), self._value)
type(self).num_weights -= 1
def __str__(self):
print('calling string')#, type(self), dir(self))
return 'MyWeight({})'.format(self._value)
MyWeight.num_weights = 0
MyWeight.zero = MyWeight(0)
MyWeight.one = MyWeight(1)
gg = mfst.FST(MyWeight)
for i in range(9):
gg.add_state()
for i in range(5):
gg.add_arc(i, i+1, weight=MyWeight(i))
gg.initial_state = 0
gg.set_final_weight(3, MyWeight(1))
print("before final")
vv = gg.get_final_weight(3)
print(type(vv), dir(vv))
print(str(vv))
print(gg)
print("after")
# try the isomorphics to itself
assert gg.isomorphic(gg)
for a in gg.get_arcs(3):
print(a)
a = None
vv = None
gg = None
assert MyWeight.num_weights == 2 # check that everything got deleted (except the zero and 1 static)
gg = mfst.FST()
sf = gg.create_from_string('test')
print(sf)
assert sf.get_unique_output_string() == 'test'
assert sf._repr_html_() is not None
print(sf._repr_html_())
def build_hc_fst(fst, n=3):
start = fst.add_state()
fst.initial_state = start
h = [fst.add_state() for i in range(n)]
c = [fst.add_state() for i in range(n)]
fst.add_arc(start, h[0], input_label='H')
fst.add_arc(start, c[0], input_label='C')
for i in range(n - 1):
fst.add_arc(h[i], h[i+1], input_label='H')
fst.add_arc(c[i], c[i+1], input_label='C', weight=fst.semiring(2))
fst.add_arc(h[i], c[i+1], input_label='C')
fst.add_arc(c[i], h[i+1], input_label='H')
final = fst.add_state()
fst.set_final_weight(final, 1)
fst.add_arc(c[-1], final)
fst.add_arc(h[-1], final)
class pathSemiring2(mfst.PythonValueSemiringWeight):
semiring_properties = 'path'
pathSemiring2.zero = pathSemiring2(0)
pathSemiring2.one = pathSemiring2(1)
pathSemiring2.one.zzz = 1
pathSemiring2.zero.zzz = 1
hc = mfst.FST(pathSemiring2)
build_hc_fst(hc)
print(hc.determinize())
hc.random_path()
fst = mfst.FST()
f1 = fst.create_from_string('hello')
f2 = fst.create_from_string('world')
g = f1.union(f2)
print(g.full_str())
g._repr_html_()
fst = mfst.FST(acceptor=True)
f1 = fst.create_from_string('hello')
f2 = fst.create_from_string('world')
g = f1.intersect(f2)
print(g.full_str())
g._repr_html_()
fst.random_path()
f1.num_arcs(0)
assert hc.shortest_path().num_states > 0
# check that we can use the other semirings that are defined in
from mfst import semirings
for s in dir(semirings):
if s.startswith('_') or s == 'AbstractSemiringWeight' or s == 'BooleanSemiringWeight':
continue
ss = getattr(semirings, s)
fst = mfst.FST(ss)
build_hc_fst(fst)
fst.determinize() # just do some operation to check that the semiring seems ok
# tests for the boolean semiring in that we can compose it with something else
bs = mfst.FST(semirings.BooleanSemiringWeight).create_from_string('test123').push()
assert mfst.FST().create_from_string('test123').compose(bs).get_unique_output_string() == 'test123'
mfst.FST('test')
import pickle
bs2 = pickle.loads(pickle.dumps(bs))
assert bs.isomorphic(bs2)
print('done')