File: test_rules.cc

package info (click to toggle)
hfst 3.16.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,532 kB
  • sloc: cpp: 101,875; sh: 6,717; python: 5,225; yacc: 4,985; lex: 2,900; makefile: 2,017; xml: 6
file content (239 lines) | stat: -rw-r--r-- 7,476 bytes parent folder | download | duplicates (4)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * A test file for functions in namespace hfst::rules.
 */

#include "HfstTransducer.h"
#include "auxiliary_functions.cc"

#include <cstdio>
#include <assert.h>
#include <fstream>

using namespace hfst;
;


void compare_and_delete(HfstTransducer * rule_transducers1[],
            HfstTransducer * rule_transducers2[],
            HfstTransducer * rule_transducers3[])
{

  if (not HfstTransducer::is_implementation_type_available
      (TROPICAL_OPENFST_TYPE))
    return;

  if (HfstTransducer::is_implementation_type_available(SFST_TYPE)) {
    rule_transducers1[0]->convert(TROPICAL_OPENFST_TYPE);
    rule_transducers2[0]->convert(TROPICAL_OPENFST_TYPE);
    rule_transducers3[0]->convert(TROPICAL_OPENFST_TYPE);
  }
  
  if (HfstTransducer::is_implementation_type_available(FOMA_TYPE)) {
    rule_transducers1[2]->convert(TROPICAL_OPENFST_TYPE);
    rule_transducers2[2]->convert(TROPICAL_OPENFST_TYPE);
    rule_transducers3[2]->convert(TROPICAL_OPENFST_TYPE);
  }
  
  if (HfstTransducer::is_implementation_type_available
      (SFST_TYPE)) {
    assert(rule_transducers1[0]->compare(*rule_transducers1[1]) );
    assert(rule_transducers2[0]->compare(*rule_transducers2[1]) );
    assert(rule_transducers3[0]->compare(*rule_transducers3[1]) );
  }

  if (HfstTransducer::is_implementation_type_available
      (FOMA_TYPE)) {
    assert(rule_transducers1[2]->compare(*rule_transducers1[1]) );
    assert(rule_transducers2[2]->compare(*rule_transducers2[1]) );
    assert(rule_transducers3[2]->compare(*rule_transducers3[1]) );
  }
  
  for (int i=0; i<3; i++) {
    if (rule_transducers1[i] != NULL)
      delete rule_transducers1[i];
    if (rule_transducers2[i] != NULL)
      delete rule_transducers2[i];
    if (rule_transducers3[i] != NULL)
      delete rule_transducers3[i];
  }
  
}

