summaryrefslogtreecommitdiff
path: root/klm/alone/assemble.cc
blob: 2ae72ce9918a6f6674a88ef0a0707248cc6e6685 (plain)
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
#include "alone/assemble.hh"

#include "alone/labeled_edge.hh"
#include "search/final.hh"

#include <iostream>

namespace alone {

std::ostream &operator<<(std::ostream &o, const search::Final &final) {
  const std::vector<const std::string*> &words = static_cast<const LabeledEdge&>(final.From()).Words();
  if (words.empty()) return o;
  const search::Final *const *child = final.Children().data();
  std::vector<const std::string*>::const_iterator i(words.begin());
  for (; i != words.end() - 1; ++i) {
    if (*i) {
      o << **i << ' ';
    } else {
      o << **child << ' ';
      ++child;
    }
  }

  if (*i) {
    if (**i != "</s>") {
      o << **i;
    }
  } else {
    o << **child;
  }

  return o;
}

namespace {

void MakeIndent(std::ostream &o, const char *indent_str, unsigned int level) {
  for (unsigned int i = 0; i < level; ++i)
    o << indent_str;
}

void DetailedFinalInternal(std::ostream &o, const search::Final &final, const char *indent_str, unsigned int indent) {
  o << "(\n";
  MakeIndent(o, indent_str, indent);
  const std::vector<const std::string*> &words = static_cast<const LabeledEdge&>(final.From()).Words();
  const search::Final *const *child = final.Children().data();
  for (std::vector<const std::string*>::const_iterator i(words.begin()); i != words.end(); ++i) {
    if (*i) {
      o << **i;
      if (i == words.end() - 1) {
        o << '\n';
        MakeIndent(o, indent_str, indent);
      } else {
        o << ' ';
      }
    } else {
      // One extra indent from the line we're currently on.  
      o << indent_str;
      DetailedFinalInternal(o, **child, indent_str, indent + 1);
      for (unsigned int i = 0; i < indent; ++i) o << indent_str;
      ++child;
    }
  }
  o << ")=" << final.Bound() << '\n';
}
} // namespace

void DetailedFinal(std::ostream &o, const search::Final &final, const char *indent_str) {
  DetailedFinalInternal(o, final, indent_str, 0);
}

void PrintFinal(const search::Final &final) {
  std::cout << final << std::endl;
}

} // namespace alone