2010-11-06 03:40:16 +03:00
|
|
|
#include "lm/model.hh"
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
#include <cstdlib>
|
2010-09-10 04:36:07 +04:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
float FloatSec(const struct timeval &tv) {
|
|
|
|
return static_cast<float>(tv.tv_sec) + (static_cast<float>(tv.tv_usec) / 1000000000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintUsage(const char *message) {
|
|
|
|
struct rusage usage;
|
|
|
|
if (getrusage(RUSAGE_SELF, &usage)) {
|
|
|
|
perror("getrusage");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::cerr << message;
|
|
|
|
std::cerr << "user\t" << FloatSec(usage.ru_utime) << "\nsys\t" << FloatSec(usage.ru_stime) << '\n';
|
|
|
|
|
|
|
|
// Linux doesn't set memory usage :-(.
|
|
|
|
std::ifstream status("/proc/self/status", std::ios::in);
|
|
|
|
std::string line;
|
|
|
|
while (getline(status, line)) {
|
|
|
|
if (!strncmp(line.c_str(), "VmRSS:\t", 7)) {
|
|
|
|
std::cerr << "rss " << (line.c_str() + 7) << '\n';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Model> void Query(const Model &model) {
|
|
|
|
PrintUsage("Loading statistics:\n");
|
2010-09-15 01:33:11 +04:00
|
|
|
typename Model::State state, out;
|
|
|
|
lm::FullScoreReturn ret;
|
|
|
|
std::string word;
|
|
|
|
|
|
|
|
while (std::cin) {
|
2010-09-10 04:36:07 +04:00
|
|
|
state = model.BeginSentenceState();
|
|
|
|
float total = 0.0;
|
2010-09-15 01:33:11 +04:00
|
|
|
bool got = false;
|
|
|
|
while (std::cin >> word) {
|
|
|
|
got = true;
|
|
|
|
ret = model.FullScore(state, model.GetVocabulary().Index(word), out);
|
2010-09-10 04:36:07 +04:00
|
|
|
total += ret.prob;
|
2010-09-15 01:33:11 +04:00
|
|
|
std::cout << word << ' ' << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << ' ';
|
2010-09-10 04:36:07 +04:00
|
|
|
state = out;
|
2010-09-15 01:33:11 +04:00
|
|
|
if (std::cin.get() == '\n') break;
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
2010-09-15 01:33:11 +04:00
|
|
|
if (!got && !std::cin) break;
|
|
|
|
ret = model.FullScore(state, model.GetVocabulary().EndSentence(), out);
|
|
|
|
total += ret.prob;
|
|
|
|
std::cout << "</s> " << static_cast<unsigned int>(ret.ngram_length) << ' ' << ret.prob << ' ';
|
2010-09-10 04:36:07 +04:00
|
|
|
std::cout << "Total: " << total << '\n';
|
|
|
|
}
|
|
|
|
PrintUsage("After queries:\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc < 2) {
|
2010-09-15 01:33:11 +04:00
|
|
|
std::cerr << "Pass language model name." << std::endl;
|
2010-09-10 04:36:07 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2010-09-15 01:33:11 +04:00
|
|
|
{
|
|
|
|
lm::ngram::Model ngram(argv[1]);
|
|
|
|
Query(ngram);
|
|
|
|
}
|
2010-09-10 04:36:07 +04:00
|
|
|
PrintUsage("Total time including destruction:\n");
|
|
|
|
}
|