Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlrich Germann <ugermann@inf.ed.ac.uk>2015-05-12 04:28:58 +0300
committerUlrich Germann <ugermann@inf.ed.ac.uk>2015-05-12 04:28:58 +0300
commitd122605c0d1a44d92526cccaeff640eee695aa62 (patch)
tree74b2de0c05adf515aa684fc7713993f5d618aa1b /moses/BaseManager.cpp
parentf087fce65eae426fa38efdea55405753992f10f7 (diff)
Code reorganization with respect to hypergraph output.
Diffstat (limited to 'moses/BaseManager.cpp')
-rw-r--r--moses/BaseManager.cpp69
1 files changed, 66 insertions, 3 deletions
diff --git a/moses/BaseManager.cpp b/moses/BaseManager.cpp
index 83d48e6e4..70bacdede 100644
--- a/moses/BaseManager.cpp
+++ b/moses/BaseManager.cpp
@@ -1,11 +1,17 @@
-#include <vector>
-
-#include "StaticData.h"
#include "BaseManager.h"
+#include "StaticData.h"
#include "moses/FF/StatelessFeatureFunction.h"
#include "moses/FF/StatefulFeatureFunction.h"
#include "moses/TranslationTask.h"
+#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>
+
using namespace std;
namespace Moses
@@ -21,6 +27,63 @@ BaseManager::GetSource() const
return m_source;
}
+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.");
+}
+
+void
+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),
+ "Cannot output hypergraphs to " << odir
+ << " because that path exists but is not a directory.");
+
+ // not clear why we need to output the weights every time we dump a search
+ // graph into a file again, but that's what the old code did.
+
+ 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();
+
+ 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) );
+ 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);
+ }
+ file.pop();
+}