00001 #include "xml_tree_parser.h"
00002
00003 #include <cassert>
00004 #include <vector>
00005
00006 #include "util/tokenize.hh"
00007
00008 #include "SyntaxTree.h"
00009 #include "tables-core.h"
00010 #include "XmlException.h"
00011 #include "XmlTree.h"
00012
00013 #include "exception.h"
00014
00015 namespace MosesTraining {
00016 namespace Syntax {
00017
00018 std::auto_ptr<SyntaxTree> XmlTreeParser::Parse(const std::string &line,
00019 bool unescape)
00020 {
00021 sentence_ = line;
00022 node_collection_.Clear();
00023 try {
00024 if (!ProcessAndStripXMLTags(sentence_, node_collection_, label_set_,
00025 top_label_set_, unescape)) {
00026 throw Exception("");
00027 }
00028 } catch (const XmlException &e) {
00029 throw Exception(e.getMsg());
00030 }
00031 std::auto_ptr<SyntaxTree> root = node_collection_.ExtractTree();
00032 words_ = util::tokenize(sentence_);
00033 AttachWords(words_, *root);
00034 return root;
00035 }
00036
00037 void XmlTreeParser::AttachWords(const std::vector<std::string> &words,
00038 SyntaxTree &root)
00039 {
00040 std::vector<SyntaxTree*> leaves;
00041 leaves.reserve(words.size());
00042 for (SyntaxTree::LeafIterator p(root); p != SyntaxTree::LeafIterator(); ++p) {
00043 leaves.push_back(&*p);
00044 }
00045
00046 std::vector<std::string>::const_iterator q = words.begin();
00047 for (std::vector<SyntaxTree*>::iterator p = leaves.begin(); p != leaves.end();
00048 ++p) {
00049 SyntaxTree *leaf = *p;
00050 const int start = leaf->value().start;
00051 const int end = leaf->value().end;
00052 if (start != end) {
00053 std::ostringstream msg;
00054 msg << "leaf node covers multiple words (" << start << "-" << end
00055 << "): this is currently unsupported";
00056 throw Exception(msg.str());
00057 }
00058 SyntaxTree *newLeaf = new SyntaxTree(SyntaxNode(*q++, start, end));
00059 leaf->children().push_back(newLeaf);
00060 newLeaf->parent() = leaf;
00061 }
00062 }
00063
00064 }
00065 }