HFST - Helsinki Finite-State Transducer Technology API  version 3.7.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HfstExtractStrings.h
1 // This program is free software: you can redistribute it and/or modify
2 // it under the terms of the GNU General Public License as published by
3 // the Free Software Foundation, version 3 of the License.
4 //
5 // This program is distributed in the hope that it will be useful,
6 // but WITHOUT ANY WARRANTY; without even the implied warranty of
7 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 // GNU General Public License for more details.
9 //
10 // You should have received a copy of the GNU General Public License
11 // along with this program. If not, see <http://www.gnu.org/licenses/>.
12 
13 #ifndef _EXTRACT_STRINGS_H_
14 #define _EXTRACT_STRINGS_H_
15 #include "HfstSymbolDefs.h"
16 #include <string>
17 #include <vector>
18 #include <iostream>
19 #include <sstream>
20 #include <set>
21 
22 /* @file HfstExtractStrings.h
23  \brief Classes WeightedPath and WeightsPaths */
24 
25 namespace hfst {
26 
27  typedef std::vector<std::pair<std::string,std::string> > StringPairVector;
28  typedef std::pair<float, StringPairVector> HfstTwoLevelPath;
29  typedef std::set<HfstTwoLevelPath> HfstTwoLevelPaths;
30 
31  using std::stringstream;
32  using std::ios;
33 
34  /* \brief (Being replaced by HfstOneLevelPath and HfstTwoLevelPath)
35  A weighted string pair that represents a path in a transducer.
36 
37  @see WeightedPaths
38  @see HfstOneLevelPath
39  @see HfstTransducer::extract_paths
40  */
41 #ifdef FOO
42  template<class W> class WeightedPath
43  {
44  public:
45  /* \brief The input string of the path. */
46  std::string istring;
47  /* \brief The output string of the path. */
48  std::string ostring;
49  /* \brief The weight of the path. */
50  W weight;
51  /* \brief An optional StringPairVector representation of the path.
52 
53  This can be used when we are interested in the exact alignment of
54  symbols in a given path. If you are going to use this variable,
55  set the value of \a is_spv_in_use 'true'. */
56  StringPairVector spv;
57  /* \brief Whether the StringPairVector representation is in use.
58 
59  This variable tells whether we are using the string pair vector
60  representation. By default, it is 'false'. */
61  bool is_spv_in_use;
62 
63  WeightedPath(const std::string &is,const std::string &os,W w)
64  { weight = w; istring = is; ostring = os; is_spv_in_use=false; }
65 
66  bool operator< (const WeightedPath &another) const
67  { if (weight == another.weight)
68  { if (istring == another.istring)
69  { if (ostring == another.ostring)
70  { /* Handle here spv. */
71  if (not is_spv_in_use)
72  return false; /* paths are equivalent */
73  unsigned int common_length
74  = (spv.size()<another.spv.size())?
75  spv.size() : another.spv.size();
76  /* Go through string pairs. */
77  for (unsigned int i=0; i<common_length; i++) {
78  if (spv[i].first == another.spv[i].first)
79  { if (spv[i].second == another.spv[i].second)
80  { continue; }
81  return (spv[i].second < another.spv[i].second); }
82  return (spv[i].first < another.spv[i].first);
83  }
84  /* Shorter path is smaller. */
85  return (spv.size() < another.spv.size());
86  }
87  return ostring < another.ostring; }
88  return istring < another.istring; }
89  return weight < another.weight; }
90 
91  std::string to_string(void) const
92  { stringstream s_stream(ios::out);
93  s_stream << istring << ":" << ostring << "\t" << weight;
94  s_stream.flush();
95  return s_stream.str();
96  }
97 
98  WeightedPath<W> &reverse(void)
99  { for(size_t i = 0; i < (istring.size() / 2); ++i)
100  { char c = istring[i];
101  istring[i] = istring[istring.size() - i - 1];
102  istring[istring.size() - i - 1] = c; }
103 
104  for(size_t i = 0; i < (ostring.size() / 2); ++i)
105  { char c = ostring[i];
106  ostring[i] = ostring[ostring.size() - i - 1];
107  ostring[ostring.size() - i - 1] = c; }
108  return *this; }
109 
110  WeightedPath &add(const WeightedPath &another,bool in_front=true)
111  { if (in_front)
112  {
113  istring = another.istring + istring;
114  ostring = another.ostring + ostring;
115  weight = weight + another.weight;
116  return *this;
117  }
118  else
119  {
120  istring = istring + another.istring;
121  ostring = ostring + another.ostring;
122  weight = weight + another.weight;
123  return *this;
124  }
125  }
126  void operator=(const WeightedPath &another)
127  { if (this == &another) { return; }
128  this->istring = another.istring;
129  this->ostring = another.ostring;
130  this->weight = another.weight; }
131  };
132 
133 
134  /* \brief A class for storing weighted string pairs that represent
135  paths in a transducer.
136 
137  Iterators to Vectors and Sets return paths in descending weight order
138  (the string with the biggest weight is returned first). (check this)
139 
140  For an example, see HfstTransducer::extract_paths.
141 
142  @see HfstTransducer::extract_paths */
143  template<class W> class WeightedPaths
144  { public:
145 
146  /* \brief A vector of weighted string pairs. */
147  typedef std::vector< WeightedPath<W> > Vector;
148  /* \brief A set of weighted string pairs. */
149  typedef std::set< WeightedPath<W> > Set;
150 
151  static void add(Vector &v,WeightedPath<W> &s)
152  {
153  for (typename Vector::iterator it = v.begin(); it != v.end(); ++it)
154  { it->add(s,false); }
155  }
156 
157  static void add(WeightedPath<W> &s,Vector &v)
158  {
159  for (typename Vector::iterator it = v.begin(); it != v.end(); ++it)
160  { it->add(s); }
161  }
162 
163  static void cat(Vector &v, const Vector &another_v)
164  {
165  v.insert(v.end(),another_v.begin(),another_v.end());
166  }
167 
168  static void reverse_strings(Vector &v)
169  {
170  for (typename Vector::iterator it = v.begin(); it != v.end(); ++it)
171  { it->reverse(); }
172  }
173  };
174 #endif
175 
176  class ExtractStringsCb
177  {
178  public:
179  class RetVal
180  {
181  public:
182  bool continueSearch;
183  bool continuePath;
184  RetVal(bool s, bool p): continueSearch(s), continuePath(p) {}
185  void operator=(const RetVal& o)
186  {
187  continueSearch = o.continueSearch;
188  continuePath = o.continuePath;
189  }
190  };
191 
202  //virtual RetVal operator()(WeightedPath<float>& path, bool final) = 0;
203  virtual RetVal operator()(HfstTwoLevelPath& path, bool final) = 0;
204  };
205  }
206 #endif
std::vector< std::pair< std::string, std::string > > StringPairVector
A vector of string pairs.
Definition: HfstDataTypes.h:106
std::set< HfstTwoLevelPath > HfstTwoLevelPaths
A set of two-level weighted paths.
Definition: HfstDataTypes.h:110
std::pair< float, StringPairVector > HfstTwoLevelPath
A path of two level of arcs with collected weight.
Definition: HfstDataTypes.h:108
Typedefs and functions for symbols, symbol pairs and sets of symbols.