Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kpu/kenlm
Browse files Browse the repository at this point in the history
  • Loading branch information
kpu committed Feb 5, 2014
2 parents c09b1cf + 4ffaf80 commit 4b0b019
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 30 deletions.
48 changes: 26 additions & 22 deletions lm/ngram_query.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "lm/enumerate_vocab.hh"
#include "lm/model.hh"
#include "util/file_piece.hh"
#include "util/usage.hh"

#include <cstdlib>
Expand All @@ -16,42 +17,41 @@
namespace lm {
namespace ngram {

template <class Model> void Query(const Model &model, bool sentence_context, std::istream &in_stream, std::ostream &out_stream) {
template <class Model> void Query(const Model &model, bool sentence_context) {
typename Model::State state, out;
lm::FullScoreReturn ret;
std::string word;
StringPiece word;

util::FilePiece in(0);
std::ostream &out_stream = std::cout;

double corpus_total = 0.0;
double corpus_total_oov_only = 0.0;
uint64_t corpus_oov = 0;
uint64_t corpus_tokens = 0;

while (in_stream) {
while (true) {
state = sentence_context ? model.BeginSentenceState() : model.NullContextState();
float total = 0.0;
bool got = false;
uint64_t oov = 0;
while (in_stream >> word) {
got = true;

while (in.ReadWordSameLine(word)) {
lm::WordIndex vocab = model.GetVocabulary().Index(word);
if (vocab == 0) ++oov;
ret = model.FullScore(state, vocab, out);
if (vocab == model.GetVocabulary().NotFound()) {
++oov;
corpus_total_oov_only += ret.prob;
}
total += ret.prob;
out_stream << word << '=' << vocab << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << '\t';
++corpus_tokens;
state = out;
char c;
while (true) {
c = in_stream.get();
if (!in_stream) break;
if (c == '\n') break;
if (!isspace(c)) {
in_stream.unget();
break;
}
}
if (c == '\n') break;
}
if (!got && !in_stream) break;
// 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; }
if (sentence_context) {
ret = model.FullScore(state, model.GetVocabulary().EndSentence(), out);
total += ret.prob;
Expand All @@ -62,13 +62,17 @@ template <class Model> void Query(const Model &model, bool sentence_context, std
corpus_total += total;
corpus_oov += oov;
}
out_stream << "Perplexity " << pow(10.0, -(corpus_total / static_cast<double>(corpus_tokens))) << std::endl;
out_stream <<
"Perplexity including OOVs:\t" << pow(10.0, -(corpus_total / static_cast<double>(corpus_tokens))) << "\n"
"Perplexity excluding OOVs:\t" << pow(10.0, -((corpus_total - corpus_total_oov_only) / static_cast<double>(corpus_tokens - corpus_oov))) << "\n"
"OOVs:\t" << corpus_oov << "\n"
;
}

template <class M> void Query(const char *file, bool sentence_context, std::istream &in_stream, std::ostream &out_stream) {
template <class M> void Query(const char *file, bool sentence_context) {
Config config;
M model(file, config);
Query(model, sentence_context, in_stream, out_stream);
Query(model, sentence_context);
}

} // namespace ngram
Expand Down
16 changes: 8 additions & 8 deletions lm/query_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ int main(int argc, char *argv[]) {
if (RecognizeBinary(file, model_type)) {
switch(model_type) {
case PROBING:
Query<lm::ngram::ProbingModel>(file, sentence_context, std::cin, std::cout);
Query<lm::ngram::ProbingModel>(file, sentence_context);
break;
case REST_PROBING:
Query<lm::ngram::RestProbingModel>(file, sentence_context, std::cin, std::cout);
Query<lm::ngram::RestProbingModel>(file, sentence_context);
break;
case TRIE:
Query<TrieModel>(file, sentence_context, std::cin, std::cout);
Query<TrieModel>(file, sentence_context);
break;
case QUANT_TRIE:
Query<QuantTrieModel>(file, sentence_context, std::cin, std::cout);
Query<QuantTrieModel>(file, sentence_context);
break;
case ARRAY_TRIE:
Query<ArrayTrieModel>(file, sentence_context, std::cin, std::cout);
Query<ArrayTrieModel>(file, sentence_context);
break;
case QUANT_ARRAY_TRIE:
Query<QuantArrayTrieModel>(file, sentence_context, std::cin, std::cout);
Query<QuantArrayTrieModel>(file, sentence_context);
break;
default:
std::cerr << "Unrecognized kenlm model type " << model_type << std::endl;
Expand All @@ -56,10 +56,10 @@ int main(int argc, char *argv[]) {
#ifdef WITH_NPLM
} else if (lm::np::Model::Recognize(file)) {
lm::np::Model model(file);
Query(model, sentence_context, std::cin, std::cout);
Query(model, sentence_context);
#endif
} else {
Query<ProbingModel>(file, sentence_context, std::cin, std::cout);
Query<ProbingModel>(file, sentence_context);
}
std::cerr << "Total time including destruction:\n";
util::PrintUsage(std::cerr);
Expand Down
1 change: 1 addition & 0 deletions util/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ obj file_piece_test.o : file_piece_test.cc /top//boost_unit_test_framework : $(c
fakelib kenutil : bit_packing.cc ersatz_progress.cc exception.cc file.cc file_piece.cc mmap.cc murmur_hash.cc pool.cc read_compressed scoped.cc string_piece.cc usage.cc double-conversion//double-conversion : <include>.. <os>LINUX,<threading>single:<source>rt : : <include>.. ;

exe cat_compressed : cat_compressed_main.cc kenutil ;
exe file_piece_cat : file_piece_main.cc kenutil ;

alias programs : cat_compressed ;

Expand Down
7 changes: 7 additions & 0 deletions util/file_piece.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ StringPiece FilePiece::ReadLine(char delim) {
}
}

bool FilePiece::ReadLineOrEOF(StringPiece &to, char delim) {
try {
to = ReadLine(delim);
} catch (const util::EndOfFileException &e) { return false; }
return true;
}

float FilePiece::ReadFloat() {
return ReadNumber<float>();
}
Expand Down
23 changes: 23 additions & 0 deletions util/file_piece.hh
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,33 @@ class FilePiece {
return Consume(FindDelimiterOrEOF(delim));
}

// Read word until the line or file ends.
bool ReadWordSameLine(StringPiece &to, const bool *delim = kSpaces) {
assert(delim[static_cast<unsigned char>('\n')]);
// Skip non-enter spaces.
for (; ; ++position_) {
if (position_ == position_end_) {
try {
Shift();
} catch (const util::EndOfFileException &e) { return false; }
// And break out at end of file.
if (position_ == position_end_) return false;
}
if (!delim[static_cast<unsigned char>(*position_)]) break;
if (*position_ == '\n') return false;
}
// We can't be at the end of file because there's at least one character open.
to = Consume(FindDelimiterOrEOF(delim));
return true;
}

// Unlike ReadDelimited, this includes leading spaces and consumes the delimiter.
// It is similar to getline in that way.
StringPiece ReadLine(char delim = '\n');

// Doesn't throw EndOfFileException, just returns false.
bool ReadLineOrEOF(StringPiece &to, char delim = '\n');

float ReadFloat();
double ReadDouble();
long int ReadLong();
Expand Down

0 comments on commit 4b0b019

Please sign in to comment.