00001 #include <fstream>
00002 #include <iostream>
00003 #include <vector>
00004 #include <string>
00005
00006 #include <boost/shared_ptr.hpp>
00007
00008 #include "BleuScorer.h"
00009 #include "Reference.h"
00010 #include "moses/Util.h"
00011 #include "util/exception.hh"
00012
00013 using namespace MosesTuning;
00014
00015 int main(int argc, char **argv)
00016 {
00017 if (argc == 1) {
00018 std::cerr << "Usage: ./sentence-bleu-nbest ref1 [ref2 ...] < plain-nbest > bleu-scores" << std::endl;
00019 return 1;
00020 }
00021
00022 std::vector<std::string> refFiles(argv + 1, argv + argc);
00023
00024
00025 std::string config;
00026 std::string factors;
00027 std::string filter;
00028
00029 BleuScorer scorer(config);
00030 scorer.setFactors(factors);
00031 scorer.setFilter(filter);
00032
00033
00034 std::vector<boost::shared_ptr<std::ifstream> > refStreams;
00035 for (std::vector<std::string>::const_iterator refFile=refFiles.begin(); refFile!=refFiles.end(); ++refFile) {
00036 TRACE_ERR("Loading reference from " << *refFile << std::endl);
00037 boost::shared_ptr<std::ifstream> ifs(new std::ifstream(refFile->c_str()));
00038 UTIL_THROW_IF2(!ifs, "Cannot open " << *refFile);
00039 refStreams.push_back(ifs);
00040 }
00041
00042
00043 std::string nbestLine;
00044 int sid = -1;
00045 Reference ref;
00046 while ( getline(std::cin, nbestLine) ) {
00047 std::vector<std::string> items;
00048 Moses::TokenizeMultiCharSeparator(items, nbestLine, " ||| ");
00049 int sidCurrent = Moses::Scan<int>(items[0]);
00050
00051 if (sidCurrent != sid) {
00052 ref.clear();
00053 if (!scorer.GetNextReferenceFromStreams(refStreams, ref)) {
00054 UTIL_THROW2("Missing references");
00055 }
00056 sid = sidCurrent;
00057 }
00058 ScoreStats scoreStats;
00059 scorer.CalcBleuStats(ref, items[1], scoreStats);
00060 std::vector<float> stats(scoreStats.getArray(), scoreStats.getArray() + scoreStats.size());
00061 std::cout << smoothedSentenceBleu(stats) << std::endl;
00062 }
00063
00064 return 0;
00065 }
00066