#include "resultReader.h" #include #include using namespace std; template ResultReader::ResultReader(Grammar const &g, Data &d, void (*start_int_node)(string, vector &, int,double,int,int,Data &), void (*end_int_node)(vector &, vector &, Data &), void (*leaf)(string, vector &, Data &)) :_g(g), _d(d), _start_int_node_funct(start_int_node), _end_int_node_funct(end_int_node), _leaf_funct(leaf) { } template ResultReader::~ResultReader() { } template void ResultReader::readResult(string treestring) { string rest; vector ancestors; if (isdigit(treestring[1])) rest = treestring; else rest = treestring.substr(treestring.find(' ')+1); ancestors.push_back(tToken("^")); subtreepair(rest, 1, ancestors); return; } template string ResultReader::subtreepair(string const &rest, int level, vector &ancestors) { // parse first child string next(subtree(rest, level, ancestors)); vector children; // if there is a second child, parse that if (next.size() > 1 && next.compare(0,2, " (") == 0) { children.push_back(tToken(ancestors.back())); ancestors.pop_back(); //first child is not an ancestor of 2nd child next = subtree(next.substr(1), level, ancestors); } children.push_back(tToken(ancestors.back())); ancestors.pop_back(); // this subtree done, pop back up a level // finished node's children: function if (_end_int_node_funct != NULL) _end_int_node_funct(children, ancestors, _d); // return next; } template string const ResultReader::subtree(string const &rest, int level, vector &ancestors) { if (rest.compare(0,2,"(\"") == 0) { int quoteindex = rest.find("\"", 2); while (quoteindex != string::npos && rest[quoteindex-1] == '\\') { quoteindex = rest.find("\"", quoteindex+1); //skip escaped quotes } if (quoteindex == string::npos) { cerr << "something wrong finding leaf in:" << rest << endl; exit(1); } string s1 = rest.substr(2, quoteindex-2); // function to deal with finding a leaf if (_leaf_funct != NULL) _leaf_funct(s1, ancestors, _d); // end function to deal with leaf ancestors.push_back(tToken("\""+s1+"\"")); int dummy; string lookahead, r1; istringstream(rest.substr(quoteindex+1)) >> dummy >> lookahead; if (lookahead.compare("\"token") == 0) { // skip input token r1 = rest.substr(rest.find("]\")", quoteindex+1)+3); } else { // skip rest of leaf, no input token if (isdigit(lookahead[0])) { r1 = rest.substr(rest.find(")", quoteindex+1)+1); } else { // more changes to the format?? cerr << "mal-formed leaf: " << rest << endl; exit(1); } } return r1; } else { // internal node int edge,start, end; double score; string s1; istringstream(rest.substr(1)) >> edge >> s1 >> score >> start >> end; // record lexical type, not lex entry string let = _g.letype(s1); if (!let.empty()) s1 = let; for (unsigned int i=0;i