File: test_examples.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 (245 lines) | stat: -rw-r--r-- 6,514 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
240
241
242
243
244
245
/*
   Test file for ...
*/

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

using namespace hfst;

using hfst::implementations::HfstBasicTransition;
using hfst::implementations::HfstBasicTransducer;

bool function(const StringPair &sp, StringPairSet &sps)
{
  if (sp.second.compare(sp.first) != 0)
    return false;

  std::string isymbol = sp.first;
  std::string osymbol;

  if (sp.second.compare("a") == 0 ||
      sp.second.compare("o") == 0 ||
      sp.second.compare("u") == 0)
    osymbol = std::string("<back_wovel>");
  if (sp.second.compare("e") == 0 ||
      sp.second.compare("i") == 0)
    osymbol = std::string("<front_wovel>");

  sps.insert(StringPair(isymbol, osymbol));
  return true;
}


int main(int argc, char **argv)
{

  const unsigned int TYPES_SIZE=3;
  const ImplementationType types [] = {SFST_TYPE,
                       TROPICAL_OPENFST_TYPE,
                       /*LOG_OPENFST_TYPE,*/
                       FOMA_TYPE};

  HfstBasicTransducer tr1;
  tr1.add_state(1);
  tr1.set_final_weight(1, 0);
  tr1.add_transition
    (0, HfstBasicTransition(1, "@_UNKNOWN_SYMBOL_@", "foo", 0) );
  // tr1 is [ @_UNKNOWN_SYMBOL_@:foo ]
  
  HfstBasicTransducer tr2;
  tr2.add_state(1);
  tr2.add_state(2);
  tr2.set_final_weight(2, 0);
  tr2.add_transition
    (0, HfstBasicTransition(1, "@_IDENTITY_SYMBOL_@",
                "@_IDENTITY_SYMBOL_@", 0) );
  tr2.add_transition
    (1, HfstBasicTransition(2, "bar", "bar", 0) );
  // tr2 is [ [ @_IDENTITY_SYMBOL_@:@_IDENTITY_SYMBOL_@ ] [ bar:bar ] ]

  // The disjunction should be
  HfstBasicTransducer disj;
  disj.add_state(1);
  disj.add_state(2);
  disj.set_final_weight(2, 0);

  disj.add_transition
    (0, HfstBasicTransition(1, "@_IDENTITY_SYMBOL_@",
                "@_IDENTITY_SYMBOL_@", 0) );
  disj.add_transition
    (0, HfstBasicTransition(1, "foo", "foo", 0) );

  disj.add_transition
    (0, HfstBasicTransition(2, "@_UNKNOWN_SYMBOL_@", "foo", 0) );
  disj.add_transition
    (0, HfstBasicTransition(2, "bar", "foo", 0) );

  disj.add_transition
    (1, HfstBasicTransition(2, "bar", "bar", 0) );


  FILE * ofile = fopen("testfile.att", "wb");
  fprintf(ofile, "0 1 a b 0.4\n");
  fprintf(ofile, "1 c d\n");
  fclose(ofile);


  // For all transducer implementation types, perform the following tests: */
  for (unsigned int i=0; i<TYPES_SIZE; i++)
    {
      if (not HfstTransducer::is_implementation_type_available(types[i]))
	continue;

      verbose_print("expanding unknowns", types[i]);
      
      HfstTransducer Tr1(tr1, types[i]);
      HfstTransducer Tr2(tr2, types[i]);
      HfstTransducer Disj(disj, types[i]);

      Tr1.disjunct(Tr2).minimize();
      // Tr1 is expanded to [ @_UNKNOWN_SYMBOL_@:foo | bar:foo ]
      // Tr2 is expanded to
      // [ [ @_IDENTITY_SYMBOL_@:@_IDENTITY_SYMBOL_@ | foo:foo ] [ bar:bar ] ]
 
      assert(Tr1.compare(Disj));


      verbose_print("testing NotValidAttFormatException", types[i]);

      FILE * ifile = fopen("testfile.att", "rb");
      try {
	unsigned int linecount = 0;
	HfstTransducer t(ifile, types[i], "@_EPSILON_SYMBOL_@", linecount);
    assert(false);
      }
      catch (NotValidAttFormatException e)
    {
      assert(true);
    }
      fclose(ifile);
      

    }
      remove("testfile.att");

      if (HfstTransducer::is_implementation_type_available(FOMA_TYPE))
  {
    ImplementationType type=FOMA_TYPE;
    
    /* Create a simple lexicon transducer
       [[foo bar foo] | [foo bar baz]]. */
    
    HfstTokenizer tok;
    tok.add_multichar_symbol("foo");
    tok.add_multichar_symbol("bar");
    tok.add_multichar_symbol("baz");
    
    HfstTransducer words("foobarfoo", tok, type);
    HfstTransducer t("foobarbaz", tok, type);
    words.disjunct(t);
    
    
    /* Create a rule transducer that optionally replaces
       "bar" with "baz" between "foo" and "foo". */
    
    HfstTransducerPair context
      (HfstTransducer("foo", type),
       HfstTransducer("foo", type) );
    HfstTransducer mapping
      ("bar", "baz", type);
    bool optional=true;
    
    StringPairSet alphabet;
    alphabet.insert(StringPair("foo", "foo"));
    alphabet.insert(StringPair("bar", "bar"));
    alphabet.insert(StringPair("baz", "baz"));
    
    HfstTransducer rule = rules::replace_up
      (context, mapping, optional, alphabet);
    
    
    /* Apply the rule transducer to the lexicon. */
    words.compose(rule).minimize();
    
    
    /* Extract all string pairs from the result and print
       them to stdout. */
    
    HfstTwoLevelPaths results;
    
    try {
      words.extract_paths(results);
    }
    catch (TransducerIsCyclicException e)
      {
    /* This should not happen because transducer is not cyclic. */
    fprintf(stderr, "TEST FAILED\n");
    exit(1);
      }
    
    /* Go through all paths. */
    for (HfstTwoLevelPaths::const_iterator it = results.begin();
         it != results.end(); it++)
      {
    /* Go through each path. */
    StringPairVector spv = it->second;
    std::string istring("");
    std::string ostring("");
    
    for (StringPairVector::const_iterator IT = spv.begin();
         IT != spv.end(); IT++)
      {
        istring.append(IT->first);
        ostring.append(IT->second);
      }
    /*fprintf(stdout, "%s : %s\n",
        istring.c_str(),
        ostring.c_str());*/
      }
  }


      if (HfstTransducer::is_implementation_type_available(SFST_TYPE))
  {
    ImplementationType type = SFST_TYPE;

    StringPairSet ruleset1;
    ruleset1.insert(StringPair("a","b"));
    ruleset1.insert(StringPair("c","d"));
    ruleset1.insert(StringPair("e","f"));
    HfstTransducer rule1(ruleset1, type);

    StringPairSet ruleset2;
    ruleset2.insert(StringPair("a","b"));
    ruleset2.insert(StringPair("g","g"));
    ruleset2.insert(StringPair("i","j"));
    HfstTransducer rule2(ruleset2, type);

    StringPairSet ruleset3;
    ruleset3.insert(StringPair("a","b"));
    ruleset3.insert(StringPair("k","l"));
    ruleset3.insert(StringPair("m","n"));
    HfstTransducer rule3(ruleset3, type);

    std::vector<HfstTransducer> rules;
    rules.push_back(rule1);
    rules.push_back(rule2);
    rules.push_back(rule3);

    HfstTransducer test("A", "a", type);
    test.compose_intersect(rules);
    //std::cerr << test;
  }


      if (HfstTransducer::is_implementation_type_available(SFST_TYPE))
  {
    HfstTransducer t("a", "a", SFST_TYPE);
    t.substitute(&function);
    HfstTransducer T("a", "<back_wovel>", SFST_TYPE);
    assert(t.compare(T));
  }

}