rendered paste body/** This file contains an example use of the Minipar API. The program reads a sentence from the standard input one line at a time (upto 1024 characters), treating each line as a sentence. The outputs of the program are the parse trees of the input sentences. Using the command line options one output the parse trees in constituency or dependency format and decide which features to include in the output. The printing routines in this file should provide some idea how to access the information contained in a ParseTree object. */#include <cstring>#include <cstdlib>#include <iostream>using namespace std;//#include "quotewhite.h"#define quote_white(x) x#include "ptree.h"char *empty_cat_str = "()";const int MAX_LINE_LEN=1024;int PRINT_ROOT = 0;int PRINT_RELATION = 0;int PRINT_FEATURE = 0;int PRINT_TRIPLES = 0;int INTERACTIVE = 1;int HAS_COMMANDS = 0;void print_triples(const ParseNode* node){ ParseNode* parent = ((ParseNode*) node)->parent(); if (node->relation() && parent && parent->category()) { if (parent->root() && parent->category() && node->category() && node->root() && node->relation()) cout << parent->root() << "\t" << parent->category() << ':' << node->relation() << ':' << node->category() << "\t" << node->root() << endl; } forall (ParseNode*, sub, TRSTree, node) { print_triples(sub); }}void print_parse_tree(const ParseNode* n, int level){ ParseNode* node = dynamic_cast(ParseNode*, n); if (node==0) return; cout << endl; // print two spaces for each level of indentation if (node->label()) cout << node->label() << '\t'; else cout << node->high() << '\t'; for (int i = 0; i<level; i++) cout << " "; cout << "("; if (node->category()) cout << node->category(); if (node->word()) { if (strpbrk(node->word(), " \t\n()")) cout << " \"" << node->word() << "\""; else if (strcmp(node->word(), "\"")==0) cout << " \"\\\"\""; else cout << " " << node->word(); } if (PRINT_ROOT || PRINT_RELATION || PRINT_FEATURE) { if (PRINT_ROOT && node->root()) cout << " (root " << node->root() << ")"; if (PRINT_RELATION && node->relation()) cout << " (relation " << node->relation() << ")"; if (PRINT_FEATURE) { cout << " (atts"; // print the set of features forall (FeatureValue, fv, TArray, node->features()) { cout << " (" << fv._feature << ' ' << fv._value << ")"; } cout << ")"; } } // print the subtrees forall (ParseNode*, sub, TRSTree, node) { print_parse_tree(sub, level+1); } cout << ")";}void print_dependency_node(const ParseNode* n){ ParseNode* node = dynamic_cast(ParseNode*, n); if (node==node->tree()->root()) return; cout << node->label() << "\t("; if (node->word() && *node->word()) cout << quote_white(node->word()); else cout << empty_cat_str; /* was << "()" */ cout << '\t'; if (node->root()) { if (node->word() && strcasecmp(node->root(), node->word())==0) cout << '~'; else cout << quote_white(node->root()); } cout << ' '; if (node->category()) cout << node->category(); else cout << "U"; cout << '\t'; if (node->parent() && node->parent()!=node->tree()->root()) cout << node->parent()->label(); else cout << "*"; cout << '\t'; if (node->relation()) cout << node->relation(); if (node->parent() && node->parent()->root()) cout << "\t(gov " << quote_white(node->parent()->root()) << ")"; if (node->antecedent()) cout << "\t(antecedent " << node->antecedent()->label() << ")"; if (!node->features().empty()) { cout << "\t(atts"; // print the set of attributes forall (FeatureValue, fv, TArray, node->features()) { cout << " (" << fv._feature << ' ' << fv._value << ")"; } cout << ")"; } cout << ")" << endl;}void print_dependency_tree(const ParseNode* n){ int print_self = 0; if (n==0) return; ParseNode* node = dynamic_cast(ParseNode*, n); if (node->parent()==0) cout << "(" << endl; int pos = node->head_pos(); forall (ParseNode*, sub, TRSTree, node) { int sub_pos = sub->head_pos(); if (print_self==0 && sub_pos>=pos) { print_dependency_node(node); print_self = 1; } print_dependency_tree(sub); } if (!print_self) print_dependency_node(node); if (node->parent()==0) cout << ")";}int main(int argc, char* argv[]){ char line[MAX_LINE_LEN+2]; char* features=0; const char* paths = getenv("MINIPATH"); // ParseType type = DEPENDENCY; for (int i = 1; i<argc; i++) {// if (strcmp(argv[i], "-c")==0)// type = CONSTITUENCY; if (strcmp(argv[i], "-i")==0) INTERACTIVE = 0; else if (strcmp(argv[i], "-r")==0) PRINT_ROOT = 1; else if (strcmp(argv[i], "-d")==0) { HAS_COMMANDS = 1; } else if (strcmp(argv[i], "-t")==0) { PRINT_TRIPLES = 1; // type = DEPENDENCY; } else if (strcmp(argv[i], "-f")==0 && i<argc) { PRINT_FEATURE = 1; features = argv[++i]; } else if (strcmp(argv[i], "-l")==0) PRINT_RELATION = 1; else if (strcmp(argv[i], "-p")==0 && i<argc) paths = argv[++i]; else if (strcmp(argv[i], "-e")==0 && i<argc) empty_cat_str = argv[++i]; else if (strcmp(argv[i], "-h")==0 || strcmp(argv[i], "-help")==0) { cout << "pdemo -d -i -f FEATURES -l -p PATHS -h -help" << endl; cout << " -c\tprint constituency trees instead of dependency trees" << endl; cout << " -i\tdo not print the prompt '> '" << endl; cout << " -f FEATURES\n\tinclude the features in the output" << endl; cout << " -l\tprint the grammatical relations in the output" << endl; cout << " -p PATHS\n\tdefine the search path" << endl; cout << " -h or -help\n\tprint this help message" << endl; cout << " -d lines beginning with ~ are treated as commands." << endl; cout << " -e TRACE\n\tuse TRACE to designate empty categories; this defaults to ()" << endl; } } if (paths==0) { cerr << "The search path for data files are not defined.\n "; cerr << "You must use '-p PATHS' option or define the environment\n" << "variable MINIPATH" << endl; exit(1); } initialize_minipar(paths); if (features) extract_features(features); ParseTree parsetree; if (INTERACTIVE) cout << "> " << flush; while (cin.getline(line, MAX_LINE_LEN)) { if (HAS_COMMANDS && line[0]=='~') { interpret_command_line(line+1); } else { parsetree.reset(); parse(line, parsetree); if (PRINT_TRIPLES) print_triples(parsetree.root()); else print_dependency_tree(parsetree.root()); cout << endl; } if (INTERACTIVE) cout << "> " << flush; }}