Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RuleTableParser.cpp « compact-rule-table « training « scripts - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6272079fdca4252ea698c3824957bfa0ef740642 (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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "RuleTableParser.h"

#include "Exception.h"

#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>

#include <iostream>

#include <istream>
#include <string>

namespace moses {

RuleTableParser::RuleTableParser()
  : m_input(0) {
}

RuleTableParser::RuleTableParser(std::istream &input)
  : m_input(&input) {
  ++(*this);
}

RuleTableParser & RuleTableParser::operator++() {
  if (!m_input) {
    return *this;
  }
  if (!std::getline(*m_input, m_line)) {
    m_input = 0;
    return *this;
  }
  parseLine(m_line);
  return *this;
}

RuleTableParser RuleTableParser::operator++(int) {
  RuleTableParser tmp(*this);
  ++(*this);
  return tmp;
}

void RuleTableParser::parseLine(const std::string &line) {
  // Source symbols
  size_t pos = line.find("|||");
  if (pos == std::string::npos) {
    throw Exception("missing first delimiter");
  }
  std::string text = line.substr(0, pos);
  boost::trim(text);
  m_value.sourceRhs.clear();
  boost::split(m_value.sourceRhs, text, boost::algorithm::is_space(),
               boost::algorithm::token_compress_on);
  m_value.sourceLhs = m_value.sourceRhs.back();
  m_value.sourceRhs.pop_back();
  std::for_each(m_value.sourceRhs.begin(), m_value.sourceRhs.end(),
                trimPairedSymbolFromRight);

  // Target symbols
  size_t begin = pos+3;
  pos = line.find("|||", begin);
  if (pos == std::string::npos) {
    throw Exception("missing second delimiter");
  }
  text = line.substr(begin, pos-begin);
  boost::trim(text);
  m_value.targetRhs.clear();
  boost::split(m_value.targetRhs, text, boost::algorithm::is_space(),
               boost::algorithm::token_compress_on);
  m_value.targetLhs = m_value.targetRhs.back();
  m_value.targetRhs.pop_back();
  std::for_each(m_value.targetRhs.begin(), m_value.targetRhs.end(),
                trimPairedSymbolFromLeft);

  // Scores
  begin = pos+3;
  pos = line.find("|||", begin);
  if (pos == std::string::npos) {
    throw Exception("missing third delimiter");
  }
  text = line.substr(begin, pos-begin);
  boost::trim(text);
  m_value.scores.clear();
  boost::split(m_value.scores, text, boost::algorithm::is_space(),
               boost::algorithm::token_compress_on);

  // Alignments
  begin = pos+3;
  pos = line.find("|||", begin);
  if (pos == std::string::npos) {
    throw Exception("missing fourth delimiter");
  }
  text = line.substr(begin, pos-begin);
  m_value.alignments.clear();
  boost::trim(text);
  // boost::split behaves differently between versions on empry strings
  if (!text.empty()) {
    tmpStringVec.clear();
    boost::split(tmpStringVec, text, boost::algorithm::is_space(),
                 boost::algorithm::token_compress_on);
    for (std::vector<std::string>::const_iterator p = tmpStringVec.begin();
         p != tmpStringVec.end(); ++p) {
      assert(!p->empty());
      std::vector<std::string> tmpVec;
      tmpVec.reserve(2);
      boost::split(tmpVec, *p, boost::algorithm::is_any_of("-"));
      if (tmpVec.size() != 2) {
        throw Exception("bad alignment pair");
      }
      std::pair<int, int> alignmentPair;
      alignmentPair.first = boost::lexical_cast<int>(tmpVec[0]);
      alignmentPair.second = boost::lexical_cast<int>(tmpVec[1]);
      m_value.alignments.insert(alignmentPair);
    }
  }

  // Counts + everything else (the 'tail')
  begin = pos+3;
  pos = line.find("|||", begin);
  if (pos == std::string::npos) {
    text = line.substr(begin);
    m_value.tail.clear();
  } else {
    text = line.substr(begin, pos-begin);
    m_value.tail = line.substr(pos+3);
  }
  boost::trim(text);
  m_value.counts.clear();
  boost::split(m_value.counts, text, boost::algorithm::is_space(),
               boost::algorithm::token_compress_on);
}

void RuleTableParser::trimPairedSymbolFromLeft(std::string &s) {
  size_t len = s.size();
  if (len < 2 || s[0] != '[' || s[len-1] != ']') {
    return;
  }
  size_t pos = s.find('[', 1);
  if (pos == std::string::npos) {
    std::ostringstream msg;
    msg << "malformed non-terminal pair: " << s;
    throw Exception(msg.str());
  }
  s.erase(0, pos);
}

void RuleTableParser::trimPairedSymbolFromRight(std::string &s) {
  size_t len = s.size();
  if (len < 2 || s[0] != '[' || s[len-1] != ']') {
    return;
  }
  size_t pos = s.find('[', 1);
  if (pos == std::string::npos) {
    std::ostringstream msg;
    msg << "malformed non-terminal pair: " << s;
    throw Exception(msg.str());
  }
  s.resize(pos);
}

bool operator==(const RuleTableParser &lhs, const RuleTableParser &rhs) {
  return lhs.m_input == rhs.m_input;
}

bool operator!=(const RuleTableParser &lhs, const RuleTableParser &rhs) {
  return !(lhs == rhs);
}

}  // namespace moses