File size: 2,590 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "DerivationWriter.h"

#include "moses/Factor.h"
#include "moses/Syntax/PVertex.h"
#include "moses/Syntax/SHyperedge.h"

namespace Moses
{
namespace Syntax
{
namespace F2S
{

// 1-best version.
void DerivationWriter::Write(const SHyperedge &shyperedge,
                             std::size_t sentNum, std::ostream &out)
{
  WriteLine(shyperedge, sentNum, out);
  for (std::size_t i = 0; i < shyperedge.tail.size(); ++i) {
    const SVertex &pred = *(shyperedge.tail[i]);
    if (pred.best) {
      Write(*pred.best, sentNum, out);
    }
  }
}

// k-best derivation.
void DerivationWriter::Write(const KBestExtractor::Derivation &derivation,
                             std::size_t sentNum, std::ostream &out)
{
  WriteLine(derivation.edge->shyperedge, sentNum, out);
  for (std::size_t i = 0; i < derivation.subderivations.size(); ++i) {
    Write(*(derivation.subderivations[i]), sentNum, out);
  }
}

void DerivationWriter::WriteLine(const SHyperedge &shyperedge,
                                 std::size_t sentNum, std::ostream &out)
{
  // Sentence number.
  out << sentNum << " |||";

  // Source LHS.
  out << " ";
  WriteSymbol(shyperedge.head->pvertex->symbol, out);
  out << " ->";

  // Source RHS symbols.
  for (std::size_t i = 0; i < shyperedge.tail.size(); ++i) {
    const Word &symbol = shyperedge.tail[i]->pvertex->symbol;
    out << " ";
    WriteSymbol(symbol, out);
  }
  out << " |||";

  // Target RHS.
  out << " [X] ->";

  // Target RHS symbols.
  const TargetPhrase &phrase = *(shyperedge.label.translation);
  for (std::size_t i = 0; i < phrase.GetSize(); ++i) {
    const Word &symbol = phrase.GetWord(i);
    out << " ";
    if (symbol.IsNonTerminal()) {
      out << "[X]";
    } else {
      WriteSymbol(symbol, out);
    }
  }
  out << " |||";

  // Non-terminal alignments
  const AlignmentInfo &a = phrase.GetAlignNonTerm();
  for (AlignmentInfo::const_iterator p = a.begin(); p != a.end(); ++p) {
    out << " " << p->first << "-" << p->second;
  }
  out << " |||";

  // Spans covered by source RHS symbols.
  for (std::size_t i = 0; i < shyperedge.tail.size(); ++i) {
    const SVertex *child = shyperedge.tail[i];
    const Range &span = child->pvertex->span;
    out << " " << span.GetStartPos() << ".." << span.GetEndPos();
  }

  out << "\n";
}

void DerivationWriter::WriteSymbol(const Word &symbol, std::ostream &out)
{
  const Factor *f = symbol[0];
  if (symbol.IsNonTerminal()) {
    out << "[" << f->GetString() << "]";
  } else {
    out << f->GetString();
  }
}

}  // namespace F2S
}  // namespace Syntax
}  // namespace Moses