00001 #include <iostream>
00002 #include <string>
00003
00004 #include "moses/Phrase.h"
00005 #include "moses/FactorCollection.h"
00006 #include "moses/Timer.h"
00007 #include "moses/InputFileStream.h"
00008 #include "moses/LexicalReorderingTable.h"
00009
00010 using namespace Moses;
00011
00012 Timer timer;
00013
00014 void printHelp()
00015 {
00016 std::cerr << "Usage:\n"
00017 "options: \n"
00018 "\t-table file -- input table file name\n"
00019 "\t-f string -- f query phrase\n"
00020 "\t-e string -- e query phrase\n"
00021 "\t-c string -- context query phrase\n"
00022 "\n";
00023 }
00024
00025 std::ostream& operator<<(std::ostream& o, Scores s)
00026 {
00027 for(size_t i = 0; i < s.size(); ++i) {
00028 o << s[i] << " ";
00029 }
00030
00031 return o;
00032 };
00033
00034 int main(int argc, char** argv)
00035 {
00036 std::cerr << "queryLexicalTable v0.2 by Konrad Rawlik\n";
00037 std::string inFilePath;
00038 std::string outFilePath("out");
00039 bool cache = false;
00040 std::string query_e, query_f, query_c;
00041 bool use_context = false;
00042 bool use_e = false;
00043 if(1 >= argc) {
00044 printHelp();
00045 return 1;
00046 }
00047 for(int i = 1; i < argc; ++i) {
00048 std::string arg(argv[i]);
00049 if("-table" == arg && i+1 < argc) {
00050
00051 ++i;
00052 inFilePath = argv[i];
00053 } else if("-f" == arg && i+1 < argc) {
00054 ++i;
00055
00056 query_f = argv[i];
00057 } else if("-e" == arg && i+1 < argc) {
00058 ++i;
00059 query_e = argv[i];
00060 use_e = true;
00061 } else if("-c" == arg) {
00062 if(i+1 < argc && '-' != argv[i+1][0]) {
00063 ++i;
00064 query_c = argv[i];
00065 use_context = true;
00066 } else {
00067 use_context = false;
00068 }
00069 } else if("-cache" == arg) {
00070 ++i;
00071 cache = true;
00072 } else {
00073
00074 printHelp();
00075 return 1;
00076 }
00077 }
00078
00079 FactorList f_mask;
00080 FactorList e_mask;
00081 FactorList c_mask;
00082 f_mask.push_back(0);
00083 if(use_e) {
00084 e_mask.push_back(0);
00085 }
00086 if(use_context) {
00087 c_mask.push_back(0);
00088 }
00089 Phrase e( 0),f(0),c(0);
00090 e.CreateFromString(Output, e_mask, query_e, "|", NULL);
00091 f.CreateFromString(Input, f_mask, query_f, "|", NULL);
00092 c.CreateFromString(Input, c_mask, query_c,"|", NULL);
00093 LexicalReorderingTable* table;
00094 if(FileExists(inFilePath+".binlexr.idx")) {
00095 std::cerr << "Loading binary table...\n";
00096 table = new LexicalReorderingTableTree(inFilePath, f_mask, e_mask, c_mask);
00097 } else {
00098 std::cerr << "Loading ordinary table...\n";
00099 table = new LexicalReorderingTableMemory(inFilePath, f_mask, e_mask, c_mask);
00100 }
00101
00102 if(cache) {
00103 std::cerr << "Caching for f\n";
00104 table->InitializeForInputPhrase(f);
00105 }
00106 std::cerr << "Querying: f='" << f.GetStringRep(f_mask) << "' e='" << e.GetStringRep(e_mask) << "' c='" << c.GetStringRep(c_mask) << "'\n";
00107 std::cerr << table->GetScore(f,e,c) << "\n";
00108
00109 delete table;
00110 return 0;
00111 }
00112
00113