scorer.h 3.0 KB
Newer Older
Y
Yibing Liu 已提交
1 2 3
#ifndef SCORER_H_
#define SCORER_H_

4
#include <memory>
Y
Yibing Liu 已提交
5
#include <string>
6
#include <unordered_map>
Y
Yibing Liu 已提交
7
#include <vector>
8

9 10
#include "lm/enumerate_vocab.hh"
#include "lm/virtual_interface.hh"
Y
Yibing Liu 已提交
11 12
#include "lm/word_index.hh"
#include "util/string_piece.hh"
Y
Yibing Liu 已提交
13

14 15
#include "path_trie.h"

Y
Yibing Liu 已提交
16
const double OOV_SCORE = -1000.0;
17 18 19
const std::string START_TOKEN = "<s>";
const std::string UNK_TOKEN = "<unk>";
const std::string END_TOKEN = "</s>";
Y
Yibing Liu 已提交
20

21
// Implement a callback to retrive the dictionary of language model.
22 23
class RetriveStrEnumerateVocab : public lm::EnumerateVocab {
public:
Y
Yibing Liu 已提交
24
  RetriveStrEnumerateVocab() {}
Y
Yibing Liu 已提交
25

Y
Yibing Liu 已提交
26
  void Add(lm::WordIndex index, const StringPiece &str) {
Y
Yibing Liu 已提交
27 28
    vocabulary.push_back(std::string(str.data(), str.length()));
  }
29

Y
Yibing Liu 已提交
30
  std::vector<std::string> vocabulary;
31
};
32

Y
Yibing Liu 已提交
33 34
/* External scorer to query score for n-gram or sentence, including language
 * model scoring and word insertion.
35 36 37 38 39 40
 *
 * Example:
 *     Scorer scorer(alpha, beta, "path_of_language_model");
 *     scorer.get_log_cond_prob({ "WORD1", "WORD2", "WORD3" });
 *     scorer.get_sent_log_prob({ "WORD1", "WORD2", "WORD3" });
 */
Y
Yibing Liu 已提交
41
class Scorer {
Y
Yibing Liu 已提交
42
public:
43 44 45 46
  Scorer(double alpha,
         double beta,
         const std::string &lm_path,
         const std::vector<std::string> &vocabulary);
Y
Yibing Liu 已提交
47
  ~Scorer();
48

Y
Yibing Liu 已提交
49
  double get_log_cond_prob(const std::vector<std::string> &words);
50

Y
Yibing Liu 已提交
51
  double get_sent_log_prob(const std::vector<std::string> &words);
52

53 54
  // return the max order
  size_t get_max_order() const { return max_order_; }
55

56 57
  // return the dictionary size of language model
  size_t get_dict_size() const { return dict_size_; }
58

59 60
  // retrun true if the language model is character based
  bool is_character_based() const { return is_character_based_; }
61

Y
Yibing Liu 已提交
62 63
  // reset params alpha & beta
  void reset_params(float alpha, float beta);
64

65
  // make ngram for a given prefix
Y
Yibing Liu 已提交
66
  std::vector<std::string> make_ngram(PathTrie *prefix);
67

68 69
  // trransform the labels in index to the vector of words (word based lm) or
  // the vector of characters (character based lm)
Y
Yibing Liu 已提交
70
  std::vector<std::string> split_labels(const std::vector<int> &labels);
71

72
  // language model weight
Y
Yibing Liu 已提交
73
  double alpha;
74
  // word insertion weight
Y
Yibing Liu 已提交
75
  double beta;
76

77
  // pointer to the dictionary of FST
Y
Yibing Liu 已提交
78
  void *dictionary;
79

80
protected:
81
  // necessary setup: load language model, set char map, fill FST's dictionary
82 83 84
  void setup(const std::string &lm_path,
             const std::vector<std::string> &vocab_list);

85
  // load language model from given path
86 87
  void load_lm(const std::string &lm_path);

88
  // fill dictionary for FST
89 90 91 92
  void fill_dictionary(bool add_space);

  // set char map
  void set_char_map(const std::vector<std::string> &char_list);
93

Y
Yibing Liu 已提交
94
  double get_log_prob(const std::vector<std::string> &words);
95

96
  // translate the vector in index to string
Y
Yibing Liu 已提交
97
  std::string vec2str(const std::vector<int> &input);
98 99

private:
100 101 102 103
  void *language_model_;
  bool is_character_based_;
  size_t max_order_;
  size_t dict_size_;
104

105 106 107
  int SPACE_ID_;
  std::vector<std::string> char_list_;
  std::unordered_map<char, int> char_map_;
108

109
  std::vector<std::string> vocabulary_;
Y
Yibing Liu 已提交
110 111
};

Y
Yibing Liu 已提交
112
#endif  // SCORER_H_