forked from Aunsiels/pyformlang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fst.py
More file actions
221 lines (191 loc) · 7.55 KB
/
test_fst.py
File metadata and controls
221 lines (191 loc) · 7.55 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
""" Tests the FST """
# pylint: disable=duplicate-code
from os import path
import pytest
from pyformlang.fst import FST
from pyformlang.indexed_grammar import (
DuplicationRule, ProductionRule, EndRule,
ConsumptionRule, IndexedGrammar, Rules)
@pytest.fixture
def fst0():
fst0 = FST()
fst0.add_start_state("q0")
fst0.add_transition("q0", "a", "q1", ["b"])
fst0.add_final_state("q1")
yield fst0
@pytest.fixture
def fst1():
fst1 = FST()
fst1.add_start_state("q1")
fst1.add_transition("q1", "b", "q2", ["c"])
fst1.add_final_state("q2")
yield fst1
class TestFST:
""" Tests FST """
def test_creation(self):
""" Test Translate """
fst = FST()
assert fst is not None
assert len(fst.states) == 0
assert len(fst.input_symbols) == 0
assert len(fst.output_symbols) == 0
assert fst.get_number_transitions() == 0
assert len(fst.final_states) == 0
fst.add_start_state("q0")
assert len(fst.states) == 1
fst.add_transition("q0", "a", "q1", ["bc"])
assert len(fst.states) == 2
assert len(fst.input_symbols) == 1
assert len(fst.output_symbols) == 1
assert fst.get_number_transitions() == 1
assert len(fst.final_states) == 0
fst.add_transition("q0", "epsilon", "q1", ["bc"])
assert len(fst.states) == 2
assert len(fst.input_symbols) == 1
assert len(fst.output_symbols) == 1
assert fst.get_number_transitions() == 2
assert len(fst.final_states) == 0
fst.add_final_state("q2")
assert len(fst.states) == 3
assert len(fst.input_symbols) == 1
assert len(fst.output_symbols) == 1
assert fst.get_number_transitions() == 2
assert len(fst.final_states) == 1
fst.add_transition("q0", "a", "q1", ["d"])
assert len(fst.states) == 3
assert len(fst.input_symbols) == 1
assert len(fst.output_symbols) == 2
assert fst.get_number_transitions() == 3
assert len(fst.final_states) == 1
def test_translate(self):
""" Test a translation """
fst = FST()
fst.add_start_state("q0")
translation = list(fst.translate(["a"]))
assert len(translation) == 0
fst.add_transition("q0", "a", "q1", ["b"])
translation = list(fst.translate(["a"]))
assert len(translation) == 0
fst.add_final_state("q1")
translation = list(fst.translate(["a"]))
assert len(translation) == 1
assert translation == [["b"]]
fst.add_transition("q1", "epsilon", "q1", ["c"])
translation = list(fst.translate(["a"], max_length=10))
assert len(translation) == 10
assert ["b"] in translation
assert ["b", "c"] in translation
assert ["b"] + ["c"] * 9 in translation
def test_intersection_indexed_grammar(self):
""" Test the intersection with indexed grammar """
l_rules = []
rules = Rules(l_rules)
indexed_grammar = IndexedGrammar(rules)
fst = FST()
intersection = fst & indexed_grammar
assert intersection.is_empty()
l_rules.append(ProductionRule("S", "D", "f"))
l_rules.append(DuplicationRule("D", "A", "B"))
l_rules.append(ConsumptionRule("f", "A", "Afinal"))
l_rules.append(ConsumptionRule("f", "B", "Bfinal"))
l_rules.append(EndRule("Afinal", "a"))
l_rules.append(EndRule("Bfinal", "b"))
rules = Rules(l_rules)
indexed_grammar = IndexedGrammar(rules)
intersection = fst.intersection(indexed_grammar)
assert intersection.is_empty()
fst.add_start_state("q0")
fst.add_final_state("final")
fst.add_transition("q0", "a", "q1", ["a"])
fst.add_transition("q1", "b", "final", ["b"])
intersection = fst.intersection(indexed_grammar)
assert not intersection.is_empty()
def test_union(self, fst0, fst1):
""" Tests the union"""
fst_union = fst0.union(fst1)
self._make_test_fst_union(fst_union)
fst_union = fst0 | fst1
self._make_test_fst_union(fst_union)
def _make_test_fst_union(self, fst_union):
assert len(fst_union.start_states) == 2
assert len(fst_union.final_states) == 2
assert fst_union.get_number_transitions() == 2
translation = list(fst_union.translate(["a"]))
assert translation == [["b"]]
translation = list(fst_union.translate(["b"]))
assert translation == [["c"]]
translation = list(fst_union.translate(["a", "b"]))
assert translation == []
def test_concatenate(self, fst0, fst1):
""" Tests the concatenation """
fst_concatenate = fst0 + fst1
translation = list(fst_concatenate.translate(["a", "b"]))
assert translation == [["b", "c"]]
translation = list(fst_concatenate.translate(["a"]))
assert translation == []
translation = list(fst_concatenate.translate(["b"]))
assert translation == []
def test_concatenate2(self, fst0, fst1):
""" Tests the concatenation """
fst_concatenate = fst0 + fst1 + fst1
translation = list(fst_concatenate.translate(["a", "b", "b"]))
assert translation == [["b", "c", "c"]]
translation = list(fst_concatenate.translate(["a"]))
assert translation == []
translation = list(fst_concatenate.translate(["b"]))
assert translation == []
def test_kleene_start(self, fst0):
""" Tests the kleene star on a fst"""
fst_star = fst0.kleene_star()
translation = list(fst_star.translate(["a"]))
assert translation == [["b"]]
translation = list(fst_star.translate(["a", "a"]))
assert translation == [["b", "b"]]
translation = list(fst_star.translate([]))
assert translation == [[]]
def test_generate_empty_word_from_nothing(self):
""" Generate empty word from nothing """
fst = FST()
fst.add_start_state("q0")
fst.add_transition("q0", "epsilon", "q1", [])
fst.add_final_state("q1")
translation = list(fst.translate([]))
assert translation == [[]]
def test_epsilon_loop(self):
""" Test empty loop """
fst = FST()
fst.add_start_state("q0")
fst.add_transition("q0", "epsilon", "q1", [])
fst.add_final_state("q1")
fst.add_transition("q1", "epsilon", "q0", [])
translation = list(fst.translate([]))
assert translation == [[]]
def test_epsilon_loop2(self):
""" Test empty loop bis """
fst = FST()
fst.add_start_state("q0")
fst.add_transitions(
[("q0", "epsilon", "q1", []),
("q1", "a", "q2", ["b"]),
("q1", "epsilon", "q0", [])])
fst.add_final_state("q2")
translation = list(fst.translate(["a"]))
assert translation == [["b"]]
def test_paper(self):
""" Test for the paper """
fst = FST()
fst.add_transitions(
[(0, "I", 1, ["Je"]), (1, "am", 2, ["suis"]),
(2, "alone", 3, ["tout", "seul"]),
(2, "alone", 3, ["seul"])])
fst.add_start_state(0)
fst.add_final_state(3)
assert list(fst.translate(["I", "am", "alone"])) == \
[['Je', 'suis', 'seul'],
['Je', 'suis', 'tout', 'seul']]
fst = FST.from_networkx(fst.to_networkx())
assert list(fst.translate(["I", "am", "alone"])) == \
[['Je', 'suis', 'seul'],
['Je', 'suis', 'tout', 'seul']]
fst.write_as_dot("fst.dot")
assert path.exists("fst.dot")