mosesdecoder/mert/InternalTree.cpp

111 lines
2.3 KiB
C++
Raw Normal View History

2015-07-17 19:27:31 +03:00
#include "InternalTree.h"
namespace MosesTuning
{
InternalTree::InternalTree(const std::string & line, const bool terminal):
2015-07-21 02:00:42 +03:00
m_isTerminal(terminal)
{
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
size_t found = line.find_first_of("[] ");
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
if (found == line.npos) {
m_value = line;
}
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
else {
AddSubTree(line, 0);
}
2015-07-17 19:27:31 +03:00
}
2015-07-21 02:00:42 +03:00
size_t InternalTree::AddSubTree(const std::string & line, size_t pos)
{
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
std::string value;
char token = 0;
while (token != ']' && pos != std::string::npos) {
size_t oldpos = pos;
pos = line.find_first_of("[] ", pos);
if (pos == std::string::npos) break;
token = line[pos];
value = line.substr(oldpos,pos-oldpos);
if (token == '[') {
if (m_value.size() > 0) {
m_children.push_back(boost::make_shared<InternalTree>(value,false));
pos = m_children.back()->AddSubTree(line, pos+1);
} else {
if (value.size() > 0) {
m_value = value;
2015-07-17 19:27:31 +03:00
}
2015-07-21 02:00:42 +03:00
pos = AddSubTree(line, pos+1);
}
} else if (token == ' ' || token == ']') {
if (value.size() > 0 && !(m_value.size() > 0)) {
m_value = value;
} else if (value.size() > 0) {
m_isTerminal = false;
m_children.push_back(boost::make_shared<InternalTree>(value,true));
}
if (token == ' ') {
pos++;
}
2015-07-17 19:27:31 +03:00
}
2015-07-21 02:00:42 +03:00
if (m_children.size() > 0) {
m_isTerminal = false;
2015-07-17 19:27:31 +03:00
}
2015-07-21 02:00:42 +03:00
}
if (pos == std::string::npos) {
return line.size();
}
return std::min(line.size(),pos+1);
2015-07-17 19:27:31 +03:00
}
2015-07-21 02:00:42 +03:00
std::string InternalTree::GetString(bool start) const
{
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
std::string ret = "";
if (!start) {
ret += " ";
}
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
if (!m_isTerminal) {
ret += "[";
}
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
ret += m_value;
for (std::vector<TreePointer>::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
ret += (*it)->GetString(false);
}
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
if (!m_isTerminal) {
ret += "]";
}
return ret;
2015-07-17 19:27:31 +03:00
}
2015-07-21 02:00:42 +03:00
void InternalTree::Combine(const std::vector<TreePointer> &previous)
{
2015-07-17 19:27:31 +03:00
2015-07-21 02:00:42 +03:00
std::vector<TreePointer>::iterator it;
bool found = false;
leafNT next_leafNT(this);
for (std::vector<TreePointer>::const_iterator it_prev = previous.begin(); it_prev != previous.end(); ++it_prev) {
found = next_leafNT(it);
if (found) {
*it = *it_prev;
} else {
std::cerr << "Warning: leaf nonterminal not found in rule; why did this happen?\n";
2015-07-17 19:27:31 +03:00
}
2015-07-21 02:00:42 +03:00
}
2015-07-17 19:27:31 +03:00
}
}