00001 #include "DerivationWriter.h"
00002
00003 #include "moses/Factor.h"
00004 #include "moses/Syntax/PVertex.h"
00005 #include "moses/Syntax/SHyperedge.h"
00006
00007 namespace Moses
00008 {
00009 namespace Syntax
00010 {
00011 namespace S2T
00012 {
00013
00014
00015 void DerivationWriter::Write(const SHyperedge ­peredge,
00016 std::size_t sentNum, std::ostream &out)
00017 {
00018 WriteLine(shyperedge, sentNum, out);
00019 for (std::size_t i = 0; i < shyperedge.tail.size(); ++i) {
00020 const SVertex &pred = *(shyperedge.tail[i]);
00021 if (pred.best) {
00022 Write(*pred.best, sentNum, out);
00023 }
00024 }
00025 }
00026
00027
00028 void DerivationWriter::Write(const KBestExtractor::Derivation &derivation,
00029 std::size_t sentNum, std::ostream &out)
00030 {
00031 WriteLine(derivation.edge->shyperedge, sentNum, out);
00032 for (std::size_t i = 0; i < derivation.subderivations.size(); ++i) {
00033 Write(*(derivation.subderivations[i]), sentNum, out);
00034 }
00035 }
00036
00037 void DerivationWriter::WriteLine(const SHyperedge ­peredge,
00038 std::size_t sentNum, std::ostream &out)
00039 {
00040
00041 out << sentNum << " |||";
00042
00043
00044 out << " [X] ->";
00045
00046
00047 for (std::size_t i = 0; i < shyperedge.tail.size(); ++i) {
00048 const Word &symbol = shyperedge.tail[i]->pvertex->symbol;
00049 out << " ";
00050 if (symbol.IsNonTerminal()) {
00051 out << "[X]";
00052 } else {
00053 WriteSymbol(symbol, out);
00054 }
00055 }
00056 out << " |||";
00057
00058
00059 out << " ";
00060 WriteSymbol(shyperedge.head->pvertex->symbol, out);
00061 out << " ->";
00062
00063
00064 const TargetPhrase &phrase = *(shyperedge.label.translation);
00065 for (std::size_t i = 0; i < phrase.GetSize(); ++i) {
00066 out << " ";
00067 WriteSymbol(phrase.GetWord(i), out);
00068 }
00069 out << " |||";
00070
00071
00072 const AlignmentInfo &a = phrase.GetAlignNonTerm();
00073 for (AlignmentInfo::const_iterator p = a.begin(); p != a.end(); ++p) {
00074 out << " " << p->first << "-" << p->second;
00075 }
00076 out << " |||";
00077
00078
00079 for (std::size_t i = 0; i < shyperedge.tail.size(); ++i) {
00080 const SVertex *child = shyperedge.tail[i];
00081 const Range &span = child->pvertex->span;
00082 out << " " << span.GetStartPos() << ".." << span.GetEndPos();
00083 }
00084
00085 out << "\n";
00086 }
00087
00088 void DerivationWriter::WriteSymbol(const Word &symbol, std::ostream &out)
00089 {
00090 const Factor *f = symbol[0];
00091 if (symbol.IsNonTerminal()) {
00092 out << "[" << f->GetString() << "]";
00093 } else {
00094 out << f->GetString();
00095 }
00096 }
00097
00098 }
00099 }
00100 }