File: test_streams.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 (110 lines) | stat: -rw-r--r-- 2,937 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
/*
   Test file for HfstInputStream, HfstOutputStream and functions that
   write or read AT&T format.
*/

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

using namespace hfst;

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};

  for (unsigned int i=0; i<TYPES_SIZE; i++)
    {

      if (not HfstTransducer::is_implementation_type_available(types[i]))
	continue;

      /* From AT&T format. */
      verbose_print("Construction from AT&T format", types[i]);

      unsigned int transducers_read=0;
      FILE * file = fopen((std::string(getenv("srcdir")) +
               std::string("/test_transducers.att")).c_str(), "rb");
      assert(file != NULL);
      try
    {
      while (not feof(file))
        {
	  unsigned int linecount = 0;
          HfstTransducer t(file, types[i], "<eps>", linecount);
	  (void)linecount;
          transducers_read++;
        }
    }
      //catch (hfst::exceptions::NotValidAttFormatException e)
      catch (const HfstException e)
    {
      assert(transducers_read == 4);
    }

      /* To AT&T format. */
      verbose_print("Writing in AT&T format", types[i]);
      
      FILE * f2 = fopen("transducer.att", "wb");
      fprintf(f2,
          "0\t1\tbaz\t@0@\t0.000000\n"
          "1\t2\tfoo\tbar\t0.000000\n"
          "2\t0.000000\n");
      fclose(f2);

      FILE * ofile = fopen("transducer2.att", "wb");

      HfstTransducer t1("foo", "bar", types[i]);
      HfstTransducer t2("baz", "@_EPSILON_SYMBOL_@", types[i]);
      t2.concatenate(t1);
      t2.minimize();
      t2.write_in_att_format(ofile, true);
      fclose(ofile);
      assert(system("diff transducer2.att transducer.att") == 0);
      remove("transducer2.att");
      remove("transducer.att");

      /* From HfstInputStream. */
      verbose_print("Writing to HfstOutputStream", types[i]);

      HfstTransducer tr1("foo", types[i]);
      HfstTransducer tr2("bar", "foo", types[i]);
      HfstTransducer tr3("a", types[i]);
      HfstTransducer tr4("b", "c", types[i]);

      HfstOutputStream out("testfile.hfst", types[i]);
      out << tr1;
      out << tr2;
      out << tr3;
      out << tr4;
      out.close();

      verbose_print("Construction from HfstInputStream", types[i]);

      HfstInputStream in("testfile.hfst");
      std::vector<HfstTransducer> transducers;
      transducers_read=0;
      while (not in.is_eof())
    {
      HfstTransducer tr(in);
      transducers.push_back(tr);
      transducers_read++;
    }
      in.close();
      remove("testfile.hfst");


      assert(transducers_read == 4);

      assert(transducers[0].compare(tr1));
      assert(transducers[1].compare(tr2));
      assert(transducers[2].compare(tr3));
      assert(transducers[3].compare(tr4));

    }

}