scorer.h 2.3 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

Y
Yibing Liu 已提交
21
// Implement a callback to retrive string vocabulary.
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:
Y
Yibing Liu 已提交
43
  Scorer(double alpha, double beta, const std::string &lm_path);
Y
Yibing Liu 已提交
44
  ~Scorer();
45

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

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

Y
Yibing Liu 已提交
50
  size_t get_max_order() { return _max_order; }
51

Y
Yibing Liu 已提交
52
  bool is_char_map_empty() { return _char_map.size() == 0; }
53

Y
Yibing Liu 已提交
54
  bool is_character_based() { return _is_character_based; }
55

Y
Yibing Liu 已提交
56 57
  // reset params alpha & beta
  void reset_params(float alpha, float beta);
58

Y
Yibing Liu 已提交
59
  // make ngram
Y
Yibing Liu 已提交
60
  std::vector<std::string> make_ngram(PathTrie *prefix);
61

Y
Yibing Liu 已提交
62 63
  // fill dictionary for fst
  void fill_dictionary(bool add_space);
64

Y
Yibing Liu 已提交
65
  // set char map
Y
Yibing Liu 已提交
66
  void set_char_map(const std::vector<std::string> &char_list);
67

Y
Yibing Liu 已提交
68
  std::vector<std::string> split_labels(const std::vector<int> &labels);
69

Y
Yibing Liu 已提交
70 71 72
  // expose to decoder
  double alpha;
  double beta;
73

Y
Yibing Liu 已提交
74
  // fst dictionary
Y
Yibing Liu 已提交
75
  void *dictionary;
76

77
protected:
Y
Yibing Liu 已提交
78
  void load_LM(const char *filename);
79

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

Y
Yibing Liu 已提交
82
  std::string vec2str(const std::vector<int> &input);
83 84

private:
Y
Yibing Liu 已提交
85
  void *_language_model;
Y
Yibing Liu 已提交
86 87
  bool _is_character_based;
  size_t _max_order;
88

Y
Yibing Liu 已提交
89 90 91
  int _SPACE_ID;
  std::vector<std::string> _char_list;
  std::unordered_map<char, int> _char_map;
92

Y
Yibing Liu 已提交
93
  std::vector<std::string> _vocabulary;
Y
Yibing Liu 已提交
94 95
};

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