File size: 1,836 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 |
#include "xml_tree_parser.h"
#include <cassert>
#include <vector>
#include "util/tokenize.hh"
#include "SyntaxTree.h"
#include "tables-core.h"
#include "XmlException.h"
#include "XmlTree.h"
#include "exception.h"
namespace MosesTraining {
namespace Syntax {
std::auto_ptr<SyntaxTree> XmlTreeParser::Parse(const std::string &line,
bool unescape)
{
sentence_ = line;
node_collection_.Clear();
try {
if (!ProcessAndStripXMLTags(sentence_, node_collection_, label_set_,
top_label_set_, unescape)) {
throw Exception("");
}
} catch (const XmlException &e) {
throw Exception(e.getMsg());
}
std::auto_ptr<SyntaxTree> root = node_collection_.ExtractTree();
words_ = util::tokenize(sentence_);
AttachWords(words_, *root);
return root;
}
void XmlTreeParser::AttachWords(const std::vector<std::string> &words,
SyntaxTree &root)
{
std::vector<SyntaxTree*> leaves;
leaves.reserve(words.size());
for (SyntaxTree::LeafIterator p(root); p != SyntaxTree::LeafIterator(); ++p) {
leaves.push_back(&*p);
}
std::vector<std::string>::const_iterator q = words.begin();
for (std::vector<SyntaxTree*>::iterator p = leaves.begin(); p != leaves.end();
++p) {
SyntaxTree *leaf = *p;
const int start = leaf->value().start;
const int end = leaf->value().end;
if (start != end) {
std::ostringstream msg;
msg << "leaf node covers multiple words (" << start << "-" << end
<< "): this is currently unsupported";
throw Exception(msg.str());
}
SyntaxTree *newLeaf = new SyntaxTree(SyntaxNode(*q++, start, end));
leaf->children().push_back(newLeaf);
newLeaf->parent() = leaf;
}
}
} // namespace Syntax
} // namespace MosesTraining
|