00001 #include "BaseManager.h"
00002 #include "StaticData.h"
00003 #include "moses/FF/StatelessFeatureFunction.h"
00004 #include "moses/FF/StatefulFeatureFunction.h"
00005 #include "moses/TranslationTask.h"
00006
00007 #include <vector>
00008 #include <boost/algorithm/string/predicate.hpp>
00009 #include <boost/iostreams/device/file.hpp>
00010 #include <boost/iostreams/filter/bzip2.hpp>
00011 #include <boost/iostreams/filter/gzip.hpp>
00012 #include <boost/iostreams/filtering_stream.hpp>
00013 #include <boost/filesystem.hpp>
00014
00015 using namespace std;
00016
00017 namespace Moses
00018 {
00019
00020 BaseManager::BaseManager(ttasksptr const& ttask)
00021 : m_ttask(ttask), m_source(*(ttask->GetSource().get()))
00022 { }
00023
00024 const InputType&
00025 BaseManager::GetSource() const
00026 {
00027 return m_source;
00028 }
00029
00030 const ttasksptr
00031 BaseManager::GetTtask() const
00032 {
00033 return m_ttask.lock();
00034 }
00035
00036 void
00037 BaseManager::
00038 OutputSearchGraphAsHypergraph(std::ostream& out) const
00039 {
00040
00041
00042 UTIL_THROW2("Not implemented.");
00043 }
00044
00045 void
00046 BaseManager::
00047 OutputSearchGraphAsHypergraph(std::string const& fname, size_t const precision) const
00048 {
00049 std::string odir = boost::filesystem::path(fname).parent_path().string();
00050 if (! boost::filesystem::exists(odir))
00051 boost::filesystem::create_directory(odir);
00052 UTIL_THROW_IF2(!boost::filesystem::is_directory(odir),
00053 "Cannot output hypergraphs to " << odir
00054 << " because that path exists but is not a directory.");
00055
00056
00057
00058
00059 string weightsFile = odir + "/weights";
00060 TRACE_ERR("The weights file is " << weightsFile << "\n");
00061 ofstream weightsOut;
00062 weightsOut.open(weightsFile.c_str());
00063 weightsOut.setf(std::ios::fixed);
00064 weightsOut.precision(6);
00065
00066
00067 StaticData::Instance().GetAllWeights().Save(weightsOut);
00068 weightsOut.close();
00069
00070 boost::iostreams::filtering_ostream file;
00071 if (boost::ends_with(fname, ".gz"))
00072 file.push(boost::iostreams::gzip_compressor());
00073 else if (boost::ends_with(fname, ".bz2"))
00074 file.push( boost::iostreams::bzip2_compressor() );
00075 file.push( boost::iostreams::file_sink(fname, ios_base::out) );
00076 if (file.is_complete() && file.good()) {
00077 file.setf(std::ios::fixed);
00078 file.precision(precision);
00079 this->OutputSearchGraphAsHypergraph(file);
00080 file.flush();
00081 } else {
00082 TRACE_ERR("Cannot output hypergraph for line "
00083 << this->GetSource().GetTranslationId()
00084 << " because the output file " << fname
00085 << " is not open or not ready for writing"
00086 << std::endl);
00087 }
00088 file.pop();
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 void
00098 BaseManager::
00099 OutputSurface(std::ostream &out, Phrase const& phrase) const
00100 {
00101 std::vector<FactorType> const& factor_order = options()->output.factor_order;
00102
00103 bool markUnknown = options()->unk.mark;
00104 std::string const& fd = options()->output.factor_delimiter;
00105
00106 size_t size = phrase.GetSize();
00107 for (size_t pos = 0 ; pos < size ; pos++) {
00108 const Factor *factor = phrase.GetFactor(pos, factor_order[0]);
00109 UTIL_THROW_IF2(factor == NULL, "Empty factor 0 at position " << pos);
00110
00111 const Word &word = phrase.GetWord(pos);
00112 if(markUnknown && word.IsOOV()) {
00113 out << options()->unk.prefix;
00114 }
00115
00116 out << *factor;
00117
00118 for (size_t i = 1 ; i < factor_order.size() ; i++) {
00119 const Factor *factor = phrase.GetFactor(pos, factor_order[i]);
00120 UTIL_THROW_IF2(!factor, "Empty factor " << i << " at position " << pos);
00121 out << fd << *factor;
00122 }
00123
00124 if(markUnknown && word.IsOOV()) {
00125 out << options()->unk.suffix;
00126 }
00127
00128 out << " ";
00129 }
00130 }
00131
00132
00133
00134
00135
00136 void BaseManager::WriteApplicationContext(std::ostream &out,
00137 const ApplicationContext &context) const
00138 {
00139 assert(!context.empty());
00140 ApplicationContext::const_reverse_iterator p = context.rbegin();
00141 while (true) {
00142 out << p->second << "=" << p->first << " ";
00143 if (++p == context.rend()) {
00144 break;
00145 }
00146 out << " ";
00147 }
00148 }
00149
00150 AllOptions::ptr const&
00151 BaseManager::
00152 options() const
00153 {
00154 return GetTtask()->options();
00155 }
00156
00157
00158 }
00159
00160