mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2025-01-08 20:46:59 +03:00
1. Bug fix: DocumentBias didn't store the map from doc names to doc weights in m_bias_map;
2. getBiasMap() now returns a shared pointer to the context weights / bias map instead of a reference.
This commit is contained in:
parent
9db9492294
commit
34762f8c17
@ -24,7 +24,7 @@ namespace sapt
|
|||||||
boost::asio::io_service io_service;
|
boost::asio::io_service io_service;
|
||||||
Moses::http_client c(io_service, query, log);
|
Moses::http_client c(io_service, query, log);
|
||||||
io_service.run();
|
io_service.run();
|
||||||
|
|
||||||
if (log)
|
if (log)
|
||||||
{
|
{
|
||||||
std::string response = c.content();
|
std::string response = c.content();
|
||||||
@ -33,11 +33,9 @@ namespace sapt
|
|||||||
if (c.content().size() == 0)
|
if (c.content().size() == 0)
|
||||||
{
|
{
|
||||||
if (log) *log << "BIAS SERVER ERROR: " << c.error_msg() << std::endl;
|
if (log) *log << "BIAS SERVER ERROR: " << c.error_msg() << std::endl;
|
||||||
// UTIL_THROW_IF2(c.content().size() == 0, "No response from bias server!");
|
|
||||||
}
|
}
|
||||||
return c.content();
|
return c.content();
|
||||||
}
|
}
|
||||||
// #endif
|
|
||||||
|
|
||||||
SamplingBias::
|
SamplingBias::
|
||||||
SamplingBias(std::vector<id_type> const* sid2doc)
|
SamplingBias(std::vector<id_type> const* sid2doc)
|
||||||
@ -54,10 +52,10 @@ namespace sapt
|
|||||||
DocumentBias::
|
DocumentBias::
|
||||||
DocumentBias(std::vector<id_type> const& sid2doc,
|
DocumentBias(std::vector<id_type> const& sid2doc,
|
||||||
std::map<std::string,id_type> const& docname2docid,
|
std::map<std::string,id_type> const& docname2docid,
|
||||||
std::string const& server_url, std::string const& text,
|
std::string const& server_url,
|
||||||
|
std::string const& text,
|
||||||
std::ostream* _log)
|
std::ostream* _log)
|
||||||
: SamplingBias(&sid2doc)
|
: SamplingBias(&sid2doc)
|
||||||
// , m_bias(docname2docid.size(), 0)
|
|
||||||
{
|
{
|
||||||
this->log = _log;
|
this->log = _log;
|
||||||
#ifndef NO_MOSES
|
#ifndef NO_MOSES
|
||||||
@ -66,7 +64,6 @@ namespace sapt
|
|||||||
#endif
|
#endif
|
||||||
std::string json = query_bias_server(server_url, text, _log);
|
std::string json = query_bias_server(server_url, text, _log);
|
||||||
|
|
||||||
// std::cerr << "SERVER RESPONSE " << json << std::endl;
|
|
||||||
init_from_json(json, docname2docid, log);
|
init_from_json(json, docname2docid, log);
|
||||||
#ifndef NO_MOSES
|
#ifndef NO_MOSES
|
||||||
if (_log) *_log << "Bias query took " << timer << " seconds." << std::endl;
|
if (_log) *_log << "Bias query took " << timer << " seconds." << std::endl;
|
||||||
@ -79,13 +76,14 @@ namespace sapt
|
|||||||
std::map<std::string, float> const& context_weights,
|
std::map<std::string, float> const& context_weights,
|
||||||
std::ostream* _log)
|
std::ostream* _log)
|
||||||
: SamplingBias(&sid2doc)
|
: SamplingBias(&sid2doc)
|
||||||
// , m_bias(docname2docid.size(), 0)
|
|
||||||
{
|
{
|
||||||
this->log = _log;
|
this->log = _log;
|
||||||
init(context_weights, docname2docid);
|
init(context_weights, docname2docid);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, float>& SamplingBias::getBiasMap() {
|
SPTR<std::map<std::string, float> const>
|
||||||
|
SamplingBias::
|
||||||
|
getBiasMap() {
|
||||||
return m_bias_map;
|
return m_bias_map;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,38 +117,8 @@ namespace sapt
|
|||||||
|
|
||||||
typedef std::pair<std::string const,float> item;
|
typedef std::pair<std::string const,float> item;
|
||||||
if (total) { BOOST_FOREACH(item& x, bias) { x.second /= total; } }
|
if (total) { BOOST_FOREACH(item& x, bias) { x.second /= total; } }
|
||||||
// if (log)
|
|
||||||
// {
|
|
||||||
// BOOST_FOREACH(item& x, bias)
|
|
||||||
// {
|
|
||||||
// std::map<std::string,id_type>::const_iterator m;
|
|
||||||
// m = docname2docid.find(x.first);
|
|
||||||
// int docid = m != docname2docid.end() ? m->second : -1;
|
|
||||||
// *log << "CONTEXT SERVER RESPONSE "
|
|
||||||
// << "[" << docid << "] "
|
|
||||||
// << x.first << " " << x.second << std::endl;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
init(bias, docname2docid);
|
init(bias, docname2docid);
|
||||||
|
|
||||||
// using xmlrpc_parse_json didn't always work (parser errors)
|
|
||||||
// xmlrpc_value* b = xmlrpc_parse_json(env ,buf.str().c_str());
|
|
||||||
// std::cerr << "|" << buf.str() << "|" << std::endl;
|
|
||||||
// // if (b == NULL) std::cerr << "OOpS" << std::endl;
|
|
||||||
// xmlrpc_c::value_struct v(b); // = *b;
|
|
||||||
// std::map<std::string, xmlrpc_c::value> const
|
|
||||||
// bmap = static_cast<map<std::string, xmlrpc_c::value> >(v);
|
|
||||||
// std::map<std::string, float> bias;
|
|
||||||
// typedef std::map<std::string, xmlrpc_c::value>::value_type item;
|
|
||||||
// float total = 0;
|
|
||||||
// BOOST_FOREACH(item const& x, bmap)
|
|
||||||
// {
|
|
||||||
// total += bias[x.first] = xmlrpc_c::value_double(x.second);
|
|
||||||
// }
|
|
||||||
// typedef std::map<std::string, float>::value_type fitem;
|
|
||||||
// BOOST_FOREACH(fitem const& x, bias)
|
|
||||||
// std::cerr << x.first << " " << x.second/total << std::endl;
|
|
||||||
// // delete b;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -160,9 +128,11 @@ namespace sapt
|
|||||||
{
|
{
|
||||||
typedef std::map<std::string, float>::value_type bias_record;
|
typedef std::map<std::string, float>::value_type bias_record;
|
||||||
float total = 0;
|
float total = 0;
|
||||||
|
m_bias_map.reset(new std::map<std::string,float>(biasmap));
|
||||||
BOOST_FOREACH(bias_record const& b, biasmap)
|
BOOST_FOREACH(bias_record const& b, biasmap)
|
||||||
{
|
{
|
||||||
std::map<std::string, id_type>::const_iterator m = docname2docid.find(b.first);
|
std::map<std::string, id_type>::const_iterator m;
|
||||||
|
m = docname2docid.find(b.first);
|
||||||
if (m != docname2docid.end())
|
if (m != docname2docid.end())
|
||||||
total += (m_bias[m->second] = b.second);
|
total += (m_bias[m->second] = b.second);
|
||||||
}
|
}
|
||||||
@ -176,11 +146,14 @@ namespace sapt
|
|||||||
{
|
{
|
||||||
BOOST_FOREACH(bias_record const& b, biasmap)
|
BOOST_FOREACH(bias_record const& b, biasmap)
|
||||||
{
|
{
|
||||||
std::map<std::string, id_type>::const_iterator m = docname2docid.find(b.first);
|
std::map<std::string, id_type>::const_iterator m;
|
||||||
|
m = = docname2docid.find(b.first);
|
||||||
if (m != docname2docid.end())
|
if (m != docname2docid.end())
|
||||||
*log << "BIAS " << b.first << " " << m_bias[m->second] << std::endl;
|
*log << "BIAS " << b.first << " " << m_bias[m->second]
|
||||||
|
<< std::endl;
|
||||||
else
|
else
|
||||||
*log << "WARNING: bias reported for unknown document " << b.first << std::endl;
|
*log << "WARNING: bias reported for unknown document "
|
||||||
|
<< b.first << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -189,16 +162,17 @@ namespace sapt
|
|||||||
DocumentBias::
|
DocumentBias::
|
||||||
operator[](id_type const idx) const
|
operator[](id_type const idx) const
|
||||||
{
|
{
|
||||||
// UTIL_THROW_IF2(idx >= m_sid2docid->size(), "Out of bounds: "
|
std::map<id_type, float>::const_iterator m;
|
||||||
// << idx << "/" << m_sid2docid->size());
|
m = m_bias.find((*m_sid2docid)[idx]);
|
||||||
std::map<id_type, float>::const_iterator m = m_bias.find((*m_sid2docid)[idx]);
|
|
||||||
return m != m_bias.end() ? m->second : 0;
|
return m != m_bias.end() ? m->second : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
DocumentBias::
|
DocumentBias::
|
||||||
size() const
|
size() const
|
||||||
{ return m_sid2docid->size(); }
|
{
|
||||||
|
return m_sid2docid->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,8 +25,8 @@ namespace sapt
|
|||||||
int loglevel;
|
int loglevel;
|
||||||
std::ostream* log;
|
std::ostream* log;
|
||||||
// Map to store the biasmap as you get it from the server:
|
// Map to store the biasmap as you get it from the server:
|
||||||
std::map<std::string, float> m_bias_map;
|
SPTR<std::map<std::string, float> > m_bias_map;
|
||||||
std::map<std::string, float>& getBiasMap();
|
SPTR<std::map<std::string, float> const> getBiasMap();
|
||||||
virtual float
|
virtual float
|
||||||
operator[](id_type const ID) const = 0;
|
operator[](id_type const ID) const = 0;
|
||||||
// returns (unnormalized bias) for the class of item ID
|
// returns (unnormalized bias) for the class of item ID
|
||||||
|
Loading…
Reference in New Issue
Block a user