mosesdecoder/OnDiskPt/queryOnDiskPt.cpp

87 lines
2.0 KiB
C++
Raw Normal View History

// Query binary phrase tables.
// Christian Hardmeier, 16 May 2010
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
2012-11-12 23:56:18 +04:00
#include "moses/Util.h"
#include "OnDiskWrapper.h"
#include "SourcePhrase.h"
#include "OnDiskQuery.h"
using namespace std;
using namespace OnDiskPt;
void usage();
typedef unsigned int uint;
int main(int argc, char **argv)
{
2012-05-15 19:46:16 +04:00
int tableLimit = 20;
std::string ttable = "";
// bool useAlignments = false;
for(int i = 1; i < argc; i++) {
2012-05-15 19:46:16 +04:00
if(!strcmp(argv[i], "-tlimit")) {
if(i + 1 == argc)
usage();
2012-05-15 19:46:16 +04:00
tableLimit = atoi(argv[++i]);
} else if(!strcmp(argv[i], "-t")) {
if(i + 1 == argc)
usage();
ttable = argv[++i];
2013-05-29 21:16:15 +04:00
} else
usage();
}
if(ttable == "")
usage();
OnDiskWrapper onDiskWrapper;
onDiskWrapper.BeginLoad(ttable);
OnDiskQuery onDiskQuery(onDiskWrapper);
cerr << "Ready..." << endl;
std::string line;
while(getline(std::cin, line)) {
std::vector<std::string> tokens;
2012-05-15 19:03:40 +04:00
tokens = Moses::Tokenize(line, " ");
cerr << "line: " << line << endl;
const PhraseNode* node = onDiskQuery.Query(tokens);
2013-05-29 21:16:15 +04:00
if (node) {
// source phrase points to a bunch of rules
TargetPhraseCollection::shared_ptr coll = node->GetTargetPhraseCollection(tableLimit, onDiskWrapper);
2012-05-15 18:06:25 +04:00
string str = coll->GetDebugStr();
2012-05-15 19:46:16 +04:00
cout << "Found " << coll->GetSize() << endl;
2013-05-29 21:16:15 +04:00
for (size_t ind = 0; ind < coll->GetSize(); ++ind) {
2012-05-15 19:03:40 +04:00
const TargetPhrase &targetPhrase = coll->GetTargetPhrase(ind);
2012-05-15 19:46:16 +04:00
cerr << " ";
2012-05-15 19:35:00 +04:00
targetPhrase.DebugPrint(cerr, onDiskWrapper.GetVocab());
cerr << endl;
2012-05-15 19:03:40 +04:00
}
2013-05-29 21:16:15 +04:00
} else {
2012-05-15 18:06:25 +04:00
cout << "Not found" << endl;
}
2013-05-29 21:16:15 +04:00
std::cout << '\n';
std::cout.flush();
}
2013-05-29 21:16:15 +04:00
2012-05-15 18:06:25 +04:00
cerr << "Finished." << endl;
}
void usage()
{
std::cerr << "Usage: queryOnDiskPt [-n <nscores>] [-a] -t <ttable>\n"
2012-05-15 19:46:16 +04:00
"-tlimit <table limit> max number of rules per source phrase (default: 20)\n"
"-t <ttable> phrase table\n";
exit(1);
}