/* * $Id: readptb.cpp,v 1.9 2004/01/21 13:43:57 tsuruoka Exp $ */ #include #include #include #include #include #include #include #include #include #include #include #include "common.h" #include "atmstr.h" void print_sentence(const Sentence & s) { const list & lt = s.lt; for (list::const_iterator i = lt.begin(); i != lt.end(); i++) { cout << i->str.str() << "/" << i->pos.str() << " "; } cout << endl; } static string discard_suffix(const string & s) { for (int i = 1; i < s.size(); i++) { if (s[i] == '-') return s.substr(0, i); if (s[i] == '=') return s.substr(0, i); } return s; } static AString parse_ptb(FILE *fp, int depth, list & lt, list & lr) { int c; string nonterminal; // while (!isspace(c = getchar())) { while (!isspace(c = fgetc(fp))) { if (c == '(') { ungetc(c, fp); break; } nonterminal += char(c); } nonterminal = discard_suffix(nonterminal); if (depth == 0) nonterminal = "TOP"; list daughters; for (;;) { // while (isspace(c = getchar())) {} while (isspace(c = fgetc(fp))) {} switch (c) { case '(': { int d = parse_ptb(fp, depth + 1, lt, lr); if (d != -1) daughters.push_back(d); break; } case ')': { if (daughters.size() == 0) return -1; Rule r; r.lhs = AString(nonterminal); r.rhs = daughters; // print_rule(r); assert(r.rhs.size() > 0); lr.push_back(r); return AString(nonterminal); break; } default: { string terminal; terminal += char(c); while ( (c = fgetc(fp)) != ')' ) { terminal += char(c); } if (nonterminal == "-NONE") return -1; AString nt(nonterminal); AString t(terminal); Token tkn(nt, t); lt.push_back(tkn); /* Rule r; r.lhs = nt; r.rhs.push_back(t); print_rule(r); */ return nt; } } } } //void read_sentences(const string & filename, list & ls) void read_sentences(list & ls) { /* FILE *fp; if (!(fp = fopen(filename.c_str(), "r"))) { cout << "error: cannot open " << filename << "!" << endl; exit(1); } */ int c; list lr; // dummy for (;;) { while (isspace(c = fgetc(stdin))) {} if (c == EOF) break; list lt; parse_ptb(stdin, 0, lt, lr); // print_sentence(lt); Sentence s; s.lt = lt; ls.push_back(s); } // fclose(fp); } int main(int argc, char** argv) { list ls; // read_sentences("test22.mrg", ls); read_sentences(ls); for (list::const_iterator i = ls.begin(); i != ls.end(); i++) { // for (list::const_iterator j = i->lt.begin(); j != i->lt.end(); j++) { // cout << j->str.str() << "\t" << j->pos.str() << endl; // } // cout << endl; print_sentence(*i); } }