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 F2S
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 << " ";
00045 WriteSymbol(shyperedge.head->pvertex->symbol, out);
00046 out << " ->";
00047
00048
00049 for (std::size_t i = 0; i < shyperedge.tail.size(); ++i) {
00050 const Word &symbol = shyperedge.tail[i]->pvertex->symbol;
00051 out << " ";
00052 WriteSymbol(symbol, out);
00053 }
00054 out << " |||";
00055
00056
00057 out << " [X] ->";
00058
00059
00060 const TargetPhrase &phrase = *(shyperedge.label.translation);
00061 for (std::size_t i = 0; i < phrase.GetSize(); ++i) {
00062 const Word &symbol = phrase.GetWord(i);
00063 out << " ";
00064 if (symbol.IsNonTerminal()) {
00065 out << "[X]";
00066 } else {
00067 WriteSymbol(symbol, out);
00068 }
00069 }
00070 out << " |||";
00071
00072
00073 const AlignmentInfo &a = phrase.GetAlignNonTerm();
00074 for (AlignmentInfo::const_iterator p = a.begin(); p != a.end(); ++p) {
00075 out << " " << p->first << "-" << p->second;
00076 }
00077 out << " |||";
00078
00079
00080 for (std::size_t i = 0; i < shyperedge.tail.size(); ++i) {
00081 const SVertex *child = shyperedge.tail[i];
00082 const Range &span = child->pvertex->span;
00083 out << " " << span.GetStartPos() << ".." << span.GetEndPos();
00084 }
00085
00086 out << "\n";
00087 }
00088
00089 void DerivationWriter::WriteSymbol(const Word &symbol, std::ostream &out)
00090 {
00091 const Factor *f = symbol[0];
00092 if (symbol.IsNonTerminal()) {
00093 out << "[" << f->GetString() << "]";
00094 } else {
00095 out << f->GetString();
00096 }
00097 }
00098
00099 }
00100 }
00101 }