Allow XML-RPC requests to update weights

This commit is contained in:
Lane Schwartz 2016-11-14 13:26:34 -06:00
parent ea9d3b7f3e
commit 05006bf1e2
2 changed files with 83 additions and 0 deletions

View File

@ -48,6 +48,32 @@ public:
virtual void InitializeForInput(ttasksptr const& ttask) {
VERBOSE(1, "ReloadingLM InitializeForInput" << std::endl);
// The context scope object for this translation task
// contains a map of translation task-specific data
boost::shared_ptr<Moses::ContextScope> contextScope = ttask->GetScope();
// The key to the map is this object
void const* key = static_cast<void const*>(this);
// The value stored in the map is a string representing a phrase table
boost::shared_ptr<string> value = contextScope->get<string>(key);
// Create a stream to read the phrase table data
stringstream strme(*(value.get()));
ofstream tmp;
tmp.open(m_file.c_str());
// Read the phrase table data, one line at a time
string line;
while (getline(strme, line)) {
tmp << line << "\n";
}
tmp.close();
LanguageModelKen<Model>::LoadModel(m_file, m_lazy ? util::LAZY : util::POPULATE_OR_READ);
};

View File

@ -26,6 +26,7 @@ using Moses::FindPhraseDictionary;
using Moses::Sentence;
using Moses::TokenizeMultiCharSeparator;
using Moses::FeatureFunction;
using Moses::Scan;
boost::shared_ptr<TranslationRequest>
TranslationRequest::
@ -352,6 +353,62 @@ parse_request(std::map<std::string, xmlrpc_c::value> const& params)
}
}
si = params.find("weights");
if (si != params.end())
{
boost::unordered_map<string, FeatureFunction*> map;
{
const vector<FeatureFunction*> &ffs = FeatureFunction::GetFeatureFunctions();
BOOST_FOREACH(FeatureFunction* const& ff, ffs) {
map[ff->GetScoreProducerDescription()] = ff;
}
}
string allValues = xmlrpc_c::value_string(si->second);
BOOST_FOREACH(string values, TokenizeMultiCharSeparator(allValues, "\t")) {
vector<string> record = TokenizeMultiCharSeparator(values, "=");
if (record.size() == 2) {
string featureName = record[0];
string featureWeights = record[1];
boost::unordered_map<string, FeatureFunction*>::iterator ffi = map.find(featureName);
if (ffi != map.end()) {
FeatureFunction* ff = ffi->second;
size_t prevNumWeights = ff->GetNumScoreComponents();
vector<float> ffWeights;
BOOST_FOREACH(string weight, TokenizeMultiCharSeparator(featureWeights, " ")) {
ffWeights.push_back(Scan<float>(weight));
}
if (ffWeights.size() == ff->GetNumScoreComponents()) {
// XXX: This is NOT thread-safe
Moses::StaticData::InstanceNonConst().SetWeights(ff, ffWeights);
VERBOSE(1, "WARNING: THIS IS NOT THREAD-SAFE!\tUpdating weights for " << featureName << " to " << featureWeights << "\n");
} else {
TRACE_ERR("ERROR: Unable to update weights for " << featureName << " because " << ff->GetNumScoreComponents() << " weights are required but only " << ffWeights.size() << " were provided\n");
}
} else {
TRACE_ERR("ERROR: No FeatureFunction with name " << featureName << ", no weight update\n");
}
} else {
TRACE_ERR("WARNING: XML-RPC weights update was improperly formatted:\t" << values << "\n");
}
}
}
// // biased sampling for suffix-array-based sampling phrase table?
// if ((si = params.find("bias")) != params.end())