HFST - Helsinki Finite-State Transducer Technology - C++ API  version 3.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XfsmTransducer.h
Go to the documentation of this file.
1 // Copyright (c) 2016 University of Helsinki
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 3 of the License, or (at your option) any later version.
7 // See the file COPYING included with this distribution for more
8 // information.
9 
10 #ifndef _XFSM_TRANSDUCER_H_
11 #define _XFSM_TRANSDUCER_H_
12 
13 #include "HfstSymbolDefs.h"
14 #include "HfstExceptionDefs.h"
15 #include "HfstExtractStrings.h"
16 #include "HfstFlagDiacritics.h"
17 #include <stdlib.h>
18 
19 #ifndef _XFSMLIB_H_
20 #define _XFSMLIB_H_
21 #include "xfsm/xfsm_api.h"
22 #endif
23 #include <cstdio>
24 #include <string>
25 #include <sstream>
26 #include <iostream>
27 
28 #include "../FormatSpecifiers.h"
29 
34 namespace hfst {
35  namespace implementations
36 {
37 
38  /* A class for reading XFSM binary transducers from a file. */
39  class XfsmInputStream
40  {
41  private:
42  /* The name of the file where transducers will be read from. */
43  std::string filename;
44  /* A list of transducers that will return transducers one by one when
45  read_transducer() is called. */
46  NVptr net_list;
47  /* The size of net_list. */
48  int list_size;
49  /* The position of the transducer that read_transducer() will return. */
50  int list_pos;
51  public:
52  /* Throws an error. */
53  XfsmInputStream(void);
54  /* A stream that will read transducers from file \a filename. */
55  XfsmInputStream(const std::string &filename);
56  /* Frees the memory allocated for this XfsmInputStream. */
57  void close(void);
58  /* Whether there are any transducers left in net_list. */
59  bool is_eof(void);
60  /* Whether the stream is bad for reading, always false. */
61  bool is_bad(void);
62  /* Whether the stream is good for reading, always true. */
63  bool is_good(void);
64  /* Whether the next item returned by read_transducer() is a valid
65  XFSM transducer. Basically always true, except if is_eof() is true. */
66  bool is_fst(void);
67  /* Delayed read, returns a transducer from net_list. */
68  NETptr read_transducer();
69  };
70 
71  /* A class for writing XFSM transducers in binary format to a file. */
72  class XfsmOutputStream
73  {
74  private:
75  /* The name of the file where transducers will be written. */
76  std::string filename;
77  /* A list of transducers to be written when flush() is called. */
78  NVptr net_list;
79  public:
80  /* Throws an error. */
81  XfsmOutputStream(void);
82  /* A stream that will write transducers into file \a filename. */
83  XfsmOutputStream(const std::string &filename);
84  /* Writes the contents of net_list into file filename. */
85  void flush();
86  /* Does nothing. */
87  void close();
88  /* Delayed write, stores a copy of \a transducer in net_list. */
89  void write_transducer(NETptr transducer);
90  };
91 
92  class XfsmTransducer {
93  private:
94  static bool minimize_even_if_already_minimal_;
95 
96  public:
97  static void initialize_xfsm();
98  static NETptr create_xfsm_unknown_to_unknown_transducer();
99  static NETptr create_xfsm_identity_to_identity_transducer();
100  static id_type hfst_symbol_to_xfsm_symbol(const std::string & symbol);
101  static std::string xfsm_symbol_to_hfst_symbol(id_type id);
102  static void label_id_to_symbol_pair(id_type label_id, std::string & isymbol, std::string & osymbol);
103  static id_type symbol_pair_to_label_id(const std::string & isymbol, const std::string & osymbol);
104 
105  static NETptr create_empty_transducer(void);
106  static NETptr create_epsilon_transducer(void);
107  static NETptr define_transducer(const hfst::StringPairVector &spv);
108  static NETptr define_transducer
109  (const hfst::StringPairSet &sps, bool cyclic=false);
110  static NETptr define_transducer(const std::vector<StringPairSet> &spsv);
111  static NETptr define_transducer
112  (const std::string &symbol);
113  static NETptr define_transducer
114  (const std::string &isymbol, const std::string &osymbol);
115  static NETptr copy(NETptr t);
116  static NETptr minimize(NETptr t);
117 
118  static NETptr compose(NETptr t1, const NETptr t2);
119  static NETptr concatenate(NETptr t1, const NETptr t2);
120  static NETptr disjunct(NETptr t1, const NETptr t2);
121  static NETptr intersect(NETptr t1, const NETptr t2);
122  static NETptr subtract(NETptr t1, const NETptr t2);
123 
124  static bool are_equivalent(NETptr t1, NETptr t2);
125  static bool is_cyclic(NETptr t);
126  static unsigned int number_of_states(NETptr t);
127  static unsigned int number_of_arcs(NETptr t);
128  static NETptr eliminate_flags_xfsm(NETptr t);
129  static NETptr eliminate_flag_xfsm(NETptr t, const std::string & flag);
130 
131  static NETptr repeat_star(NETptr t);
132  static NETptr repeat_plus(NETptr t);
133  static NETptr repeat_n(NETptr t, unsigned int n);
134  static NETptr repeat_le_n(NETptr t, unsigned int n);
135  static NETptr repeat_n_plus(NETptr t, unsigned int n);
136  static NETptr repeat_n_to_k(NETptr t, unsigned int n, unsigned int k);
137 
138  static NETptr optionalize(NETptr t);
139  static NETptr invert(NETptr t);
140  static NETptr reverse(NETptr t);
141  static NETptr extract_input_language(NETptr t);
142  static NETptr extract_output_language(NETptr t);
143 
144  static NETptr insert_freely(NETptr t, const NETptr ins);
145 
146  static StringSet get_alphabet(const NETptr t);
147  static void add_symbol_to_alphabet(NETptr t, const std::string & symbol);
148  static void add_symbols_to_alphabet(NETptr t, const StringSet & symbols);
149  static void remove_symbols_from_alphabet(NETptr t, const StringSet & symbols);
150 
151  // for debugging
152  static void set_minimize_even_if_already_minimal(bool value);
153  static void set_compose_flag_as_special(bool value);
154 
155  static void write_in_att_format(NETptr t, const char * filename);
156  static void write_in_prolog_format(NETptr t, const char * filename);
157  static NETptr prolog_file_to_xfsm_transducer(const char * filename);
158 
159  } ;
160 } }
161 #endif
std::vector< std::pair< std::string, std::string > > StringPairVector
A vector of string pairs.
Definition: HfstDataTypes.h:105
A file for exceptions.
Class declarations for flag diacritic handling.
std::set< StringPair > StringPairSet
A set of symbol pairs used in substituting symbol pairs and in rule functions.
Definition: HfstSymbolDefs.h:82
Typedefs and functions for symbols, symbol pairs and sets of symbols.