00001 #include "StsgRuleWriter.h"
00002
00003 #include <cassert>
00004 #include <cmath>
00005 #include <ostream>
00006 #include <map>
00007 #include <sstream>
00008 #include <vector>
00009
00010 #include "Alignment.h"
00011 #include "Options.h"
00012 #include "StsgRule.h"
00013
00014 namespace MosesTraining
00015 {
00016 namespace Syntax
00017 {
00018 namespace GHKM
00019 {
00020
00021 void StsgRuleWriter::Write(const StsgRule &rule)
00022 {
00023 std::ostringstream sourceSS;
00024 std::ostringstream targetSS;
00025
00026
00027 const std::vector<Symbol> &sourceSide = rule.GetSourceSide();
00028 for (std::size_t i = 0; i < sourceSide.size(); ++i) {
00029 const Symbol &symbol = sourceSide[i];
00030 if (i > 0) {
00031 sourceSS << " ";
00032 }
00033 if (symbol.GetType() == NonTerminal) {
00034 sourceSS << "[X]";
00035 } else {
00036 sourceSS << symbol.GetValue();
00037 }
00038 }
00039
00040
00041 rule.GetTargetSide().PrintTree(targetSS);
00042
00043
00044 if (m_options.t2s) {
00045
00046 m_fwd << targetSS.str() << " ||| " << sourceSS.str() << " |||";
00047 m_inv << sourceSS.str() << " ||| " << targetSS.str() << " |||";
00048 } else {
00049 m_fwd << sourceSS.str() << " ||| " << targetSS.str() << " |||";
00050 m_inv << targetSS.str() << " ||| " << sourceSS.str() << " |||";
00051 }
00052
00053
00054 const std::vector<int> &nonTermAlignment = rule.GetNonTermAlignment();
00055 for (int srcIndex = 0; srcIndex < nonTermAlignment.size(); ++srcIndex) {
00056 int tgtIndex = nonTermAlignment[srcIndex];
00057 if (m_options.t2s) {
00058
00059 m_fwd << " " << tgtIndex << "-" << srcIndex;
00060 m_inv << " " << srcIndex << "-" << tgtIndex;
00061 } else {
00062 m_fwd << " " << srcIndex << "-" << tgtIndex;
00063 m_inv << " " << tgtIndex << "-" << srcIndex;
00064 }
00065 }
00066 m_fwd << " |||";
00067 m_inv << " |||";
00068
00069
00070 const Alignment &alignment = rule.GetAlignment();
00071 for (Alignment::const_iterator p = alignment.begin();
00072 p != alignment.end(); ++p) {
00073 if (m_options.t2s) {
00074
00075 m_fwd << " " << p->second << "-" << p->first;
00076 m_inv << " " << p->first << "-" << p->second;
00077 } else {
00078 m_fwd << " " << p->first << "-" << p->second;
00079 m_inv << " " << p->second << "-" << p->first;
00080 }
00081 }
00082
00083
00084 m_fwd << " ||| 1";
00085 m_inv << " ||| 1";
00086
00087
00088 if (m_options.pcfg) {
00089 m_fwd << " ||| " << std::exp(rule.GetTargetSide().GetPcfgScore());
00090 }
00091
00092 m_fwd << std::endl;
00093 m_inv << std::endl;
00094 }
00095
00096 }
00097 }
00098 }