2014-06-02 21:28:02 +04:00
|
|
|
#ifndef LM_NGRAM_QUERY_H
|
|
|
|
#define LM_NGRAM_QUERY_H
|
2011-12-20 13:44:45 +04:00
|
|
|
|
|
|
|
#include "lm/enumerate_vocab.hh"
|
|
|
|
#include "lm/model.hh"
|
2014-06-02 21:28:02 +04:00
|
|
|
#include "util/file_piece.hh"
|
2012-06-28 18:58:59 +04:00
|
|
|
#include "util/usage.hh"
|
2011-12-20 13:44:45 +04:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2012-06-28 18:58:59 +04:00
|
|
|
#include <ostream>
|
|
|
|
#include <istream>
|
2011-12-20 13:44:45 +04:00
|
|
|
#include <string>
|
|
|
|
|
2014-01-02 01:19:06 +04:00
|
|
|
#include <math.h>
|
|
|
|
|
2012-01-14 21:07:31 +04:00
|
|
|
namespace lm {
|
|
|
|
namespace ngram {
|
|
|
|
|
2014-06-02 21:28:02 +04:00
|
|
|
struct BasicPrint {
|
|
|
|
void Word(StringPiece, WordIndex, const FullScoreReturn &) const {}
|
|
|
|
void Line(uint64_t oov, float total) const {
|
|
|
|
std::cout << "Total: " << total << " OOV: " << oov << '\n';
|
|
|
|
}
|
|
|
|
void Summary(double, double, uint64_t, uint64_t) {}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FullPrint : public BasicPrint {
|
|
|
|
void Word(StringPiece surface, WordIndex vocab, const FullScoreReturn &ret) const {
|
|
|
|
std::cout << surface << '=' << vocab << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << '\t';
|
|
|
|
}
|
|
|
|
|
|
|
|
void Summary(double ppl_including_oov, double ppl_excluding_oov, uint64_t corpus_oov, uint64_t corpus_tokens) {
|
|
|
|
std::cout <<
|
|
|
|
"Perplexity including OOVs:\t" << ppl_including_oov << "\n"
|
|
|
|
"Perplexity excluding OOVs:\t" << ppl_excluding_oov << "\n"
|
|
|
|
"OOVs:\t" << corpus_oov << "\n"
|
|
|
|
"Tokenss:\t" << corpus_tokens << '\n'
|
|
|
|
;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Model, class Printer> void Query(const Model &model, bool sentence_context) {
|
|
|
|
Printer printer;
|
2011-12-20 13:44:45 +04:00
|
|
|
typename Model::State state, out;
|
|
|
|
lm::FullScoreReturn ret;
|
2014-06-02 21:28:02 +04:00
|
|
|
StringPiece word;
|
|
|
|
|
|
|
|
util::FilePiece in(0);
|
2011-12-20 13:44:45 +04:00
|
|
|
|
2014-01-02 01:19:06 +04:00
|
|
|
double corpus_total = 0.0;
|
2014-06-02 21:28:02 +04:00
|
|
|
double corpus_total_oov_only = 0.0;
|
2014-01-02 01:19:06 +04:00
|
|
|
uint64_t corpus_oov = 0;
|
|
|
|
uint64_t corpus_tokens = 0;
|
|
|
|
|
2014-06-02 21:28:02 +04:00
|
|
|
while (true) {
|
2011-12-20 13:44:45 +04:00
|
|
|
state = sentence_context ? model.BeginSentenceState() : model.NullContextState();
|
|
|
|
float total = 0.0;
|
2014-01-02 01:19:06 +04:00
|
|
|
uint64_t oov = 0;
|
2014-06-02 21:28:02 +04:00
|
|
|
|
|
|
|
while (in.ReadWordSameLine(word)) {
|
2011-12-20 13:44:45 +04:00
|
|
|
lm::WordIndex vocab = model.GetVocabulary().Index(word);
|
|
|
|
ret = model.FullScore(state, vocab, out);
|
2014-06-02 21:28:02 +04:00
|
|
|
if (vocab == model.GetVocabulary().NotFound()) {
|
|
|
|
++oov;
|
|
|
|
corpus_total_oov_only += ret.prob;
|
|
|
|
}
|
2011-12-20 13:44:45 +04:00
|
|
|
total += ret.prob;
|
2014-06-02 21:28:02 +04:00
|
|
|
printer.Word(word, vocab, ret);
|
2014-01-02 01:19:06 +04:00
|
|
|
++corpus_tokens;
|
2011-12-20 13:44:45 +04:00
|
|
|
state = out;
|
|
|
|
}
|
2014-06-02 21:28:02 +04:00
|
|
|
// If people don't have a newline after their last query, this won't add a </s>.
|
|
|
|
// Sue me.
|
|
|
|
try {
|
|
|
|
UTIL_THROW_IF('\n' != in.get(), util::Exception, "FilePiece is confused.");
|
|
|
|
} catch (const util::EndOfFileException &e) { break; }
|
2011-12-20 13:44:45 +04:00
|
|
|
if (sentence_context) {
|
|
|
|
ret = model.FullScore(state, model.GetVocabulary().EndSentence(), out);
|
|
|
|
total += ret.prob;
|
2014-01-02 01:19:06 +04:00
|
|
|
++corpus_tokens;
|
2014-06-02 21:28:02 +04:00
|
|
|
printer.Word("</s>", model.GetVocabulary().EndSentence(), ret);
|
2011-12-20 13:44:45 +04:00
|
|
|
}
|
2014-06-02 21:28:02 +04:00
|
|
|
printer.Line(oov, total);
|
2014-01-02 01:19:06 +04:00
|
|
|
corpus_total += total;
|
|
|
|
corpus_oov += oov;
|
2012-06-28 18:58:59 +04:00
|
|
|
}
|
2014-06-02 21:28:02 +04:00
|
|
|
printer.Summary(
|
|
|
|
pow(10.0, -(corpus_total / static_cast<double>(corpus_tokens))), // PPL including OOVs
|
|
|
|
pow(10.0, -((corpus_total - corpus_total_oov_only) / static_cast<double>(corpus_tokens - corpus_oov))), // PPL excluding OOVs
|
|
|
|
corpus_oov,
|
|
|
|
corpus_tokens);
|
2011-12-20 13:44:45 +04:00
|
|
|
}
|
|
|
|
|
2014-06-02 21:28:02 +04:00
|
|
|
template <class Model> void Query(const char *file, const Config &config, bool sentence_context, bool show_words) {
|
|
|
|
Model model(file, config);
|
|
|
|
if (show_words) {
|
|
|
|
Query<Model, FullPrint>(model, sentence_context);
|
|
|
|
} else {
|
|
|
|
Query<Model, BasicPrint>(model, sentence_context);
|
|
|
|
}
|
2012-01-14 21:07:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ngram
|
|
|
|
} // namespace lm
|
2011-12-20 13:44:45 +04:00
|
|
|
|
2014-06-02 21:28:02 +04:00
|
|
|
#endif // LM_NGRAM_QUERY_H
|
2011-12-20 13:44:45 +04:00
|
|
|
|
|
|
|
|