You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.3 KiB
97 lines
2.3 KiB
8 years ago
|
#ifndef SCORER_H_
|
||
|
#define SCORER_H_
|
||
|
|
||
7 years ago
|
#include <memory>
|
||
7 years ago
|
#include <string>
|
||
7 years ago
|
#include <unordered_map>
|
||
7 years ago
|
#include <vector>
|
||
7 years ago
|
|
||
7 years ago
|
#include "lm/enumerate_vocab.hh"
|
||
|
#include "lm/virtual_interface.hh"
|
||
7 years ago
|
#include "lm/word_index.hh"
|
||
|
#include "util/string_piece.hh"
|
||
8 years ago
|
|
||
7 years ago
|
#include "path_trie.h"
|
||
|
|
||
7 years ago
|
const double OOV_SCORE = -1000.0;
|
||
7 years ago
|
const std::string START_TOKEN = "<s>";
|
||
|
const std::string UNK_TOKEN = "<unk>";
|
||
|
const std::string END_TOKEN = "</s>";
|
||
8 years ago
|
|
||
7 years ago
|
// Implement a callback to retrive string vocabulary.
|
||
7 years ago
|
class RetriveStrEnumerateVocab : public lm::EnumerateVocab {
|
||
|
public:
|
||
7 years ago
|
RetriveStrEnumerateVocab() {}
|
||
8 years ago
|
|
||
7 years ago
|
void Add(lm::WordIndex index, const StringPiece &str) {
|
||
7 years ago
|
vocabulary.push_back(std::string(str.data(), str.length()));
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
std::vector<std::string> vocabulary;
|
||
7 years ago
|
};
|
||
8 years ago
|
|
||
7 years ago
|
/* External scorer to query score for n-gram or sentence, including language
|
||
|
* model scoring and word insertion.
|
||
7 years ago
|
*
|
||
|
* 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" });
|
||
|
*/
|
||
7 years ago
|
class Scorer {
|
||
8 years ago
|
public:
|
||
7 years ago
|
Scorer(double alpha, double beta, const std::string &lm_path);
|
||
7 years ago
|
~Scorer();
|
||
7 years ago
|
|
||
7 years ago
|
double get_log_cond_prob(const std::vector<std::string> &words);
|
||
7 years ago
|
|
||
7 years ago
|
double get_sent_log_prob(const std::vector<std::string> &words);
|
||
7 years ago
|
|
||
7 years ago
|
size_t get_max_order() { return _max_order; }
|
||
7 years ago
|
|
||
7 years ago
|
bool is_char_map_empty() { return _char_map.size() == 0; }
|
||
7 years ago
|
|
||
7 years ago
|
bool is_character_based() { return _is_character_based; }
|
||
7 years ago
|
|
||
7 years ago
|
// reset params alpha & beta
|
||
|
void reset_params(float alpha, float beta);
|
||
7 years ago
|
|
||
7 years ago
|
// make ngram
|
||
7 years ago
|
std::vector<std::string> make_ngram(PathTrie *prefix);
|
||
7 years ago
|
|
||
7 years ago
|
// fill dictionary for fst
|
||
|
void fill_dictionary(bool add_space);
|
||
7 years ago
|
|
||
7 years ago
|
// set char map
|
||
7 years ago
|
void set_char_map(const std::vector<std::string> &char_list);
|
||
7 years ago
|
|
||
7 years ago
|
std::vector<std::string> split_labels(const std::vector<int> &labels);
|
||
7 years ago
|
|
||
7 years ago
|
// expose to decoder
|
||
|
double alpha;
|
||
|
double beta;
|
||
7 years ago
|
|
||
7 years ago
|
// fst dictionary
|
||
7 years ago
|
void *dictionary;
|
||
8 years ago
|
|
||
7 years ago
|
protected:
|
||
7 years ago
|
void load_LM(const char *filename);
|
||
7 years ago
|
|
||
7 years ago
|
double get_log_prob(const std::vector<std::string> &words);
|
||
7 years ago
|
|
||
7 years ago
|
std::string vec2str(const std::vector<int> &input);
|
||
7 years ago
|
|
||
|
private:
|
||
7 years ago
|
void *_language_model;
|
||
7 years ago
|
bool _is_character_based;
|
||
|
size_t _max_order;
|
||
7 years ago
|
|
||
7 years ago
|
int _SPACE_ID;
|
||
|
std::vector<std::string> _char_list;
|
||
|
std::unordered_map<char, int> _char_map;
|
||
7 years ago
|
|
||
7 years ago
|
std::vector<std::string> _vocabulary;
|
||
8 years ago
|
};
|
||
|
|
||
7 years ago
|
#endif // SCORER_H_
|