| 1 | |
|---|
| 2 | /** |
|---|
| 3 | * Package: UAM Text Tools |
|---|
| 4 | * Component: dgp (dg parser) |
|---|
| 5 | * Version: 1.0 |
|---|
| 6 | * Author: Tomasz Obrebski |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #include <ctime> |
|---|
| 10 | #include <sys/time.h> |
|---|
| 11 | |
|---|
| 12 | #include "global.hh" |
|---|
| 13 | #include "mgraph.hh" |
|---|
| 14 | #include "sgraph.hh" |
|---|
| 15 | #include "grammar.hh" |
|---|
| 16 | #include "dgp1.hh" |
|---|
| 17 | #include "../common/common.h" |
|---|
| 18 | #include "cmdline.h" |
|---|
| 19 | |
|---|
| 20 | #define MAXSEGMENTS 500 |
|---|
| 21 | |
|---|
| 22 | #define MICROSECONDSELAPSED(A,B) ((B.tv_sec - A.tv_sec)*1000000 + (B.tv_usec - A.tv_usec)) |
|---|
| 23 | |
|---|
| 24 | char segment[MAXSEGMENTS][MAXLINE]; |
|---|
| 25 | int segcount=0; |
|---|
| 26 | char seg_mnode[MAXSEGMENTS]; |
|---|
| 27 | char grammarfile[255]; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | Grammar grammar; |
|---|
| 31 | MGraph mgraph; |
|---|
| 32 | SGraph sgraph(mgraph); |
|---|
| 33 | |
|---|
| 34 | FILE* grammarf; |
|---|
| 35 | FILE* debugf=stdout; |
|---|
| 36 | unsigned int info=0U; |
|---|
| 37 | |
|---|
| 38 | bool printtimeinfo=false; |
|---|
| 39 | |
|---|
| 40 | void output(); |
|---|
| 41 | |
|---|
| 42 | main(int argc, char* argv[]) |
|---|
| 43 | { |
|---|
| 44 | gengetopt_args_info args; |
|---|
| 45 | |
|---|
| 46 | if(cmdline_parser(argc,argv,&args) != 0) |
|---|
| 47 | exit(1); |
|---|
| 48 | |
|---|
| 49 | process_config_files(&args,argv[0]); |
|---|
| 50 | process_common_options(&args,argv[0]); |
|---|
| 51 | |
|---|
| 52 | if(!args.grammar_given) |
|---|
| 53 | fprintf(stderr,"dgp: no grammar given\n"); |
|---|
| 54 | |
|---|
| 55 | expand_path(args.grammar_arg,grammarfile); |
|---|
| 56 | |
|---|
| 57 | if(!(grammarf=fopen(grammarfile,"r"))) |
|---|
| 58 | fprintf(stderr,"dgp: grammar file not found: %s.\n", grammarfile), exit(1); |
|---|
| 59 | |
|---|
| 60 | if(args.debug_given) debug=true; |
|---|
| 61 | |
|---|
| 62 | if(args.time_given) printtimeinfo=true; |
|---|
| 63 | |
|---|
| 64 | for(char* c=args.info_arg; *c!='\0' ; ++c) |
|---|
| 65 | switch(*c) |
|---|
| 66 | { |
|---|
| 67 | case 'h': info|=SGraph::HEADS; break; |
|---|
| 68 | case 'd': info|=SGraph::DEPS; break; |
|---|
| 69 | case 's': info|=SGraph::SETS; break; |
|---|
| 70 | case 'c': info|=SGraph::CONSTRAINTS; break; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | grammar.read(grammarf); |
|---|
| 74 | fclose(grammarf); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | // grammar.write(cout); |
|---|
| 79 | // exit(0); |
|---|
| 80 | |
|---|
| 81 | struct timeval starttime,afterinput,afterparse,endtime; |
|---|
| 82 | |
|---|
| 83 | mgraph.clear(); |
|---|
| 84 | sgraph.clear(); |
|---|
| 85 | |
|---|
| 86 | char line[1000]; |
|---|
| 87 | while (fgets(line, MAXLINE+1, inputf)) |
|---|
| 88 | { |
|---|
| 89 | gettimeofday(&starttime,NULL); |
|---|
| 90 | line[strlen(line)-1] = '\0'; |
|---|
| 91 | strcpy(segment[segcount],line); |
|---|
| 92 | |
|---|
| 93 | char segtype[80]; |
|---|
| 94 | |
|---|
| 95 | seg_mnode[segcount] = process_seg(line, args) ? mgraph.add_node(line) : -1; |
|---|
| 96 | |
|---|
| 97 | segcount++; |
|---|
| 98 | |
|---|
| 99 | getfield(line,"3",segtype); |
|---|
| 100 | if(strcmp(segtype,"EOS")==0) |
|---|
| 101 | { |
|---|
| 102 | gettimeofday(&afterinput,NULL); |
|---|
| 103 | dgp1(); // parametry!!! MGraph, SGraph, Grammar |
|---|
| 104 | gettimeofday(&afterparse,NULL); |
|---|
| 105 | output(); |
|---|
| 106 | gettimeofday(&endtime,NULL); |
|---|
| 107 | |
|---|
| 108 | if(printtimeinfo) |
|---|
| 109 | { |
|---|
| 110 | fprintf(stderr,"### INPUT TIME: %10.2fms\n", (float)MICROSECONDSELAPSED(starttime,afterinput)/1000 ); |
|---|
| 111 | fprintf(stderr,"### PARSE TIME: %10.2fms\n", (float)MICROSECONDSELAPSED(afterinput,afterparse)/1000 ); |
|---|
| 112 | fprintf(stderr,"### OUTPUT TIME: %10.2fms\n", (float)MICROSECONDSELAPSED(afterparse,endtime)/1000 ); |
|---|
| 113 | fprintf(stderr,"### TOTAL TIME: %10.2fms\n", (float)MICROSECONDSELAPSED(starttime,endtime)/1000 ); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | mgraph.clear(); |
|---|
| 117 | sgraph.clear(); |
|---|
| 118 | segcount=0; |
|---|
| 119 | } |
|---|
| 120 | // if(args.interactive_flag) { fflush(outputf); fflush(failedf); } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | fclose(inputf); |
|---|
| 124 | fclose(outputf); |
|---|
| 125 | cmdline_parser_free(&args); |
|---|
| 126 | exit(0); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void output() |
|---|
| 130 | { |
|---|
| 131 | for(int si=0; si<segcount; ++si) |
|---|
| 132 | { |
|---|
| 133 | if(seg_mnode[si]>=0) |
|---|
| 134 | { |
|---|
| 135 | MNode& m=mgraph[seg_mnode[si]]; |
|---|
| 136 | for(vector<int>::iterator s=m.snodes.begin(); s!=m.snodes.end(); ++s) |
|---|
| 137 | { |
|---|
| 138 | fputs(segment[si],outputf); |
|---|
| 139 | sgraph.print_node(outputf, *s, info); |
|---|
| 140 | fputc('\n',outputf); |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | else |
|---|
| 144 | { |
|---|
| 145 | fputs(segment[si],outputf); |
|---|
| 146 | fputc('\n',outputf); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | } |
|---|