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:
Ulrich Germann 2015-10-28 12:37:52 +00:00
parent 9db9492294
commit 34762f8c17
2 changed files with 22 additions and 48 deletions

View File

@ -24,7 +24,7 @@ namespace sapt
boost::asio::io_service io_service;
Moses::http_client c(io_service, query, log);
io_service.run();
if (log)
{
std::string response = c.content();
@ -33,11 +33,9 @@ namespace sapt
if (c.content().size() == 0)
{
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();
}
// #endif
SamplingBias::
SamplingBias(std::vector<id_type> const* sid2doc)
@ -54,10 +52,10 @@ namespace sapt
DocumentBias::
DocumentBias(std::vector<id_type> const& sid2doc,
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)
: SamplingBias(&sid2doc)
// , m_bias(docname2docid.size(), 0)
{
this->log = _log;
#ifndef NO_MOSES
@ -66,7 +64,6 @@ namespace sapt
#endif
std::string json = query_bias_server(server_url, text, _log);
// std::cerr << "SERVER RESPONSE " << json << std::endl;
init_from_json(json, docname2docid, log);
#ifndef NO_MOSES
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::ostream* _log)
: SamplingBias(&sid2doc)
// , m_bias(docname2docid.size(), 0)
{
this->log = _log;
init(context_weights, docname2docid);
}
std::map<std::string, float>& SamplingBias::getBiasMap() {
SPTR<std::map<std::string, float> const>
SamplingBias::
getBiasMap() {
return m_bias_map;
}
@ -119,38 +117,8 @@ namespace sapt
typedef std::pair<std::string const,float> item;
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);
// 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
@ -160,9 +128,11 @@ namespace sapt
{
typedef std::map<std::string, float>::value_type bias_record;
float total = 0;
m_bias_map.reset(new std::map<std::string,float>(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())
total += (m_bias[m->second] = b.second);
}
@ -176,11 +146,14 @@ namespace sapt
{
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())
*log << "BIAS " << b.first << " " << m_bias[m->second] << std::endl;
*log << "BIAS " << b.first << " " << m_bias[m->second]
<< std::endl;
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::
operator[](id_type const idx) const
{
// UTIL_THROW_IF2(idx >= m_sid2docid->size(), "Out of bounds: "
// << idx << "/" << m_sid2docid->size());
std::map<id_type, float>::const_iterator m = m_bias.find((*m_sid2docid)[idx]);
std::map<id_type, float>::const_iterator m;
m = m_bias.find((*m_sid2docid)[idx]);
return m != m_bias.end() ? m->second : 0;
}
size_t
DocumentBias::
size() const
{ return m_sid2docid->size(); }
{
return m_sid2docid->size();
}

View File

@ -25,8 +25,8 @@ namespace sapt
int loglevel;
std::ostream* log;
// Map to store the biasmap as you get it from the server:
std::map<std::string, float> m_bias_map;
std::map<std::string, float>& getBiasMap();
SPTR<std::map<std::string, float> > m_bias_map;
SPTR<std::map<std::string, float> const> getBiasMap();
virtual float
operator[](id_type const ID) const = 0;
// returns (unnormalized bias) for the class of item ID