diff options
author | Kenneth Heafield <github@kheafield.com> | 2012-01-14 21:07:31 +0400 |
---|---|---|
committer | Kenneth Heafield <github@kheafield.com> | 2012-01-14 21:07:31 +0400 |
commit | d49cc6c9f8305b014ce00ee21e31abab83d4eeab (patch) | |
tree | 1ed8339ac61368b76e7427829a946a424e0038ae /lm | |
parent | 23c79b81693e883172737c5ea7ee02112f14174b (diff) |
Make lm constructor explicit
Diffstat (limited to 'lm')
-rw-r--r-- | lm/model.hh | 2 | ||||
-rw-r--r-- | lm/ngram_query.cc | 27 | ||||
-rw-r--r-- | lm/ngram_query.hh | 34 |
3 files changed, 38 insertions, 25 deletions
diff --git a/lm/model.hh b/lm/model.hh index 731d60b7e..6ea62a789 100644 --- a/lm/model.hh +++ b/lm/model.hh @@ -90,7 +90,7 @@ template <class Search, class VocabularyT> class GenericModel : public base::Mod * TrieModel. To classify binary files, call RecognizeBinary in * lm/binary_format.hh. */ - GenericModel(const char *file, const Config &config = Config()); + explicit GenericModel(const char *file, const Config &config = Config()); /* Score p(new_word | in_state) and incorporate new_word into out_state. * Note that in_state and out_state must be different references: diff --git a/lm/ngram_query.cc b/lm/ngram_query.cc index 3603cf25a..8f7a0e1cc 100644 --- a/lm/ngram_query.cc +++ b/lm/ngram_query.cc @@ -8,31 +8,32 @@ int main(int argc, char *argv[]) { } try { bool sentence_context = (argc == 2); - lm::ngram::ModelType model_type; - if (lm::ngram::RecognizeBinary(argv[1], model_type)) { + using namespace lm::ngram; + ModelType model_type; + if (RecognizeBinary(argv[1], model_type)) { switch(model_type) { - case lm::ngram::HASH_PROBING: + case HASH_PROBING: Query<lm::ngram::ProbingModel>(argv[1], sentence_context, std::cin, std::cout); break; - case lm::ngram::TRIE_SORTED: - Query<lm::ngram::TrieModel>(argv[1], sentence_context, std::cin, std::cout); + case TRIE_SORTED: + Query<TrieModel>(argv[1], sentence_context, std::cin, std::cout); break; - case lm::ngram::QUANT_TRIE_SORTED: - Query<lm::ngram::QuantTrieModel>(argv[1], sentence_context, std::cin, std::cout); + case QUANT_TRIE_SORTED: + Query<QuantTrieModel>(argv[1], sentence_context, std::cin, std::cout); break; - case lm::ngram::ARRAY_TRIE_SORTED: - Query<lm::ngram::ArrayTrieModel>(argv[1], sentence_context, std::cin, std::cout); + case ARRAY_TRIE_SORTED: + Query<ArrayTrieModel>(argv[1], sentence_context, std::cin, std::cout); break; - case lm::ngram::QUANT_ARRAY_TRIE_SORTED: - Query<lm::ngram::QuantArrayTrieModel>(argv[1], sentence_context, std::cin, std::cout); + case QUANT_ARRAY_TRIE_SORTED: + Query<QuantArrayTrieModel>(argv[1], sentence_context, std::cin, std::cout); break; - case lm::ngram::HASH_SORTED: + case HASH_SORTED: default: std::cerr << "Unrecognized kenlm model type " << model_type << std::endl; abort(); } } else { - Query<lm::ngram::ProbingModel>(argv[1], sentence_context, std::cin, std::cout); + Query<ProbingModel>(argv[1], sentence_context, std::cin, std::cout); } PrintUsage("Total time including destruction:\n"); diff --git a/lm/ngram_query.hh b/lm/ngram_query.hh index 6bf6ec48b..4990df226 100644 --- a/lm/ngram_query.hh +++ b/lm/ngram_query.hh @@ -15,6 +15,9 @@ #include <sys/time.h> #endif +namespace lm { +namespace ngram { + #if !defined(_WIN32) && !defined(_WIN64) float FloatSec(const struct timeval &tv) { return static_cast<float>(tv.tv_sec) + (static_cast<float>(tv.tv_usec) / 1000000000.0); @@ -43,48 +46,57 @@ void PrintUsage(const char *message) { #endif } -template <class Model> void Query(const Model &model, bool sentence_context, std::istream &inStream, std::ostream &outStream) { +template <class Model> void Query(const Model &model, bool sentence_context, std::istream &in_stream, std::ostream &out_stream) { PrintUsage("Loading statistics:\n"); typename Model::State state, out; lm::FullScoreReturn ret; std::string word; - while (inStream) { + while (in_stream) { state = sentence_context ? model.BeginSentenceState() : model.NullContextState(); float total = 0.0; bool got = false; unsigned int oov = 0; - while (inStream >> word) { + while (in_stream >> word) { got = true; lm::WordIndex vocab = model.GetVocabulary().Index(word); if (vocab == 0) ++oov; ret = model.FullScore(state, vocab, out); total += ret.prob; - outStream << word << '=' << vocab << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << '\t'; + out_stream << word << '=' << vocab << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << '\t'; state = out; char c; while (true) { - c = inStream.get(); - if (!inStream) break; + c = in_stream.get(); + if (!in_stream) break; if (c == '\n') break; if (!isspace(c)) { - inStream.unget(); + in_stream.unget(); break; } } if (c == '\n') break; } - if (!got && !inStream) break; + if (!got && !in_stream) break; if (sentence_context) { ret = model.FullScore(state, model.GetVocabulary().EndSentence(), out); total += ret.prob; - outStream << "</s>=" << model.GetVocabulary().EndSentence() << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << '\t'; + out_stream << "</s>=" << model.GetVocabulary().EndSentence() << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << '\t'; } - outStream << "Total: " << total << " OOV: " << oov << '\n'; - } + out_stream << "Total: " << total << " OOV: " << oov << '\n'; + } PrintUsage("After queries:\n"); } +template <class M> void Query(const char *file, bool sentence_context, std::istream &in_stream, std::ostream &out_stream) { + Config config; +// config.load_method = util::LAZY; + M model(file, config); + Query(model, sentence_context, in_stream, out_stream); +} + +} // namespace ngram +} // namespace lm #endif // LM_NGRAM_QUERY__ |