00001 #include "lm/ngram_query.hh"
00002 #include "util/getopt.hh"
00003
00004 #ifdef WITH_NPLM
00005 #include "lm/wrappers/nplm.hh"
00006 #endif
00007
00008 #include <stdlib.h>
00009
00010 void Usage(const char *name) {
00011 std::cerr <<
00012 "KenLM was compiled with maximum order " << KENLM_MAX_ORDER << ".\n"
00013 "Usage: " << name << " [-b] [-n] [-w] [-s] lm_file\n"
00014 "-b: Do not buffer output.\n"
00015 "-n: Do not wrap the input in <s> and </s>.\n"
00016 "-v summary|sentence|word: Level of verbosity\n"
00017 "-l lazy|populate|read|parallel: Load lazily, with populate, or malloc+read\n"
00018 "The default loading method is populate on Linux and read on others.\n";
00019 exit(1);
00020 }
00021
00022 int main(int argc, char *argv[]) {
00023 if (argc == 1 || (argc == 2 && !strcmp(argv[1], "--help")))
00024 Usage(argv[0]);
00025
00026 lm::ngram::Config config;
00027 bool sentence_context = true;
00028 unsigned int verbosity = 2;
00029 bool flush = false;
00030
00031 int opt;
00032 while ((opt = getopt(argc, argv, "bnv:l:")) != -1) {
00033 switch (opt) {
00034 case 'b':
00035 flush = true;
00036 break;
00037 case 'n':
00038 sentence_context = false;
00039 break;
00040 case 'v':
00041 if (!strcmp(optarg, "word") || !strcmp(optarg, "2")) {
00042 verbosity = 2;
00043 } else if (!strcmp(optarg, "sentence") || !strcmp(optarg, "1")) {
00044 verbosity = 1;
00045 } else if (!strcmp(optarg, "summary") || !strcmp(optarg, "0")) {
00046 verbosity = 0;
00047 } else {
00048 Usage(argv[0]);
00049 }
00050 break;
00051 case 'l':
00052 if (!strcmp(optarg, "lazy")) {
00053 config.load_method = util::LAZY;
00054 } else if (!strcmp(optarg, "populate")) {
00055 config.load_method = util::POPULATE_OR_READ;
00056 } else if (!strcmp(optarg, "read")) {
00057 config.load_method = util::READ;
00058 } else if (!strcmp(optarg, "parallel")) {
00059 config.load_method = util::PARALLEL_READ;
00060 } else {
00061 Usage(argv[0]);
00062 }
00063 break;
00064 case 'h':
00065 default:
00066 Usage(argv[0]);
00067 }
00068 }
00069 if (optind + 1 != argc)
00070 Usage(argv[0]);
00071 lm::ngram::QueryPrinter printer(1, verbosity >= 2, verbosity >= 1, true, flush);
00072 const char *file = argv[optind];
00073 try {
00074 using namespace lm::ngram;
00075 ModelType model_type;
00076 if (RecognizeBinary(file, model_type)) {
00077 switch(model_type) {
00078 case PROBING:
00079 Query<lm::ngram::ProbingModel>(file, config, sentence_context, printer);
00080 break;
00081 case REST_PROBING:
00082 Query<lm::ngram::RestProbingModel>(file, config, sentence_context, printer);
00083 break;
00084 case TRIE:
00085 Query<TrieModel>(file, config, sentence_context, printer);
00086 break;
00087 case QUANT_TRIE:
00088 Query<QuantTrieModel>(file, config, sentence_context, printer);
00089 break;
00090 case ARRAY_TRIE:
00091 Query<ArrayTrieModel>(file, config, sentence_context, printer);
00092 break;
00093 case QUANT_ARRAY_TRIE:
00094 Query<QuantArrayTrieModel>(file, config, sentence_context, printer);
00095 break;
00096 default:
00097 std::cerr << "Unrecognized kenlm model type " << model_type << std::endl;
00098 abort();
00099 }
00100 #ifdef WITH_NPLM
00101 } else if (lm::np::Model::Recognize(file)) {
00102 lm::np::Model model(file);
00103 Query<lm::np::Model, lm::ngram::QueryPrinter>(model, sentence_context, printer);
00104 Query<lm::np::Model, lm::ngram::QueryPrinter>(model, sentence_context, printer);
00105 #endif
00106 } else {
00107 Query<ProbingModel>(file, config, sentence_context, printer);
00108 }
00109 util::PrintUsage(std::cerr);
00110 } catch (const std::exception &e) {
00111 std::cerr << e.what() << std::endl;
00112 return 1;
00113 }
00114 return 0;
00115 }