path_trie.h 1.6 KB
Newer Older
1 2 3
#ifndef PATH_TRIE_H
#define PATH_TRIE_H
#pragma once
Y
Yibing Liu 已提交
4
#include <fst/fstlib.h>
5 6 7 8 9 10 11 12
#include <algorithm>
#include <limits>
#include <memory>
#include <utility>
#include <vector>

using FSTMATCH = fst::SortedMatcher<fst::StdVectorFst>;

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

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

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

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

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

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

Y
Yibing Liu 已提交
38
  void set_matcher(std::shared_ptr<FSTMATCH> matcher);
39

Y
Yibing Liu 已提交
40
  bool is_empty() { return _ROOT == character; }
41

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

Y
Yibing Liu 已提交
45 46 47 48 49 50 51 52
  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;
53

Y
Yibing Liu 已提交
54 55 56
private:
  int _ROOT;
  bool _exists;
57
  bool _has_dictionary;
58

Y
Yibing Liu 已提交
59
  std::vector<std::pair<int, PathTrie*>> _children;
60

Y
Yibing Liu 已提交
61
  // pointer to dictionary of FST
Y
Yibing Liu 已提交
62 63
  fst::StdVectorFst* _dictionary;
  fst::StdVectorFst::StateId _dictionary_state;
Y
Yibing Liu 已提交
64
  // true if finding ars in FST
Y
Yibing Liu 已提交
65
  std::shared_ptr<FSTMATCH> _matcher;
66 67
};

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