forked from koskenni/pytwolc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwbt.py
196 lines (175 loc) · 6.26 KB
/
twbt.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
"""Module for detailed handling transducers
"""
import hfst
def pairname(insym, outsym):
"""Convert a pair of symbols into a single label
insym -- input symbol as a string
outsym -- output symbol as a string
Returns a string notation of the pair, eg.
>>> pairname ('a', 'a')
a
>>> pairname('i','j')
i:j
"""
if insym == outsym:
return(insym)
else:
return(insym + ":" + outsym)
def equivpairs(bfst):
"""Find and print all sets of equivalent transition pairs.
bfst -- a HfstBasicTransducer whose transition symbol pairs are analyzed
Sets of transition symbol pairs behaving identicaly are computed.
The sets are printed if they contain more than one element.
"""
transitions_for_pairsymbol = {} # {pairsym: list of trs, ..}
for state in bfst.states():
for arc in bfst.transitions(state):
target = arc.get_target_state()
pair_symbol = pairname(arc.get_input_symbol(),
arc.get_output_symbol())
if pair_symbol not in transitions_for_pairsymbol:
transitions_for_pairsymbol[pair_symbol] = set()
transitions_for_pairsymbol[pair_symbol].add((state,target))
pairsymbols_for_transition_sets = {} # {tr set: pair syms, ..}
for pair_symbol, st in transitions_for_pairsymbol.items():
froz = frozenset(st)
if froz not in pairsymbols_for_transition_sets:
pairsymbols_for_transition_sets[froz] = []
pairsymbols_for_transition_sets[froz].append(pair_symbol)
labelsym = {} # {sym: sym representing it in pprinting}
for fs, sl in pairsymbols_for_transition_sets.items():
sorted_sl = sorted(sl)
model = sorted(sl)[0]
for sym in sorted(sl):
if len(sym) < len(model): model = sym
for sym in sorted(sl):
labelsym[sym] = model
#print("labelsym: ", labelsym) ##
return(labelsym, pairsymbols_for_transition_sets)
def fst2dicfst(FST):
BFST = hfst.HfstBasicTransducer(FST)
dicfst = {}
for state in BFST.states():
tdir = {}
for arc in BFST.transitions(state):
prnm = pairname(arc.get_input_symbol(),
arc.get_output_symbol())
tdir[prnm] = arc.get_target_state()
dicfst[state] = (BFST.is_final_state(state), tdir)
return(dicfst)
def fst_to_fsa(FST, separator='^'):
"""Converts FST into an FSA by joining input and output symbols with separator"""
FB = hfst.HfstBasicTransducer(FST)
sym_pairs = FB.get_transition_pairs()
dict = {}
for sym_pair in sym_pairs:
in_sym, out_sym = sym_pair
joint_sym = in_sym + separator + out_sym
dict[sym_pair] = (joint_sym, joint_sym)
FB.substitute(dict)
FSA = hfst.HfstTransducer(FB)
# print("fst_to_fsa:\n", FSA) ##
return FSA
def fsa_to_fst(FSA, separator='^'):
BFSA = hfst.HfstBasicTransducer(FSA)
sym_pairs = BFSA.get_transition_pairs()
dic = {}
for sym_pair in sym_pairs:
insym, outsym = sym_pair
in_sym, out_sym = outsym.split(separator)
dic[sym_pair] = (in_sym, out_sym)
BFSA.substitute(dic)
FST = hfst.HfstTransducer(BFSA)
return FST
def ppfst(FST, print_equiv_classes=True, title=""):
"""Pretty-prints a HfstTransducer or a HfstBasicTransducer.
FST -- the transducer to be pretty-printed
print_equiv_classes -- if True, then print also
the equivalence classes
If the transducer has a name, it is printed as a heading.
>>> twbt.ppfst(hfst.regex("a* [b:p|c] [c|b:p]"), True)
0 . -> 0 a ; -> 1 b:p ;
1 . -> 2 b ;
2 :
Classes of equivalent symbols:
b:p c
"""
name = FST.get_name()
if name:
print("\n" + name)
if title:
print("\n" + title)
BFST = hfst.HfstBasicTransducer(FST)
labsy, transy = equivpairs(BFST)
for state in BFST.states():
d = {}
for arc in BFST.transitions(state):
target = arc.get_target_state()
if target not in d: d[target] = []
prnm = pairname(arc.get_input_symbol(),
arc.get_output_symbol())
d[target].append(prnm)
print(" ", state, (": " if BFST.is_final_state(state) else ". "),
end="")
for st, plist in d.items():
ls = [p for p in plist if p == labsy[p]]
print( " " + (" ".join(ls)) + " -> " + str(st), end=" ;" )
print()
#print(transy) ##
if print_equiv_classes:
all_short = True
for ss, pl in transy.items():
if len(pl) > 1:
all_short = False
break
if not all_short:
print("Classes of equivalent symbols:")
for ss, pl in transy.items():
if len(pl) > 1:
print(" ", " ".join(sorted(pl)))
return
def ppdef(XRC, name, displayed_formula):
FST = XRC.compile(name)
BFST = hfst.HfstBasicTransducer(FST)
FST = hfst.HfstTransducer(BFST)
FST.set_name(name + " = " + displayed_formula)
ppfst(FST, True)
#alph = [pairname(insym, outsym) for insym, outsym
# in FST.get_transition_pairs()]
#print(name, '=',', '.join(sorted(alph)))
return
def pp_paths(TR, heading, limit=30):
results = paths(TR, limit)
print(heading, end="")
if len(results) == 0:
print(" None")
else:
print()
for line in results:
print(line)
def paths(TR, limit=30):
path_tuple = TR.extract_paths(output='raw', max_number=limit)
results = []
for weight, path in path_tuple:
lst = [pairname(insym, outsym) for insym, outsym in path]
str = " ".join(lst)
results.append(str)
return(results)
def expanded_examples(TR, insyms, symbol_pair_set):
# print("symbol_pair_set =", symbol_pair_set) ##
BT = hfst.HfstBasicTransducer(TR)
# print("BT.get_transition_pairs() =", BT.get_transition_pairs()) ##
for insym in insyms:
lst = [(ins, outs)
for ins, outs
in symbol_pair_set if ins == insym]
for sympair in lst:
# print("sympair, lst =", sympair, lst) ##
BT.substitute(sympair, tuple(lst))
T = hfst.HfstTransducer(BT)
T.set_name("negative and positive together")
T.minimize()
# ppfst(T, True) ##
#T.minus(TR)
#T.minimize()
return(T)