diff options
author | Ulrich Germann <ugermann@inf.ed.ac.uk> | 2015-03-18 16:46:15 +0300 |
---|---|---|
committer | Ulrich Germann <ugermann@inf.ed.ac.uk> | 2015-03-18 16:46:15 +0300 |
commit | 058abc32c378cbae1b2ea615f13e4681a8b1f4fd (patch) | |
tree | b459d03ab528c8320104a680d9ff494146d54600 /moses/server | |
parent | 1ca4f4253929f2488230d6ae622c19df8fad5089 (diff) |
Fixes (?) numerous bugs discovered when the code was actually compiled (an error in the bjam configuration prevented this).
Diffstat (limited to 'moses/server')
-rw-r--r-- | moses/server/Optimizer.cpp | 3 | ||||
-rw-r--r-- | moses/server/TranslationTask.cpp | 119 | ||||
-rw-r--r-- | moses/server/TranslationTask.h | 34 | ||||
-rw-r--r-- | moses/server/Translator.h | 12 | ||||
-rw-r--r-- | moses/server/Updater.cpp | 4 | ||||
-rw-r--r-- | moses/server/Updater.h | 6 |
6 files changed, 106 insertions, 72 deletions
diff --git a/moses/server/Optimizer.cpp b/moses/server/Optimizer.cpp index 838ae5c7f..d7d5f939c 100644 --- a/moses/server/Optimizer.cpp +++ b/moses/server/Optimizer.cpp @@ -1,4 +1,5 @@ #include "Optimizer.h" +#include <iostream> namespace MosesServer { @@ -64,7 +65,7 @@ namespace MosesServer #else string errmsg = "Error: Perplexity minimization requires dlib "; errmsg += "(compilation option --with-dlib)"; - cerr << errmsg << endl; + std::cerr << errmsg << std::endl; *retvalP = xmlrpc_c::value_string(errmsg); #endif } diff --git a/moses/server/TranslationTask.cpp b/moses/server/TranslationTask.cpp index 99e3cb29c..dba283297 100644 --- a/moses/server/TranslationTask.cpp +++ b/moses/server/TranslationTask.cpp @@ -1,22 +1,23 @@ #include "TranslationTask.h" +#include <boost/foreach.hpp> namespace MosesServer { using namespace Moses; using namespace std; - virtual void + void TranslationTask:: Run() { - parse_request(); + parse_request(m_paramList.getStruct(0)); - StaticData cosnt& SD = StaticData::Instance(); + StaticData const& SD = Moses::StaticData::Instance(); //Make sure alternative paths are retained, if necessary if (m_withGraphInfo || m_nbestSize>0) // why on earth is this a global variable? Is this even thread-safe???? UG - (const_cast<StaticData&>(staticData)).SetOutputSearchGraph(true); + (const_cast<Moses::StaticData&>(SD)).SetOutputSearchGraph(true); std::stringstream out, graphInfo, transCollOpts; @@ -37,17 +38,17 @@ namespace MosesServer /// add phrase alignment information from a Hypothesis void TranslationTask:: - add_phrase_aln_info(Hypothesis const& h, vector<xmlrpc_c::value>& aInfo) + add_phrase_aln_info(Hypothesis const& h, vector<xmlrpc_c::value>& aInfo) const { - if (!m_oopt.withPhraseAlignment) return; + if (!m_withAlignInfo) return; WordsRange const& trg = h.GetCurrTargetWordsRange(); WordsRange const& src = h.GetCurrSourceWordsRange(); std::map<std::string, xmlrpc_c::value> pAlnInfo; pAlnInfo["tgt-start"] = xmlrpc_c::value_int(trg.GetStartPos()); - pAlgInfo["src-start"] = xmlrpc_c::value_int(src.GetStartPos()); + pAlnInfo["src-start"] = xmlrpc_c::value_int(src.GetStartPos()); pAlnInfo["src-end"] = xmlrpc_c::value_int(src.GetEndPos()); - aInfo.push_back(xmlrpc_c::value_struct(phraseAlignInfo)); + aInfo.push_back(xmlrpc_c::value_struct(pAlnInfo)); } void @@ -67,7 +68,8 @@ namespace MosesServer bool TranslationTask:: - compareSearchGraphNode(const SearchGraphNode& a, const SearchGraphNode b) + compareSearchGraphNode(const Moses::SearchGraphNode& a, + const Moses::SearchGraphNode& b) { return a.hypo->GetId() < b.hypo->GetId(); } void @@ -82,7 +84,7 @@ namespace MosesServer vector<SearchGraphNode> searchGraph; manager.GetSearchGraph(searchGraph); std::sort(searchGraph.begin(), searchGraph.end()); - BOOST_FOREACH(SearchGraphNode const& n, searchGraph) + BOOST_FOREACH(Moses::SearchGraphNode const& n, searchGraph) { map<string, xmlrpc_c::value> x; // search graph xml node x["forward"] = value_double(n.forward); @@ -104,12 +106,12 @@ namespace MosesServer } searchGraphXml.push_back(value_struct(x)); } - retData["sg"] = value_array(searchGraphXml); + retData["sg"] = xmlrpc_c::value_array(searchGraphXml); } void TranslationTask:: - output_phrase(ostream& out, Phrase const& phrase) + output_phrase(ostream& out, Phrase const& phrase) const { if (!m_reportAllFactors) { @@ -127,9 +129,9 @@ namespace MosesServer vector<xmlrpc_c::value> nBestXml; manager.CalcNBest(m_nbestSize, nBestList, m_nbestDistinct); - BOOST_FOREACH(TrellisPath const& path, nBestList) + BOOST_FOREACH(Moses::TrellisPath const* path, nBestList) { - vector<const Hypothesis *> const& E = path.GetEdges(); + vector<const Hypothesis *> const& E = path->GetEdges(); if (!E.size()) continue; std::map<std::string, xmlrpc_c::value> nBestXmlItem; pack_hypothesis(E, "hyp", nBestXmlItem); @@ -137,21 +139,21 @@ namespace MosesServer { // should the score breakdown be reported in a more structured manner? ostringstream buf; - path.GetScoreBreakdown().OutputAllFeatureScores(buf); - nBestXMLItem["fvals"] = xmlrpc_c::value_string(buf.str()); + path->GetScoreBreakdown().OutputAllFeatureScores(buf); + nBestXmlItem["fvals"] = xmlrpc_c::value_string(buf.str()); } // weighted score - nBestXMLItem["totalScore"] = xmlrpc_c::value_double(path.GetTotalScore()); - nBestXml.push_back(xmlrpc_c::value_struct(nBestXMLItem)); + nBestXmlItem["totalScore"] = xmlrpc_c::value_double(path->GetTotalScore()); + nBestXml.push_back(xmlrpc_c::value_struct(nBestXmlItem)); } retData["nbest"] = xmlrpc_c::value_array(nBestXml); } void TranslationTask:: - insertTranslationOptions(Manager& manager, - map<string, xmlrpc_c::value>& retData) + insertTranslationOptions(Moses::Manager& manager, + std::map<std::string, xmlrpc_c::value>& retData) { const TranslationOptionCollection* toptsColl = manager.getSntTranslationOptions(); @@ -187,33 +189,40 @@ namespace MosesServer } retData["topt"] = xmlrpc_c::value_array(toptsXml); } + + bool + check(std::map<std::string, xmlrpc_c::value> const& params, std::string const key) + { + std::map<std::string, xmlrpc_c::value>::const_iterator m; + return (params.find(key) != params.end()); + } TranslationTask:: TranslationTask(xmlrpc_c::paramList const& paramList, boost::condition_variable& cond, boost::mutex& mut) - : m_paramList(paramList), m_cond(cond), m_mutex(mutex), m_done(false) + : m_cond(cond), m_mutex(mut), m_done(false), m_paramList(paramList) { } void TranslationTask:: - parse_request(std::map<std::string, xmlrpc_c::value> const& req) + parse_request(std::map<std::string, xmlrpc_c::value> const& params) { // parse XMLRPC request // params_t const params = m_paramList.getStruct(0); m_paramList.verifyEnd(1); // ??? UG // source text must be given, or we don't know what to translate + typedef std::map<std::string, xmlrpc_c::value> params_t; params_t::const_iterator si = params.find("text"); if (si == params.end()) throw xmlrpc_c::fault("Missing source text", xmlrpc_c::fault::CODE_PARSE); m_source = xmlrpc_c::value_string(si->second); - XVERBOSE(1,"Input: " << source << endl); + XVERBOSE(1,"Input: " << m_source << endl); - m_oopt.init(req); - m_withPhraseAlignment = check(params, "align"); - m_withWordAlignment = check(params, "word-align"); - m_withSearchGraph = check(params, "sg"); - m_withTransOpts = check(params, "topt"); - m_outputAllFactors = check(params, "report-all-factors"); + m_withAlignInfo = check(params, "align"); + m_withWordAlignInfo = check(params, "word-align"); + m_withGraphInfo = check(params, "sg"); + m_withTopts = check(params, "topt"); + m_reportAllFactors = check(params, "report-all-factors"); m_nbestDistinct = check(params, "nbest-distinct"); m_withScoreBreakdown = check(params, "add-score-breakdown"); @@ -229,21 +238,22 @@ namespace MosesServer if (w.size() && (si = params.find("model_name")) != params.end()) { string const model_name = xmlrpc_c::value_string(si->second); - // PhraseDictionaryMultiModel* pdmm - // = (PhraseDictionaryMultiModel*) FindPhraseDictionary(model_name); - PhraseDictionaryMultiModel* pdmm = FindPhraseDictionary(model_name); + PhraseDictionaryMultiModel* pdmm + = (PhraseDictionaryMultiModel*) FindPhraseDictionary(model_name); + // Moses::PhraseDictionaryMultiModel* pdmm + // = FindPhraseDictionary(model_name); pdmm->SetTemporaryMultiModelWeightsVector(w); } } - // biased sampling for suffix-array-based sampling phrase table? - if ((si = params.find("bias")) != params.end()) - { - std::vector<xmlrpc_c::value> tmp - = xmlrpc_c::value_array(si->second).cvalue(); - for (size_t i = 1; i < tmp.size(); i += 2) - m_bias[xmlrpc_c::value_int(tmp[i-1])] = xmlrpc_c::value_double(tmp[i]); - } + // // biased sampling for suffix-array-based sampling phrase table? + // if ((si = params.find("bias")) != params.end()) + // { + // std::vector<xmlrpc_c::value> tmp + // = xmlrpc_c::value_array(si->second).cvalue(); + // for (size_t i = 1; i < tmp.size(); i += 2) + // m_bias[xmlrpc_c::value_int(tmp[i-1])] = xmlrpc_c::value_double(tmp[i]); + // } } // end of Translationtask::parse_request() @@ -251,21 +261,21 @@ namespace MosesServer TranslationTask:: run_chart_decoder() { - TreeInput tinput; - tinput.Read(stringstream(m_source + "\n"), - StaticData::Instance().GetInputFactorOrder()); + Moses::TreeInput tinput(NULL); + istringstream buf(m_source + "\n"); + tinput.Read(buf, StaticData::Instance().GetInputFactorOrder()); - ChartManager manager(tinput); + Moses::ChartManager manager(tinput); manager.Decode(); - const ChartHypothesis *hypo = manager.GetBestHypothesis(); + const Moses::ChartHypothesis *hypo = manager.GetBestHypothesis(); ostringstream out; outputChartHypo(out,hypo); m_target = out.str(); m_retData["text"] = xmlrpc_c::value_string(m_target); - if (m_addGraphInfo) + if (m_withGraphInfo) { std::ostringstream sgstream; manager.OutputSearchGraphMoses(sgstream); @@ -284,7 +294,7 @@ namespace MosesServer output_phrase(target, e->GetCurrTargetPhrase()); dest[key] = xmlrpc_c::value_string(target.str()); - if (m_addAlignmentInfo) + if (m_withAlignInfo) { // phrase alignment, if requested vector<xmlrpc_c::value> p_aln; @@ -293,11 +303,11 @@ namespace MosesServer dest["align"] = xmlrpc_c::value_array(p_aln); } - if (m_addWordAlignmentInfo) + if (m_withWordAlignInfo) { // word alignment, if requested vector<xmlrpc_c::value> w_aln; BOOST_FOREACH(Hypothesis const* e, edges) - e->OutputLocalWordAligment(w_aln); + e->OutputLocalWordAlignment(w_aln); dest["word-align"] = xmlrpc_c::value_array(w_aln); } } @@ -319,17 +329,18 @@ namespace MosesServer TranslationTask:: run_phrase_decoder() { - Manager manager(Sentence(0, m_source)); + Manager manager(Sentence(NULL,0, m_source)); // if (m_bias.size()) manager.SetBias(&m_bias); manager.Decode(); - pack_hypothesis(manager.GetBestHypothesis(), m_retData); + pack_hypothesis(manager.GetBestHypothesis(), "text", m_retData); - if (m_addGraphInfo) insertGraphInfo(manager,m_retData); - if (m_addTopts) insertTranslationOptions(manager,m_retData); + if (m_withGraphInfo) insertGraphInfo(manager,m_retData); + if (m_withTopts) insertTranslationOptions(manager,m_retData); if (m_nbestSize) outputNBest(manager, m_retData); - (const_cast<StaticData&>(staticData)).SetOutputSearchGraph(false); + (const_cast<StaticData&>(Moses::StaticData::Instance())) + .SetOutputSearchGraph(false); // WTF? one more reason not to have this as global variable! --- UG } diff --git a/moses/server/TranslationTask.h b/moses/server/TranslationTask.h index 5174e8241..4fedd75b8 100644 --- a/moses/server/TranslationTask.h +++ b/moses/server/TranslationTask.h @@ -18,6 +18,8 @@ #include "moses/Manager.h" #include "moses/StaticData.h" #include "moses/ThreadPool.h" +#include "moses/TranslationModel/PhraseDictionaryMultiModel.h" +#include "moses/TreeInput.h" namespace MosesServer { @@ -26,16 +28,16 @@ namespace MosesServer { boost::condition_variable& m_cond; boost::mutex& m_mutex; + bool m_done; xmlrpc_c::paramList const& m_paramList; std::map<std::string, xmlrpc_c::value> m_retData; - bool m_done; std::map<uint32_t,float> m_bias; // for biased sampling std::string m_source, m_target; bool m_withAlignInfo; bool m_withWordAlignInfo; - bool m_withGrapInfo; + bool m_withGraphInfo; bool m_withTopts; bool m_reportAllFactors; bool m_nbestDistinct; @@ -44,6 +46,9 @@ namespace MosesServer void parse_request(); + + void + parse_request(std::map<std::string, xmlrpc_c::value> const& req); virtual void run_chart_decoder(); @@ -52,31 +57,38 @@ namespace MosesServer run_phrase_decoder(); void - pack_hypothesis(vector<Hypothesis const* > const& edges, + pack_hypothesis(std::vector<Moses::Hypothesis const* > const& edges, std::string const& key, std::map<std::string, xmlrpc_c::value> & dest) const; - + + void + pack_hypothesis(Moses::Hypothesis const* h, std::string const& key, + std::map<std::string, xmlrpc_c::value> & dest) const; + + void - output_phrase(ostream& out, Phrase const& phrase); + output_phrase(std::ostream& out, Moses::Phrase const& phrase) const; void - add_phrase_aln(Hypothesis const& h, std::vector<xmlrpc_c::value>& aInfo); + add_phrase_aln_info(Moses::Hypothesis const& h, + std::vector<xmlrpc_c::value>& aInfo) const; void - outputChartHypo(ostream& out, const ChartHypothesis* hypo); + outputChartHypo(std::ostream& out, const Moses::ChartHypothesis* hypo); bool - compareSearchGraphNode(const SearchGraphNode& a, const SearchGraphNode b); + compareSearchGraphNode(const Moses::SearchGraphNode& a, + const Moses::SearchGraphNode& b); void - insertGraphInfo(Manager& manager, + insertGraphInfo(Moses::Manager& manager, std::map<std::string, xmlrpc_c::value>& retData); void - outputNBest(Manager const& manager, + outputNBest(Moses::Manager const& manager, std::map<std::string, xmlrpc_c::value>& retData); void - insertTranslationOptions(Manager& manager, + insertTranslationOptions(Moses::Manager& manager, std::map<std::string, xmlrpc_c::value>& retData); public: diff --git a/moses/server/Translator.h b/moses/server/Translator.h index 3bdf94b91..b05098ed8 100644 --- a/moses/server/Translator.h +++ b/moses/server/Translator.h @@ -1,11 +1,17 @@ // -*- c++ -*- #pragma once -#include "MosesServer.h" -namespace Moses +#include <xmlrpc-c/base.hpp> +#include <xmlrpc-c/registry.hpp> +#include <xmlrpc-c/server_abyss.hpp> +#include "moses/ThreadPool.h" +#ifndef WITH_THREADS +#pragma message("COMPILING WITHOUT THREADS!") +#endif +namespace MosesServer { class - MosesServer:: + // MosesServer:: Translator : public xmlrpc_c::method { public: diff --git a/moses/server/Updater.cpp b/moses/server/Updater.cpp index f8bd442cf..95cafd71a 100644 --- a/moses/server/Updater.cpp +++ b/moses/server/Updater.cpp @@ -45,12 +45,12 @@ namespace MosesServer throw xmlrpc_c::fault("Missing target sentence", xmlrpc_c::fault::CODE_PARSE); m_trg = xmlrpc_c::value_string(si->second); - XVERBOSE(1,"target = " << m_trg_ << endl); + XVERBOSE(1,"target = " << m_trg << endl); if((si = params.find("alignment")) == params.end()) throw xmlrpc_c::fault("Missing alignment", xmlrpc_c::fault::CODE_PARSE); m_aln = xmlrpc_c::value_string(si->second); XVERBOSE(1,"alignment = " << m_aln << endl); - bounded_ = ((si = params.find("bounded")) != params.end()); + m_bounded = ((si = params.find("bounded")) != params.end()); m_add2ORLM = ((si = params.find("updateORLM")) != params.end()); }; diff --git a/moses/server/Updater.h b/moses/server/Updater.h index c4866b3d3..c3c72da50 100644 --- a/moses/server/Updater.h +++ b/moses/server/Updater.h @@ -17,11 +17,15 @@ #include <xmlrpc-c/server_abyss.hpp> -namespace MoseServer +namespace MosesServer { class Updater: public xmlrpc_c::method { + + typedef std::map<std::string, xmlrpc_c::value> params_t; + + std::string m_src, m_trg, m_aln; bool m_bounded, m_add2ORLM; |