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