2011-12-20 13:44:45 +04:00
|
|
|
#ifndef LM_NGRAM_QUERY__
|
|
|
|
#define LM_NGRAM_QUERY__
|
|
|
|
|
|
|
|
#include "lm/enumerate_vocab.hh"
|
|
|
|
#include "lm/model.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 {
|
|
|
|
|
|
|
|
template <class Model> void Query(const Model &model, bool sentence_context, std::istream &in_stream, std::ostream &out_stream) {
|
2011-12-20 13:44:45 +04:00
|
|
|
typename Model::State state, out;
|
|
|
|
lm::FullScoreReturn ret;
|
|
|
|
std::string word;
|
|
|
|
|
2014-01-02 01:19:06 +04:00
|
|
|
double corpus_total = 0.0;
|
|
|
|
uint64_t corpus_oov = 0;
|
|
|
|
uint64_t corpus_tokens = 0;
|
|
|
|
|
2012-01-14 21:07:31 +04:00
|
|
|
while (in_stream) {
|
2011-12-20 13:44:45 +04:00
|
|
|
state = sentence_context ? model.BeginSentenceState() : model.NullContextState();
|
|
|
|
float total = 0.0;
|
|
|
|
bool got = false;
|
2014-01-02 01:19:06 +04:00
|
|
|
uint64_t oov = 0;
|
2012-01-14 21:07:31 +04:00
|
|
|
while (in_stream >> word) {
|
2011-12-20 13:44:45 +04:00
|
|
|
got = true;
|
|
|
|
lm::WordIndex vocab = model.GetVocabulary().Index(word);
|
|
|
|
if (vocab == 0) ++oov;
|
|
|
|
ret = model.FullScore(state, vocab, out);
|
|
|
|
total += ret.prob;
|
2012-01-14 21:07:31 +04:00
|
|
|
out_stream << word << '=' << vocab << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << '\t';
|
2014-01-02 01:19:06 +04:00
|
|
|
++corpus_tokens;
|
2011-12-20 13:44:45 +04:00
|
|
|
state = out;
|
|
|
|
char c;
|
|
|
|
while (true) {
|
2012-01-14 21:07:31 +04:00
|
|
|
c = in_stream.get();
|
|
|
|
if (!in_stream) break;
|
2011-12-20 13:44:45 +04:00
|
|
|
if (c == '\n') break;
|
|
|
|
if (!isspace(c)) {
|
2012-01-14 21:07:31 +04:00
|
|
|
in_stream.unget();
|
2011-12-20 13:44:45 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
2012-01-14 21:07:31 +04:00
|
|
|
if (!got && !in_stream) 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;
|
2012-01-14 21:07:31 +04:00
|
|
|
out_stream << "</s>=" << model.GetVocabulary().EndSentence() << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << '\t';
|
2011-12-20 13:44:45 +04:00
|
|
|
}
|
2012-01-14 21:07:31 +04:00
|
|
|
out_stream << "Total: " << total << " OOV: " << oov << '\n';
|
2014-01-02 01:19:06 +04:00
|
|
|
corpus_total += total;
|
|
|
|
corpus_oov += oov;
|
2012-06-28 18:58:59 +04:00
|
|
|
}
|
2014-01-02 01:19:06 +04:00
|
|
|
out_stream << "Perplexity " << pow(10.0, -(corpus_total / static_cast<double>(corpus_tokens))) << std::endl;
|
2011-12-20 13:44:45 +04:00
|
|
|
}
|
|
|
|
|
2012-01-14 21:07:31 +04:00
|
|
|
template <class M> void Query(const char *file, bool sentence_context, std::istream &in_stream, std::ostream &out_stream) {
|
|
|
|
Config config;
|
|
|
|
M model(file, config);
|
|
|
|
Query(model, sentence_context, in_stream, out_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ngram
|
|
|
|
} // namespace lm
|
2011-12-20 13:44:45 +04:00
|
|
|
|
|
|
|
#endif // LM_NGRAM_QUERY__
|
|
|
|
|
|
|
|
|