00001
00002
00003
00004 #include <cstdlib>
00005 #include <cstring>
00006 #include <string>
00007 #include <vector>
00008
00009 #include "moses/TranslationModel/CompactPT/PhraseDictionaryCompact.h"
00010 #include "moses/Util.h"
00011 #include "moses/Phrase.h"
00012 #include "moses/parameters/AllOptions.h"
00013
00014 void usage();
00015
00016 typedef unsigned int uint;
00017
00018 using namespace Moses;
00019
00020 int main(int argc, char **argv)
00021 {
00022 int nscores = 4;
00023 std::string ttable = "";
00024 bool useAlignments = false;
00025 bool reportCounts = false;
00026
00027 for(int i = 1; i < argc; i++) {
00028 if(!strcmp(argv[i], "-n")) {
00029 if(i + 1 == argc)
00030 usage();
00031 nscores = atoi(argv[++i]);
00032 } else if(!strcmp(argv[i], "-t")) {
00033 if(i + 1 == argc)
00034 usage();
00035 ttable = argv[++i];
00036 } else if(!strcmp(argv[i], "-a")) {
00037 useAlignments = true;
00038 } else if (!strcmp(argv[i], "-c")) {
00039 reportCounts = true;
00040 } else
00041 usage();
00042 }
00043
00044 if(ttable == "")
00045 usage();
00046
00047 std::vector<FactorType> input(1, 0);
00048 std::vector<FactorType> output(1, 0);
00049 std::vector<float> weight(nscores, 0);
00050
00051 std::stringstream ss;
00052 ss << nscores;
00053 PhraseDictionaryCompact pdc("PhraseDictionaryCompact input-factor=0 output-factor=0 num-features=" + ss.str() + " path=" + ttable);
00054 AllOptions::ptr opts(new AllOptions);
00055 pdc.Load(opts);
00056
00057 std::string line;
00058 while(getline(std::cin, line)) {
00059 Phrase sourcePhrase;
00060
00061 sourcePhrase.CreateFromString(Input, input, line, NULL);
00062
00063 TargetPhraseVectorPtr decodedPhraseColl
00064 = pdc.GetTargetPhraseCollectionRaw(sourcePhrase);
00065
00066 if(decodedPhraseColl != NULL) {
00067 if(reportCounts)
00068 std::cout << sourcePhrase << decodedPhraseColl->size() << std::endl;
00069 else
00070 for(TargetPhraseVector::iterator it = decodedPhraseColl->begin(); it != decodedPhraseColl->end(); it++) {
00071 TargetPhrase &tp = *it;
00072 std::cout << sourcePhrase << "||| ";
00073 std::cout << static_cast<const Phrase&>(tp) << "|||";
00074
00075 if(useAlignments)
00076 std::cout << " " << tp.GetAlignTerm() << "|||";
00077
00078 std::vector<float> scores = tp.GetScoreBreakdown().GetScoresForProducer(&pdc);
00079 for(size_t i = 0; i < scores.size(); i++)
00080 std::cout << " " << exp(scores[i]);
00081 std::cout << std::endl;
00082 }
00083 } else if(reportCounts)
00084 std::cout << sourcePhrase << 0 << std::endl;
00085
00086 std::cout.flush();
00087 }
00088 }
00089
00090 void usage()
00091 {
00092 std::cerr << "Usage: queryPhraseTable [-n <nscores>] [-a] -t <ttable>\n"
00093 "-n <nscores> number of scores in phrase table (default: 5)\n"
00094 "-c only report counts of entries\n"
00095 "-a binary phrase table contains alignments\n"
00096 "-t <ttable> phrase table\n";
00097 exit(1);
00098 }