2014-12-02 17:38:03 +03:00
|
|
|
#include "BaseManager.h"
|
2015-05-12 04:28:58 +03:00
|
|
|
#include "StaticData.h"
|
2014-12-02 20:40:53 +03:00
|
|
|
#include "moses/FF/StatelessFeatureFunction.h"
|
|
|
|
#include "moses/FF/StatefulFeatureFunction.h"
|
2015-03-26 19:37:04 +03:00
|
|
|
#include "moses/TranslationTask.h"
|
2014-12-02 20:40:53 +03:00
|
|
|
|
2015-05-12 04:28:58 +03:00
|
|
|
#include <vector>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/iostreams/device/file.hpp>
|
|
|
|
#include <boost/iostreams/filter/bzip2.hpp>
|
|
|
|
#include <boost/iostreams/filter/gzip.hpp>
|
|
|
|
#include <boost/iostreams/filtering_stream.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2014-12-02 20:40:53 +03:00
|
|
|
using namespace std;
|
2014-12-02 17:38:03 +03:00
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
2015-03-26 19:37:04 +03:00
|
|
|
|
2015-04-30 08:05:11 +03:00
|
|
|
BaseManager::BaseManager(ttasksptr const& ttask)
|
2015-03-26 19:37:04 +03:00
|
|
|
: m_ttask(ttask), m_source(*(ttask->GetSource().get()))
|
|
|
|
{ }
|
|
|
|
|
2015-04-30 08:05:11 +03:00
|
|
|
const InputType&
|
|
|
|
BaseManager::GetSource() const
|
2015-05-02 13:45:24 +03:00
|
|
|
{
|
|
|
|
return m_source;
|
|
|
|
}
|
2015-03-26 19:37:04 +03:00
|
|
|
|
2015-07-02 03:32:34 +03:00
|
|
|
const ttasksptr
|
2015-07-13 16:42:59 +03:00
|
|
|
BaseManager::GetTtask() const
|
2015-07-04 02:00:44 +03:00
|
|
|
{
|
2015-06-23 15:24:42 +03:00
|
|
|
return m_ttask.lock();
|
|
|
|
}
|
|
|
|
|
2015-05-12 04:28:58 +03:00
|
|
|
void
|
|
|
|
BaseManager::
|
|
|
|
OutputSearchGraphAsHypergraph(std::ostream& out) const
|
|
|
|
{
|
|
|
|
// This virtual function that may not be implemented everywhere, but it should for
|
|
|
|
// derived classes that use it
|
|
|
|
UTIL_THROW2("Not implemented.");
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:05:43 +03:00
|
|
|
void
|
2015-05-12 04:28:58 +03:00
|
|
|
BaseManager::
|
|
|
|
OutputSearchGraphAsHypergraph(std::string const& fname, size_t const precision) const
|
|
|
|
{
|
|
|
|
std::string odir = boost::filesystem::path(fname).parent_path().string();
|
|
|
|
if (! boost::filesystem::exists(odir))
|
|
|
|
boost::filesystem::create_directory(odir);
|
|
|
|
UTIL_THROW_IF2(!boost::filesystem::is_directory(odir),
|
2015-05-13 13:29:16 +03:00
|
|
|
"Cannot output hypergraphs to " << odir
|
|
|
|
<< " because that path exists but is not a directory.");
|
2015-05-12 04:28:58 +03:00
|
|
|
|
2015-05-13 13:05:43 +03:00
|
|
|
// not clear why we need to output the weights every time we dump a search
|
2015-05-12 04:28:58 +03:00
|
|
|
// graph into a file again, but that's what the old code did.
|
2015-05-13 13:05:43 +03:00
|
|
|
|
2015-05-12 04:28:58 +03:00
|
|
|
string weightsFile = odir + "/weights";
|
|
|
|
TRACE_ERR("The weights file is " << weightsFile << "\n");
|
|
|
|
ofstream weightsOut;
|
|
|
|
weightsOut.open(weightsFile.c_str());
|
|
|
|
weightsOut.setf(std::ios::fixed);
|
|
|
|
weightsOut.precision(6);
|
|
|
|
// just temporarily, till we've implemented weight scoring in the manager
|
|
|
|
// (or the translation task)
|
|
|
|
StaticData::Instance().GetAllWeights().Save(weightsOut);
|
|
|
|
weightsOut.close();
|
2015-05-13 13:05:43 +03:00
|
|
|
|
2015-05-12 04:28:58 +03:00
|
|
|
boost::iostreams::filtering_ostream file;
|
|
|
|
if (boost::ends_with(fname, ".gz"))
|
|
|
|
file.push(boost::iostreams::gzip_compressor());
|
|
|
|
else if (boost::ends_with(fname, ".bz2"))
|
|
|
|
file.push( boost::iostreams::bzip2_compressor() );
|
|
|
|
file.push( boost::iostreams::file_sink(fname, ios_base::out) );
|
2015-05-13 13:29:16 +03:00
|
|
|
if (file.is_complete() && file.good()) {
|
|
|
|
file.setf(std::ios::fixed);
|
|
|
|
file.precision(precision);
|
|
|
|
this->OutputSearchGraphAsHypergraph(file);
|
|
|
|
file.flush();
|
|
|
|
} else {
|
|
|
|
TRACE_ERR("Cannot output hypergraph for line "
|
|
|
|
<< this->GetSource().GetTranslationId()
|
|
|
|
<< " because the output file " << fname
|
|
|
|
<< " is not open or not ready for writing"
|
|
|
|
<< std::endl);
|
|
|
|
}
|
2015-05-12 04:28:58 +03:00
|
|
|
file.pop();
|
|
|
|
}
|
2015-03-26 19:37:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-02 22:09:10 +03:00
|
|
|
/***
|
|
|
|
* print surface factor only for the given phrase
|
|
|
|
*/
|
|
|
|
void BaseManager::OutputSurface(std::ostream &out, const Phrase &phrase,
|
2015-01-14 14:07:42 +03:00
|
|
|
const std::vector<FactorType> &outputFactorOrder,
|
|
|
|
bool reportAllFactors) const
|
2014-12-02 22:09:10 +03:00
|
|
|
{
|
|
|
|
UTIL_THROW_IF2(outputFactorOrder.size() == 0,
|
2015-01-14 14:07:42 +03:00
|
|
|
"Cannot be empty phrase");
|
2014-12-02 22:09:10 +03:00
|
|
|
if (reportAllFactors == true) {
|
|
|
|
out << phrase;
|
|
|
|
} else {
|
|
|
|
size_t size = phrase.GetSize();
|
|
|
|
for (size_t pos = 0 ; pos < size ; pos++) {
|
|
|
|
const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[0]);
|
|
|
|
out << *factor;
|
|
|
|
UTIL_THROW_IF2(factor == NULL,
|
2015-01-14 14:07:42 +03:00
|
|
|
"Empty factor 0 at position " << pos);
|
2014-12-02 22:09:10 +03:00
|
|
|
|
|
|
|
for (size_t i = 1 ; i < outputFactorOrder.size() ; i++) {
|
|
|
|
const Factor *factor = phrase.GetFactor(pos, outputFactorOrder[i]);
|
|
|
|
UTIL_THROW_IF2(factor == NULL,
|
2015-01-14 14:07:42 +03:00
|
|
|
"Empty factor " << i << " at position " << pos);
|
2014-12-02 22:09:10 +03:00
|
|
|
|
|
|
|
out << "|" << *factor;
|
|
|
|
}
|
|
|
|
out << " ";
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 17:38:03 +03:00
|
|
|
}
|
|
|
|
|
2014-12-03 20:04:10 +03:00
|
|
|
// Emulates the old operator<<(ostream &, const DottedRule &) function. The
|
|
|
|
// output format is a bit odd (reverse order and double spacing between symbols)
|
|
|
|
// but there are scripts and tools that expect the output of -T to look like
|
|
|
|
// that.
|
|
|
|
void BaseManager::WriteApplicationContext(std::ostream &out,
|
2015-01-14 14:07:42 +03:00
|
|
|
const ApplicationContext &context) const
|
2014-12-03 20:04:10 +03:00
|
|
|
{
|
|
|
|
assert(!context.empty());
|
|
|
|
ApplicationContext::const_reverse_iterator p = context.rbegin();
|
|
|
|
while (true) {
|
|
|
|
out << p->second << "=" << p->first << " ";
|
|
|
|
if (++p == context.rend()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-02 22:09:10 +03:00
|
|
|
} // namespace
|
|
|
|
|
2014-12-02 17:38:03 +03:00
|
|
|
|