MAIN.Cpp
资源名称:ictprop.rar [点击查看]
上传用户:tenhai
上传日期:2021-02-19
资源大小:492k
文件大小:7k
源码类别:
组合框控件
开发平台:
Visual C++
- #include "stdafx.h"
- #include <string.h>
- #include "symbol.h"
- #include "grmrgrph.h"
- #include "rrtbl.h"
- #include "error.h"
- #include "grmrpars.h"
- #include "roleinvs.h"
- #include "sntncepars.h"
- #include "sntncelex.h"
- #include <sys/timeb.h>
- #include <time.h>
- #include <conio.h>
- #include "monitor.h"
- #define CMDLEN 80
- #define MAXARGS 4
- #define MAXOPTIONS 10
- #define FOREST "forest.txt"
- #define EDGES "edges.txt"
- char treebuf[20000];
- char tokenbuf[100];
- int brp;
- int T=0;
- CString str;
- char *strippostfix(char *cate){
- int i, catelen;
- catelen=strlen(cate);
- for (i=0; i<catelen; i++){
- if (cate[i]=='_')
- break;
- tokenbuf[i]=cate[i];
- }
- tokenbuf[i]=' ';
- return tokenbuf;
- }
- printvpf(pf_t *ppf){
- unsigned int curpos, sublen;
- // unsigned __int64 curser;
- subpf_t subpf;
- if (ppf->subpfs==NULL){
- strcat(treebuf, strippostfix(code2token(ppf->cate)));
- strcat(treebuf, "(");
- strcat(treebuf, ppf->litoramb.literal);
- strcat(treebuf, ")");
- return 0;
- }
- subpf=ppf->subpfs[0];
- strcat(treebuf, strippostfix(code2token(ppf->cate)));
- strcat(treebuf, " ( ");
- sublen=rightlenof(subpf.rulno);
- for (curpos=0;curpos<sublen;curpos++){
- printvpf(subpf.sublst[curpos]);
- if (curpos<sublen-1)
- strcat(treebuf, " ");
- }
- strcat(treebuf, " ) ");
- return 0;
- }
- getoptions(char **argv, int *parmpos, char *options){
- int nop=0, pos=0;
- char flag;
- if (argv[*parmpos]==NULL || argv[*parmpos][0]!='-')
- return 0;
- while ((flag=argv[*parmpos][++pos])!=' '){
- int scanner=0;
- while (scanner<nop && options[scanner]!=flag)
- scanner++;
- if (scanner==nop){
- nop++;
- if (nop>MAXOPTIONS){
- theMonitor.display("Too Many flags.n");
- return -1;
- }
- }
- options[scanner]=flag;
- }
- (*parmpos)++;
- return nop;
- }
- exeparse(char **argv, int argc){
- char *sfile=NULL, options[MAXOPTIONS];
- int nop=0, soption=0, ioption=0, parmpos=1;
- int i;
- clock_t start, finish;
- clock_t starti, finishi;
- extern FILE *pfedgs;
- double duration;
- extern pf_t *pfroot;
- if ((nop=getoptions(argv, &parmpos, options))<0)
- return 0;
- while (--nop>=0)
- switch (options[nop]){
- case 's':soption=1;break;
- case 'i':ioption=1;break;
- default :str.Format("Unkown flag %c.n", options[nop]); theMonitor.display(str);return 0;
- }
- if (parmpos<argc){
- sfile=argv[parmpos++];
- if (parmpos<argc){
- ntrees=atoi(argv[parmpos]);
- }
- }
- if (!rrready){
- str.Format("No gramar present.n");
- theMonitor.display(str);
- return 0;
- }
- if (sfile!=NULL){
- pfs=fopen(sfile, "r");
- if (pfs==NULL){
- str.Format("Unable to open file %s.n", sfile);
- theMonitor.display(str);
- return 0;
- }
- }
- else
- pfs=stdin;
- pft=fopen(FOREST, "w+t");
- if (pft==NULL){
- str.Format("Unable to open file %s.n", "forest.txt");
- theMonitor.display(str);
- return 0;
- }
- pfedgs=fopen(EDGES, "w+t");
- symbmode=soption;
- interactive=ioption;
- rrset=(rrset_struct *)calloc(arccount+2, sizeof (rrset_struct));
- for (i=0;i<=(arccount+1);i++){
- rrset[i].prr=NULL;
- rrset[i].next=-1;
- }
- catetbl=(pf_t **)calloc(MAXCNODE*tntcount, sizeof(pf_t *));
- lstack=(unsigned int*)calloc(tntcount, sizeof(unsigned int));
- addchartnode();
- // mark=(char *)malloc(ntcount);
- mymark=(char *)malloc(tntcount);
- edgepool=(edge_t *)calloc(MAXENODE, sizeof(edge_t));
- trnodepool=(pf_t *)calloc(MAXTNODE, sizeof(pf_t));
- // compedgepool=(edge_t *)calloc(MAXSTACK, sizeof(edge_t));
- T=0;
- start = clock();
- while (!feof(pfs)){
- extern int nedge;
- sentbuf[0]=' ';
- fgets(sentbuf, MAXSENTLEN, pfs);
- str.Format("%s", sentbuf);
- theMonitor.list(str);
- starti = clock(); //u
- parses();
- T++;
- if (pfroot!=NULL){
- /*
- ambcnt=countbranch(pfroot);
- fprintf(pft, "%I64d Trees:n", ambcnt);
- for (i=1;(i<=ambcnt)&&(i<=ntrees);i++){
- treebuf[0]=' ';
- printpf(pfroot, i-1);
- fprintf(pft, "%sn", treebuf);
- }
- */
- treebuf[0]=' ';
- printvpf(pfroot);
- fprintf(pft, "%sn", treebuf);
- str.Format("%s", treebuf);
- theMonitor.show(T, str);
- //fprintf(pft, "Probability: %.15fn", pfroot->prb);
- str.Format("%d:successn",T);
- theMonitor.display(str);
- //_cprintf("%d", T);
- //clear, take 4 as the maximum length
- //_putch(8); _putch(8); _putch(8); _putch(8);
- }
- else{
- fprintf(pft, "NAn");
- str.Format("%d:failn", T);
- theMonitor.display(str);
- }
- releasewholepf();
- finishi = clock();//n
- //fprintf(pft, "parsed in %2.6f secondsnn", (double)(finishi - starti) / CLOCKS_PER_SEC);
- }
- finish = clock();
- duration = (double)(finish - start) / CLOCKS_PER_SEC;
- str.Format( "%d sentences parsed in %2.6f secondsn", T, duration );
- theMonitor.display(str);
- free(rrset);
- free(catetbl);
- // free(mark);
- free(mymark);
- free(edgepool);
- free(trnodepool);
- // free(compedgepool);
- releasechart();
- if (pfs!=stdin)
- fclose(pfs);
- fclose(pft);
- fclose(pfedgs);
- return 0;
- }
- exebuild(char **argv, int argc){
- char *gfile=NULL, options[MAXOPTIONS];
- int nop=0, voption=0, parmpos=1;
- if ((nop=getoptions(argv, &parmpos, options))<0)
- return 0;
- if (nop)
- switch (options[0]){
- case 'v': voption=1;break;
- default : str.Format("Unkown flag %c.n", options[0]);return 0;
- }
- if (parmpos<argc){
- gfile=argv[parmpos++];
- if (parmpos<argc){
- str.Format("Meaningless argument(s) followed.n");
- theMonitor.display(str);
- return 0;
- }
- }
- if (gfile!=NULL){
- pfg=fopen(gfile, "r");
- if (pfg==NULL){
- str.Format("Unable to open file %s.n", gfile);
- theMonitor.display(str);
- return 0;
- }
- }
- else
- pfg=stdin;
- pfd=pfg;/*fopen("dict.txt", "r");*/
- /*
- if (pfd==NULL){
- printf("Unable to open file %s.n", "dict.txt");
- return 0;
- }*/
- release();
- verbose=voption;
- ECHK(parseg();, {if (pfg!=stdin) fclose(pfg);errecovery();return 0;})
- ECHK(installdict();fclose(pfg);, {errecovery();return 0;})
- ECHK(roleinvr();genexpec();, {errecovery();return 0;})
- genpostfix();
- if (verbose)
- listrr();
- str.Format("%s is %s", gfile,"built successfully.n");
- theMonitor.display(str);
- return 0;
- }
- execmd(char *cmdline){
- char *argv[MAXARGS], *args;
- int i, argc=0, retval=0;
- for (i=0;i<MAXARGS;i++)
- argv[i]=NULL;
- if ((args=strtok(cmdline, " "))==NULL)
- return retval;
- do{
- argv[argc]=(char *)malloc(sizeof(char)*(strlen(args)+1));
- strcpy(argv[argc], args);
- argc++;
- }while (argc<MAXARGS && (args=strtok(NULL, " "))!=NULL);
- if (argc==MAXARGS){
- str.Format("Too Many Arguments.n");
- theMonitor.display(str);
- }
- else{
- if (strcmp(argv[0], "parse")==0)
- retval=exeparse(argv, argc);
- else
- if (strcmp(argv[0], "build")==0)
- retval=exebuild(argv, argc);
- else
- if (strcmp(argv[0], "quit")==0)
- retval=-1;
- else{
- str.Format("Unknown command.n");
- theMonitor.display(str);
- }
- }
- for (i=0;i<argc;i++)
- free(argv[i]);
- return retval;
- }