int main(int argc, char **argv) {
  
  ImplementationType types [] = {SFST_TYPE, TROPICAL_OPENFST_TYPE, FOMA_TYPE};
  HfstTransducer * rule_transducers1 [3] = {NULL, NULL, NULL};
  HfstTransducer * rule_transducers2 [3] = {NULL, NULL, NULL};
  HfstTransducer * rule_transducers3 [3] = {NULL, NULL, NULL};
  
  
  /* HfstTransducer two_level_if(
     HfstTransducerPair &context,
     StringPairSet &mappings,
     StringPairSet &alphabet,
     ImplementationType type); */
  
  verbose_print("HfstTransducer two_level_if("
		"HfstTransducerPair &context,"
		"StringPairSet &mappings,"
		"StringPairSet &alphabet,"
		"ImplementationType type");

  {
    for (int i=0; i<3; i++) {
      
      if (not HfstTransducer::is_implementation_type_available(types[i]))
	continue;
      
      HfstTransducer leftc("c", types[i]);
      HfstTransducer rightc("c", types[i]);
      HfstTransducerPair context(leftc, rightc);
      StringPair mapping("a", "b");
      StringPairSet mappings;
      mappings.insert(mapping);
      StringPairSet alphabet;
      alphabet.insert(StringPair("a", "a"));
      alphabet.insert(StringPair("a", "b"));
      alphabet.insert(StringPair("b", "b"));
      alphabet.insert(StringPair("c", "c"));
      

      HfstTransducer rule_transducer1
	= rules::two_level_if(context, mappings, alphabet);
      HfstTransducer rule_transducer2
	= rules::two_level_only_if(context, mappings, alphabet);
      HfstTransducer rule_transducer3
	= rules::two_level_if_and_only_if(context, mappings, alphabet);
      rule_transducers1[i] = new HfstTransducer(rule_transducer1);
      rule_transducers2[i] = new HfstTransducer(rule_transducer2);
      rule_transducers3[i] = new HfstTransducer(rule_transducer3);
    }
  }
  
  compare_and_delete(rule_transducers1,
		     rule_transducers2,
		     rule_transducers3);
  
  // replace_down_karttunen
  {
    for (int i=0; i<3; i++) {
      
      if (not HfstTransducer::is_implementation_type_available(types[i]))
	continue;
      
      HfstTokenizer TOK;
      TOK.add_multichar_symbol("@_EPSILON_SYMBOL_@");
      HfstTransducer mapping("ab", "x", TOK, types[i]);
      HfstTransducer left_context("ab", "ab", TOK, types[i]);
      HfstTransducer right_context("a", types[i]);
      HfstTransducerPair context(left_context, right_context);
      StringPairSet alphabet;
      alphabet.insert(StringPair("a", "a"));
      alphabet.insert(StringPair("b", "b"));
      alphabet.insert(StringPair("x", "x"));
      bool optional = false;
      
      HfstTransducer replace_down_transducer
	= rules::replace_down_karttunen(context, mapping, optional, alphabet);
      
      HfstTransducer test_abababa("abababa", TOK, types[i]);
      test_abababa.compose(replace_down_transducer);
      HfstTransducer abxaba("abababa",
			    "abx@_EPSILON_SYMBOL_@aba", TOK, types[i]);
      HfstTransducer ababxa("abababa",
			    "ababx@_EPSILON_SYMBOL_@a", TOK, types[i]);
      HfstTransducer expected_result(types[i]);
      expected_result.disjunct(abxaba);
      expected_result.disjunct(ababxa);
      assert(expected_result.compare(test_abababa));
    }
  }
  
#ifdef FOO
  /* HfstTransducer &replace_in_context(
     HfstTransducerPair &context,
     ReplaceType repl_type,
     HfstTransducer &t,
     bool optional,
     StringPairSet &alphabet) */
  
  verbose_print("HfstTransducer &replace_in_context("
		"HfstTransducerPair &context,"
		"ReplaceType repl_type,"
		"HfstTransducer &t,"
		"bool optional,"
		"StringPairSet &alphabet");
  
  for (int i=0; i<3; i++) {
    
    if (not HfstTransducer::is_implementation_type_available(types[i]))
      continue;
    
    HfstTransducer left("c",types[i]);
    HfstTransducer right("c",types[i]);
    HfstTransducerPair context(left,right);
    HfstTransducer mapping("a","b",types[i]);
    HfstTransducer mapping2("b","c",types[i]);
    mapping.disjunct(mapping2);
    StringPairSet alphabet;
    alphabet.insert(StringPair("a","a"));
    alphabet.insert(StringPair("b","b"));
    alphabet.insert(StringPair("c","c"));
    HfstTransducer rule = rules::replace_up(context, mapping, false, alphabet);
  }
#endif
 

  // replace_up for SFST in a special case that seems to fail sometimes

  if (HfstTransducer::is_implementation_type_available(SFST_TYPE))
    {
      ImplementationType type = SFST_TYPE;
      
      HfstBasicTransducer mapping;
      
      mapping.add_transition
	(0, HfstBasicTransition(1, "@_EPSILON_SYMBOL_@", "X", 0));
      mapping.set_final_weight(1, 0);
      
      mapping.add_transition
	(0, HfstBasicTransition(2, "@_UNKNOWN_SYMBOL_@", "X", 0));
      mapping.add_transition
	(0, HfstBasicTransition(2, "X", "X", 0));
      mapping.add_transition
	(2, HfstBasicTransition(2, "X", "@_EPSILON_SYMBOL_@", 0));
      mapping.add_transition
	(2, HfstBasicTransition
	 (2, "@_UNKNOWN_SYMBOL_@", "@_EPSILON_SYMBOL_@", 0));
      mapping.set_final_weight(2, 0);
      
      HfstTransducer mapping_(mapping, type);
      
      StringPairSet alphabet;
      alphabet.insert(StringPair("a", "a"));
      alphabet.insert(StringPair("b", "b"));
      alphabet.insert(StringPair("X", "X"));
      alphabet.insert(StringPair("K", "K"));
      
      bool optional = false;
      
      HfstTransducerPair context(HfstTransducer("K", "K", type),
				 HfstTransducer("K", "K", type));
      
      HfstTransducer rule
	= rules::replace_up(context, mapping_, optional, alphabet);
      
      HfstTokenizer TOK;
      
      HfstTransducer abKabKab("abKabKab", TOK, type);
      
      abKabKab.compose(rule).minimize();
      
      HfstTransducer abKabKab_("abKabKab", TOK, type);
      
      assert(not abKabKab.compare(abKabKab_));
      
    }
  
}