path_trie.h 1.6 KB
Newer Older
1 2
#ifndef PATH_TRIE_H
#define PATH_TRIE_H
3

4 5 6 7 8 9
#include <algorithm>
#include <limits>
#include <memory>
#include <utility>
#include <vector>

10
#include "fst/fstlib.h"
11

Y
Yibing Liu 已提交
12 13 14
/* Trie tree for prefix storing and manipulating, with a dictionary in
 * finite-state transducer for spelling correction.
 */
15 16
class PathTrie {
public:
Y
Yibing Liu 已提交
17 18
  PathTrie();
  ~PathTrie();
19

Y
Yibing Liu 已提交
20
  // get new prefix after appending new char
Y
Yibing Liu 已提交
21
  PathTrie* get_path_trie(int new_char, bool reset = true);
22

Y
Yibing Liu 已提交
23
  // get the prefix in index from root to current node
Y
Yibing Liu 已提交
24
  PathTrie* get_path_vec(std::vector<int>& output);
25

Y
Yibing Liu 已提交
26
  // get the prefix in index from some stop node to current nodel
Y
Yibing Liu 已提交
27 28 29
  PathTrie* get_path_vec(std::vector<int>& output,
                         int stop,
                         size_t max_steps = std::numeric_limits<size_t>::max());
30

Y
Yibing Liu 已提交
31
  // update log probs
Y
Yibing Liu 已提交
32
  void iterate_to_vec(std::vector<PathTrie*>& output);
33

Y
Yibing Liu 已提交
34
  // set dictionary for FST
Y
Yibing Liu 已提交
35
  void set_dictionary(fst::StdVectorFst* dictionary);
36

37
  void set_matcher(std::shared_ptr<fst::SortedMatcher<fst::StdVectorFst>>);
38

39
  bool is_empty() { return ROOT_ == character; }
40

Y
Yibing Liu 已提交
41
  // remove current path from root
Y
Yibing Liu 已提交
42
  void remove();
43

Y
Yibing Liu 已提交
44 45 46 47 48 49 50 51
  float log_prob_b_prev;
  float log_prob_nb_prev;
  float log_prob_b_cur;
  float log_prob_nb_cur;
  float score;
  float approx_ctc;
  int character;
  PathTrie* parent;
52

Y
Yibing Liu 已提交
53
private:
54 55 56
  int ROOT_;
  bool exists_;
  bool has_dictionary_;
57

58
  std::vector<std::pair<int, PathTrie*>> children_;
59

Y
Yibing Liu 已提交
60
  // pointer to dictionary of FST
61 62
  fst::StdVectorFst* dictionary_;
  fst::StdVectorFst::StateId dictionary_state_;
Y
Yibing Liu 已提交
63
  // true if finding ars in FST
64
  std::shared_ptr<fst::SortedMatcher<fst::StdVectorFst>> matcher_;
65 66
};

Y
Yibing Liu 已提交
67
#endif  // PATH_TRIE_H