Esempio n. 1
0
int kopt_rpc_server_init(unsigned short port, int argc, char *argv[])
{
	int i;

	if (port == 0)
		port = 9000;

	i = karg_find(argc, argv, "--or-port", 1);
	if (i > 0 && (i + 1) < argc) {
		int tmp;
		if (!kstr_toint(argv[i + 1], &tmp))
			port = tmp;
	}

	klog("port: %d\n", port);

	ignore_pipe();
	spl_thread_create(worker_thread_or_server, (void*)(long)port, 0);
	return 0;
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
    FILE *bron;
    float distweight[1024];
    float total,max,threshold;
    int  i,j,nrddt,mid,sock,connected,nrdist,maxnr;
    char **dtwords;
    char **dwords;
    char thisdtword[1024];
    char thisdword[1024];
    char locaps[1024];
    char target[1024];
    char stem[1024];
    char classifyline[32768];
    char line[32768];
    char buff[32768];
    char feats[NRFEAT][1024];
    char *part;
    char category[1024];
    char ddtmatch;

    sscanf(argv[1],"%f",&threshold);
    if ((threshold<0.5)||
            (threshold>1.0))
    {
        fprintf(stderr,"[t-dt_checker] ERROR: threshold value not between 0.5 and 1.0\n");
        exit(1);
    }

    bron=fopen(argv[2],"r");
    nrddt=0;
    fgets(line,32768,bron);
    while (!feof(bron))
    {
        nrddt++;
        fgets(line,32768,bron);
    }
    fclose(bron);

    dtwords=malloc(nrddt*sizeof(char*));
    dwords=malloc(nrddt*sizeof(char*));

    bron=fopen(argv[2],"r");
    for (i=0; i<nrddt; i++)
    {
        fscanf(bron,"%s %s ",
               thisdtword,thisdword);
        dtwords[i]=malloc((strlen(thisdtword)+1)*sizeof(char));
        strcpy(dtwords[i],thisdtword);
        dwords[i]=malloc((strlen(thisdword)+1)*sizeof(char));
        strcpy(dwords[i],thisdword);
    }
    fclose(bron);

    if (DEBUG)
        fprintf(stderr,"read %d t-dt triggers from %s\n",
                nrddt,argv[2]);

    // process inst file

    // first, start up communications with the Timbl server
    ignore_pipe();
    sock=make_connection(PORT,SOCK_STREAM,MACHINE);
    if (sock==-1)
    {
        fprintf(stderr,"the confusible server is not responding\n");
        exit(1);
    }
    else
        connected=1;

    // cut off the Timbl welcome message
    sock_gets(sock,buff,sizeof(buff)-1);

    // cut off the Timbl base message
    sock_gets(sock,buff,sizeof(buff)-1);

    // tell the Timbl server to use the t-dt base
    sock_puts(sock,"base t-dt\n");

    // cut off the Timbl acknowledgement
    sock_gets(sock,buff,sizeof(buff)-1);

    mid=NRFEAT/2;
    bron=fopen(argv[3],"r");
    fgets(line,32768,bron);
    while (!feof(bron))
    {
        part=strtok(line," \n");
        for (i=0; ((part!=NULL)&&(i<NRFEAT)); i++)
        {
            strcpy(feats[i],part);
            part=strtok(NULL," \n");
        }
        if (!((strcmp(feats[mid],"<begin>")==0)||
                (strcmp(feats[mid],"<end>")==0)))
        {
            strcpy(locaps,"");
            for (i=0; i<strlen(feats[mid]); i++)
            {
                strcat(locaps," ");
                if ((feats[mid][i]>='A')&&
                        (feats[mid][i]<='Z'))
                    locaps[i]=feats[mid][i]+32;
                else
                    locaps[i]=feats[mid][i];
            }

            ddtmatch=0;
            i=0;
            while ((i<nrddt)&&
                    (!ddtmatch))
            {
                if ((strcmp(feats[mid],dtwords[i])==0)||
                        (strcmp(locaps,dtwords[i])==0)||
                        (strcmp(feats[mid],dwords[i])==0)||
                        (strcmp(locaps,dwords[i])==0))
                    ddtmatch=1;
                if (!ddtmatch)
                    i++;
            }

            fprintf(stdout,"%s",
                    feats[mid]);
            if (ddtmatch)
            {
                // call Timbl
                strcpy(classifyline,"c ");
                for (j=0; j<NRFEAT; j++)
                {
                    strcat(classifyline,feats[j]);
                    strcat(classifyline," ");
                }
                strcat(classifyline,"?\n");

                if (DEBUG)
                    fprintf(stderr,"\ncalling Timbl with %s",
                            classifyline);

                sock_puts(sock,classifyline);
                sock_gets(sock,buff,sizeof(buff));

                if (DEBUG)
                    fprintf(stderr,"getting back: %s\n",
                            buff);

                part=strtok(buff," \n");
                part=strtok(NULL," \n");
                strcpy(category,"");
                for (j=1; j<strlen(part)-1; j++)
                {
                    strcat(category," ");
                    category[j-1]=part[j];
                }
                while ((part!=NULL)&&
                        (strcmp(part,"{")!=0))
                    part=strtok(NULL," \n");

                if (part!=NULL)
                {
                    nrdist=0;
                    while ((part!=NULL)&&
                            (strcmp(part,"}")!=0))
                    {
                        part=strtok(NULL," \n");
                        if (strcmp(part,"}")!=0)
                        {
                            part=strtok(NULL," \n");
                            if (part[strlen(part)-1]==',')
                                sscanf(part,"%f,",&distweight[nrdist]);
                            else
                                sscanf(part,"%f",&distweight[nrdist]);
                            nrdist++;
                        }
                    }
                    if (DEBUG)
                    {
                        fprintf(stderr,"distro of %d: ",
                                nrdist);
                        for (i=0; i<nrdist; i++)
                            fprintf(stderr," %9.4f",
                                    distweight[i]);
                    }
                    max=0.0;
                    total=0.0;
                    for (i=0; i<nrdist; i++)
                    {
                        total+=distweight[i];
                        if (distweight[i]>max)
                        {
                            max=distweight[i];
                            maxnr=i;
                        }
                    }

                    if (DEBUG)
                        fprintf(stderr," - max %6.3f certainty\n",
                                (max/total));

                    if ((max/total>=threshold)&&
                            (total>MINOCC))
                    {
                        if (strcmp(category,target)!=0)
                        {
                            fprintf(stdout," %s",
                                    category);
                            fprintf(stderr,"correcting %s to %s\n",
                                    feats[mid],category);
                        }
                    }

                }
            }
            fprintf(stdout,"\n");
        }
        fgets(line,32768,bron);
    }
    fclose(bron);
    close(sock);

    return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
  FILE *bron;
  int  i,j,mid,sock,connected,withletters,nrexceptions=0;
  float letterratio;
  char classifyline[32768];
  char word[1024];
  char word2[1024];
  char locap[1024];
  char partlocap[1024];
  char line[32768];
  char buff[32768];
  char feats[NRFEAT][1024];
  char ***exceptions;
  char *part;
  char inflection,exception,locapmatch;

  // read exceptions

  bron=fopen(argv[1],"r");
  fgets(line,32768,bron);
  while (!feof(bron))
    {
      nrexceptions++;
      fgets(line,32768,bron);
    }
  fclose(bron);
  exceptions=malloc(nrexceptions*sizeof(char**));
  bron=fopen(argv[1],"r");
  for (i=0; i<nrexceptions; i++)
    {
      fscanf(bron,"%s %s ",
	     word,word2);
      exceptions[i]=malloc(2*sizeof(char*));
      exceptions[i][0]=malloc((strlen(word)+1)*sizeof(char));
      exceptions[i][1]=malloc((strlen(word2)+1)*sizeof(char));
      strcpy(exceptions[i][0],word);
      strcpy(exceptions[i][1],word2);
    }
  if (DEBUG2)
    fprintf(stderr,"read %d exception pairs from %s\n",
	    nrexceptions,argv[1]);

  // process inst file

  // first, start up communications with the WOPR server
  ignore_pipe();
  sock=make_connection(PORT,SOCK_STREAM,MACHINE);
  if (sock==-1)
    {
      fprintf(stderr,"the WOPR server is not responding\n");
      exit(1);
    }
  else
    connected=1;

  mid=NRFEAT/2;
  bron=fopen(argv[2],"r");
  fgets(line,32768,bron);
  while (!feof(bron))
    {
      part=strtok(line," \n");
      for (i=0; ((part!=NULL)&&(i<NRFEAT)); i++)
	{
	  strcpy(feats[i],part);
	  part=strtok(NULL," \n");
	}

      strcpy(word,feats[mid]);

      if (!((strcmp(word,"<begin>")==0)||
	    (strcmp(word,"<end>")==0)))
	{

	  fprintf(stdout,"%s",
		  word);
	  
	  strcpy(locap,"");
	  withletters=0;
	  for (i=0; i<strlen(word); i++)
	    {
	      strcat(locap," ");
	      if ((word[i]>='A')&&(word[i]<='Z'))
		locap[i]=word[i]+32;
	      else
		locap[i]=word[i];
	      if ((locap[i]>='a')&&(locap[i]<='z'))
		withletters++;
	    }

	  letterratio=(1.*withletters)/(1.*strlen(word));


	  if (letterratio>0.5)
	    {

	      /*
	      ignore_pipe();
	      sock=make_connection(PORT,SOCK_STREAM,MACHINE);
	      if (sock==-1)
		{
		  fprintf(stderr,"the WOPR server is not responding\n");
		  exit(1);
		}
	      else
		connected=1;
	      */

	      // call WOPR
	      strcpy(classifyline,"");
	      for (j=0; j<NRFEAT; j++)
		{
		  if (j!=mid)
		    {
		      strcat(classifyline,feats[j]);
		      strcat(classifyline," ");
		    }
		}
	      strcat(classifyline,word);
	      strcat(classifyline,"\n");
	      
	      if (DEBUG2)
		fprintf(stderr,"calling WOPR with %s",
			classifyline);
	      
	      sock_puts(sock,classifyline);
	      sock_gets(sock,buff,sizeof(buff));
	      
	      if (DEBUG2)
		fprintf(stderr,"getting back: %s\n",
			buff);
	      
	      part=strtok(buff,"\t\n");
	      
	      while ((part!=NULL)&&
		     (strcmp(part,"__EMPTY__")!=0))
		{
		  // special WOPR check
		  exception=0;
		  for (i=0; ((i<nrexceptions)&&(!exception)); i++)
		    {
		      if (((strcmp(word,exceptions[i][0])==0)&&
			   (strcmp(part,exceptions[i][1])==0))||
			  ((strcmp(word,exceptions[i][1])==0)&&
			   (strcmp(part,exceptions[i][0])==0)))
			{
			  exception=1;
			  if (DEBUG)
			    fprintf(stderr,"WOPR caught an exception (%s %s) and will remain silent\n",
				word,part);
			}
		    }
		 
		  // check: locap match?
		  locapmatch=0;

		  strcpy(partlocap,"");
		  for (i=0; i<strlen(part); i++)
		    {
		      strcat(partlocap," ");
		      if ((part[i]>='A')&&(part[i]<='Z'))
			partlocap[i]=part[i]+32;
		      else
			partlocap[i]=part[i];
		    }

		  if (strcmp(partlocap,locap)==0)
		    locapmatch=1;

		  // check: plural?
		  inflection=0;
		  if (!exception)
		    {
		      if ((((part[strlen(part)-1]=='s')&&
			    (word[strlen(word)-1]!='s')))||
			  (((part[strlen(part)-1]!='s')&&
			    (word[strlen(word)-1]=='s'))))
			inflection=1;
		      if ((((part[strlen(part)-1]=='e')&&
			    (word[strlen(word)-1]!='e')))||
			  (((part[strlen(part)-1]!='e')&&
			    (word[strlen(word)-1]=='e'))))
			inflection=1;
		      if ((((part[strlen(part)-1]=='n')&&
			    (word[strlen(word)-1]=='t')))||
			  (((part[strlen(part)-1]=='t')&&
			    (word[strlen(word)-1]=='n'))))
			inflection=1;
		    }
		  if ((!inflection)&&
		      (!exception)&&
		      (!locapmatch)&&
		      (strlen(part)>MINLEN))
		    {
		      fprintf(stdout," %s",
			      part);
		      fprintf(stderr,"WOPR corrects [%s] into [%s]\n",
			      word,part);
		    }
		  part=strtok(NULL,"\t\n");
		}

	      //close(sock);

	    } 
	  fprintf(stdout,"\n");
	}
      fgets(line,32768,bron);
    }
  fclose(bron);
  close(sock);

  return 0;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
  FILE *bron;
  float distweight[1024];
  float total,max,threshold;
  int  i,j,sock,connected,nrdist,counter,defcon,maxnr,result;
  char classifyline[32768];
  char readbuffer[1024];
  char locap[1024];
  char buffer[NRFEAT][1024];
  char membuffer[NRFEAT+3][1024];
  char buff[32768];
  char timblbuffer[1024];
  char punc[NRFEAT][32];
  char cap[1024];
  char puncstring[1024];
  char *part;
  char category[1024];
  char realout[1024];
  char capped,ready;

  ready=0;
  defcon=maxnr=connected=result=0;

  sscanf(argv[1],"%f",&threshold);
  if ((threshold<0.5)||
      (threshold>1.0))
    {
      fprintf(stderr,"[punc-recase_checker] ERROR: threshold value not between 0.5 and 1.0\n");
      exit(1);
    }

  strcpy(puncstring,",.`'\"’‘!?:;()");

  // process text file (not prefab inst!)

  // first, start up communications with the Timbl server
  ignore_pipe();
  sock=make_connection(PORT,SOCK_STREAM,MACHINE);
  if (sock==-1)
    {
      fprintf(stderr,"the punc-recase server is not responding\n");
      exit(1);
    }
  else
    connected=1;

  // cut off the Timbl welcome message
  sock_gets(sock,buff,sizeof(buff)-1); 

  // cut off the Timbl base message
  sock_gets(sock,buff,sizeof(buff)-1); 

  // tell the Timbl server to use the punc-recase base
  sprintf(timblbuffer,"base punc-recase\n");
  sock_puts(sock,timblbuffer);

  // cut off the Timbl acknowledgement
  sock_gets(sock,buff,sizeof(buff)-1); 

  // initialize buffers
  strcpy(cap,"");
  for (i=0; i<NRFEAT; i++)
    {
      strcpy(membuffer[i],"_");
      strcpy(buffer[i],"_");
      strcpy(punc[i],"NP");
      strcat(cap," ");
      cap[i]='-';
    }

  bron=fopen(argv[2],"r");
  counter=0;
  while (!ready)
    {
      if (!feof(bron))
	{
	  result=fscanf(bron,"%s ",
			readbuffer);
	}
      else
	strcpy(readbuffer,"_");

      /*
      if ((counter>(NRFEAT/2)-1)&&
	  (defcon<=NRFEAT/2))
	{
	  
	  fprintf(stdout,"%s",
		  membuffer[(NRFEAT/2)-1]);
	  if (DEBUG)
	    fprintf(stderr,"original token: [%s]\n",
		    membuffer[(NRFEAT/2)-1]);
	}
      */

      for (i=NRFEAT; i>0; i--)
	strcpy(membuffer[i],membuffer[i-1]);
      strcpy(membuffer[0],readbuffer);

      if (strstr(puncstring,readbuffer))
	{
	  strcpy(punc[NRFEAT-1],readbuffer);
	}
      else
	{
	  capped=0;
	  if ((readbuffer[0]>='A')&&
	      (readbuffer[0]<='Z'))
	    capped=1;
	  strcpy(locap,"");
	  for (i=0; i<strlen(readbuffer); i++)
	    {
	      strcat(locap," ");
	      if ((readbuffer[i]>='A')&&
		  (readbuffer[i]<='Z'))
		locap[i]=readbuffer[i]+32;
	      else
		locap[i]=readbuffer[i];
	    }
	  
	  for (i=0; i<NRFEAT-1; i++)
	    {
	      strcpy(buffer[i],buffer[i+1]);
	      strcpy(punc[i],punc[i+1]);
	      cap[i]=cap[i+1];
	    }
	  strcpy(buffer[NRFEAT-1],locap);
	  if (capped)
	    cap[NRFEAT-1]='C';
	  else
	    cap[NRFEAT-1]='-';
	  strcpy(punc[NRFEAT-1],"NP");
		
      
	  if (DEBUG)
	    fprintf(stderr,"checking [%s]-[%c]-[%s]\n",
		    buffer[(NRFEAT/2)],
		    cap[(NRFEAT/2)],
		    punc[(NRFEAT/2)]);
	  
	  if (counter>2)
	    {
	      //if (strcmp(punc[(NRFEAT/2)],"NP")==0)
		{
		  if ((counter>(NRFEAT/2)-1)&&
		      (defcon<=NRFEAT/2))
		    {
		      
		      fprintf(stdout,"%s",
			     buffer[(NRFEAT/2)]);
		      if (DEBUG)
			fprintf(stderr,"original token: [%s]\n",
				buffer[(NRFEAT/2)]);
		    }


		  if (DEBUG)
		    {
		      fprintf(stderr,"\nbuffer:");
		      for (i=0; i<NRFEAT; i++)
			fprintf(stderr," [%10s]",
				buffer[i]);
		      fprintf(stderr,"\n");
		      
		      fprintf(stderr,"cap:   ");
		      for (i=0; i<NRFEAT; i++)
			fprintf(stderr," [%10c]",
				cap[i]);
		      fprintf(stderr,"\n");
		      
		      fprintf(stderr,"punc:  ");
		      for (i=0; i<NRFEAT; i++)
			fprintf(stderr," [%10s]",
				punc[i]);
		      fprintf(stderr,"\n");
		    }
		  
		  
		  strcpy(classifyline,"c ");
		  
		  for (i=0; i<NRFEAT-1; i++)
		    {
		      strcat(classifyline,buffer[i]);
		      strcat(classifyline," ");
		    }
		  
		  if (strcmp(punc[2],"NP")!=0)
		    {
		      if (cap[3]!='-')
			{
			  strcat(classifyline,punc[2]);
			  strcpy(realout,punc[2]);
			  strcat(classifyline,"C\n");
			  strcat(realout,"C");
			}
		      else
			{
			  strcat(classifyline,punc[2]);
			  strcpy(realout,punc[2]);
			  strcat(classifyline,"\n");
			}
		    }
		  else
		    {
		      if (cap[3]!='-')
			{
			  strcat(classifyline,"C\n");
			  strcpy(realout,"C");
			}
		      else
			{
			  strcat(classifyline,"-\n");
			  strcpy(realout,"-");
			}
		    }
		  
		  if (DEBUG)
		    fprintf(stderr,"\ncalling Timbl with %s",
			    classifyline);
		  
		  sock_puts(sock,classifyline);
		  sock_gets(sock,buff,sizeof(buff));
		  
		  if (DEBUG)
		    fprintf(stderr,"getting back: %s\n",
			    buff);
		  
		  part=strtok(buff," \n");
		  part=strtok(NULL," \n");
		  strcpy(category,"");
		  for (j=1; j<strlen(part)-1; j++)
		    {
		      strcat(category," ");
		      category[j-1]=part[j];
		    }
		  while ((part!=NULL)&&
			 (strcmp(part,"{")!=0))
		    part=strtok(NULL," \n");
		  
		  if (part!=NULL)
		    {
		      nrdist=0;
		      while ((part!=NULL)&&
			     (strcmp(part,"}")!=0))
			{
			  part=strtok(NULL," \n");
			  if (strcmp(part,"}")!=0)
			    {
			      part=strtok(NULL," \n");
			      if (part[strlen(part)-1]==',')
				sscanf(part,"%f,",&distweight[nrdist]);
			      else
				sscanf(part,"%f",&distweight[nrdist]);
			      nrdist++;
			    }
			}
		      if (DEBUG)
			{
			  fprintf(stderr,"distro of %d:",
				  nrdist);
			  for (i=0; i<nrdist; i++)
			    fprintf(stderr," %.0f",
				    distweight[i]);
			}
		      
		      max=0.0;
		      maxnr=0;
		      total=0.0;
		      for (i=0; i<nrdist; i++)
			{
			  total+=distweight[i];
			  if (distweight[i]>max)
			    {
			      max=distweight[i];
			      maxnr=i;
			    }
			}
		      
		      if (DEBUG)
			fprintf(stderr," - max %6.3f certainty\n",
				(max/total));
		      
		      if ((max/total>=threshold)&&
			  // (max/total<1.0)&&
			  (total>MINOCC))
			{
			  if ((strcmp(category,realout)!=0)&&
			      (strcmp(category,"-")!=0))
			    {
			      if (DEBUG)
				{
				  fprintf(stderr,"line: %s",
					  classifyline);
				  fprintf(stderr,"we have to do something: predicted %s is not actual %s\n",
					  category,realout);
				  fprintf(stderr,"correction: [%s]\n",
					  category);
				}
			      fprintf(stdout," %s",
				      category);
			    }
			}
		    }
		}
	      if (strcmp(punc[NRFEAT/2],"NP")!=0)
		{
		  if ((counter>(NRFEAT/2)-1)&&
		      (defcon<=NRFEAT/2))
		    {
		      
		      fprintf(stdout,"\n%s",
			     punc[(NRFEAT/2)]);
		      if (DEBUG)
			fprintf(stderr,"original token: [%s]\n",
				punc[(NRFEAT/2)]);
		    }


		}
	    }
	
	
	  if ((counter>(NRFEAT/2)-1)&&
	      (defcon<=NRFEAT/2))
	    {
	      fprintf(stdout,"\n");
	      if (DEBUG)
		fprintf(stderr,"\n");
	    }
	}

      if (feof(bron))
        defcon++;
      if (defcon>(NRFEAT/2))
        ready=1;

      counter++;
    }
  fclose(bron);
  close(sock);

  return 0;
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
  FILE *bron;
  float distweight[1024];
  char word1[1024];
  char capword1[1024];
  char word2[1024];
  char capword2[1024];
  float total,max,threshold;
  int  i,j,mid,sock,connected,nrdist,maxnr;
  char classifyline[32768];
  char line[32768];
  char buff[32768];
  char buffer[1024];
  char feats[NRFEAT][1024];
  char *part;
  char category[1024];
  char match;

  strcpy(word1,argv[1]);
  strcpy(capword1,word1);
  capword1[0]-=32;

  strcpy(word2,argv[2]);
  strcpy(capword2,word2);
  capword2[0]-=32;

  sscanf(argv[3],"%f",&threshold);
  if ((threshold<0.5)||
      (threshold>1.0))
    {
      fprintf(stderr,"[confusible_checker] ERROR: threshold value not between 0.5 and 1.0\n");
      exit(1);
    }

  // process inst file

  // first, start up communications with the Timbl server
  ignore_pipe();
  sock=make_connection(PORT,SOCK_STREAM,MACHINE);
  if (sock==-1)
    {
      fprintf(stderr,"the confusible server is not responding\n");
      exit(1);
    }
  else
    connected=1;

  // cut off the Timbl welcome message
  sock_gets(sock,buff,sizeof(buff)-1); 

  // cut off the Timbl base message
  sock_gets(sock,buff,sizeof(buff)-1); 

  // tell the Timbl server to use the word1-word2 base
  sprintf(buffer,"base %s-%s\n",
	  word1,word2);
  sock_puts(sock,buffer);

  // cut off the Timbl acknowledgement
  sock_gets(sock,buff,sizeof(buff)-1); 
  
  mid=NRFEAT/2;
  bron=fopen(argv[4],"r");
  fgets(line,32768,bron);
  while (!feof(bron))
    {
      part=strtok(line," \n");
      for (i=0; ((part!=NULL)&&(i<NRFEAT)); i++)
	{
	  strcpy(feats[i],part);
	  part=strtok(NULL," \n");
	}
      if (!((strcmp(feats[mid],"<begin>")==0)||
	    (strcmp(feats[mid],"<end>")==0)))
	{
	  match=0;
	  if ((strcmp(feats[mid],word1)==0)||
	      (strcmp(feats[mid],capword1)==0)||
	      (strcmp(feats[mid],word2)==0)||
	      (strcmp(feats[mid],capword2)==0))
	    match=1;
	  
	  fprintf(stdout,"%s",
		  feats[mid]);
	  
	  if (match)
	    {
	      // call Timbl
	      strcpy(classifyline,"c ");
	      for (j=0; j<NRFEAT; j++)
		{
		  if (j!=mid)
		    strcat(classifyline,feats[j]);
		  strcat(classifyline," ");
		}
	      strcat(classifyline,"?\n");

	      if (DEBUG)
		fprintf(stderr,"\ncalling Timbl with %s",
			classifyline);
	      
	      sock_puts(sock,classifyline);
	      sock_gets(sock,buff,sizeof(buff));
	      
	      if (DEBUG)
		fprintf(stderr,"getting back: %s\n",
			buff);
	      
	      part=strtok(buff," \n");
	      part=strtok(NULL," \n");
	      strcpy(category,"");
	      for (j=1; j<strlen(part)-1; j++)
		{
		  strcat(category," ");
		  category[j-1]=part[j];
		}
	      while ((part!=NULL)&&
		     (strcmp(part,"{")!=0))
		part=strtok(NULL," \n");
	      
	      if (part!=NULL)
		{
		  nrdist=0;
		  while ((part!=NULL)&&
			 (strcmp(part,"}")!=0))
		    {
		      part=strtok(NULL," \n");
		      if (strcmp(part,"}")!=0)
			{
			  part=strtok(NULL," \n");
			  if (part[strlen(part)-1]==',')
			    sscanf(part,"%f,",&distweight[nrdist]);
			  else
			    sscanf(part,"%f",&distweight[nrdist]);
			  nrdist++;
			}
		    }
		  if (DEBUG)
		    {
		      fprintf(stderr,"distro of %d:",
			      nrdist);
		      for (i=0; i<nrdist; i++)
			fprintf(stderr," %.0f",
				distweight[i]);
		    }
		  
		  max=0.0;
		  total=0.0;
		  for (i=0; i<nrdist; i++)
		    {
		      total+=distweight[i];
		      if (distweight[i]>max)
			{
			  max=distweight[i];
			  maxnr=i;
			}
		    }
		  
		  if (DEBUG)
		    fprintf(stderr," - max %6.3f certainty\n",
			    (max/total));
		  
		  if ((max/total>=threshold)&&
		      (max/total<1.0)&&
		      (total>MINOCC))
		    {
		      if (strcmp(category,feats[mid])!=0)
			{
			  fprintf(stdout," %s%6.3f",
				  category,(max/total));
			  fprintf(stderr,"corrected %s into %s\n",
				  feats[mid],category);
			}
		    }
		}
	    }
	  fprintf(stdout,"\n");
	}
      fgets(line,32768,bron);
    }
  fclose(bron);
  close(sock);

  return 0;
}
Esempio n. 6
0
int main(int argc, char **argv) {

  /*Set default values for tty device, server port and background operation*/
  char ttydev[1024];
  strncpy(ttydev, "/dev/ttyS0", 1023);
  char serverport[1024];
  strncpy(serverport, "3376", 1023);
  int auth = FALSE;
  int verbose = FALSE;
  
  /*Find options on the commandline*/
  int c;
  extern char *optarg;
  
  while ((c = getopt(argc, argv, "s:p:dv")) != -1) {
    switch(c) {
      case 's':
        strncpy(ttydev, optarg, 1023);
        break;
      case 'p':
        strncpy(serverport, optarg, 1023);
	break;
      case 'd':
        background = TRUE;
	break;
      case 'a':
        auth = TRUE;
	break;
      case 'v':
        verbose = TRUE;
	break;
      case '?':
        print_usage();
    }
  }
  
  /*open camera interface*/
  VISCA_open_interface(&interface, &camera, ttydev);
  
  /*Set up some signal handling*/
  ignore_pipe();
  struct sigaction sa;  
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = sig_chld;
  sigaction(SIGCHLD, &sa, NULL);
  sa.sa_handler = sig_abort;
  sigaction(SIGINT, &sa, NULL);
  sigaction(SIGQUIT, &sa, NULL);
  sigaction(SIGTERM, &sa, NULL);

  /*initialize network stuff - taken from socket-faq*/
  int port = -1;
  port = atoport(serverport, "tcp");
  if (port == -1) {
    fprintf(stderr,"Unable to find service: %s\n",serverport);
    exit(1);
  }

  /*check for lock file*/
  struct stat tempstat;
  if (stat("/var/lock/visca-daemon.lock", &tempstat) == 0) {
    fprintf(stderr, "Lockfile /var/lock/visca-daemon.lock exists\nMaybe another visca-daemo is running already?\n\n");
    exit(1);      
  }

  /*go to background if the user wants us to*/
  if (background == TRUE) {
    if(daemon(0,0) != 0) {
      fprintf(stderr,"Unable to detach from console\n");
      exit(1);
    }
    openlog("visca-daemon", 0, LOG_DAEMON);
  }

  mknod("/var/lock/visca-daemon.lock", S_IFREG, 0);
  
  if (background == TRUE) {
    syslog(LOG_INFO, "Visca Camera daemon started.\n");
  } else {
    fprintf(stderr, "Visca Camera daemon started.\n");
  } 
  
  /*Wait for a client to make connections*/
  /*forking is handled by sockhelp.c from the socket-faq*/
  int sock = get_connection(SOCK_STREAM, port, &listensock);
  connectsock = sock;
  if (background == TRUE) {
    syslog(LOG_INFO, "New connection established\n");
  } else {
    fprintf(stderr, "New connection established\n");
  } 

  /*Greet the user*/
  char buffer[1024];
  sock_puts(sock,"Welcome to the VISCA camera server\n");
  
  int connected = 1;
  int errorcode;
  int ret1, ret2, ret3;
  while (connected) {
    /* Read input */
    if (sock_gets(sock, buffer, 1024) < 0) {
      connected = 0;
    } else if (buffer[0]=='\0') {
      connected = 0;    
    } else {
      if (verbose == TRUE) {
        if (background == TRUE) {
          syslog(LOG_INFO, "Command received: %s\n", buffer);
	} else {
          fprintf(stderr, "Command received: %s\n", buffer);
	}
      }
      errorcode = VISCA_doCommand(buffer, &ret1, &ret2, &ret3, &interface, &camera);
      switch(errorcode) {
        case 10:
	  snprintf(buffer, 1023, "10 OK - no return value\n");
          break;
        case 11:
	  snprintf(buffer, 1023, "11 OK - one return value\nRET1: %i\n", ret1);
          break;    
        case 12:
	  snprintf(buffer, 1023, "12 OK - two return values\nRET1: %i\nRET2: %i\n", ret1, ret2);
          break;
        case 13:
	  snprintf(buffer, 1023, "13 OK - three return values\nRET1: %i\nRET2: %i\nRET3: %i\n", ret1, ret2, ret3);
          break;
        case 40:
	  snprintf(buffer, 1023, "40 ERROR - command not recognized\n");
          break;
        case 41:
	  snprintf(buffer, 1023, "41 ERROR - argument 1 not recognized\n");
          break;
        case 42:
	  snprintf(buffer, 1023, "42 ERROR - argument 2 not recognized\n");
          break;
        case 43:
	  snprintf(buffer, 1023, "43 ERROR - argument 3 not recognized\n");
          break;
        case 44:
	  snprintf(buffer, 1023, "44 ERROR - argument 4 not recognized\n");
          break;
        case 45:
	  snprintf(buffer, 1023, "45 ERROR - argument 5 not recognized\n");
          break;
        case 46:
	  snprintf(buffer, 1023, "46 ERROR - camera replied with an error\n");
          break;
        case 47:
	  snprintf(buffer, 1023, "47 ERROR - camera replied with an unknown return value\n");
          break;
        default:
	  snprintf(buffer, 1023, "unknown error code: %i\n", errorcode);
      }
      if (verbose == TRUE) {
        if (background == TRUE) {
          syslog(LOG_INFO, "Answer sent: %s", buffer);
	} else {
          fprintf(stderr, "Answer sent: %s", buffer);
	}
      }

      if (sock_puts(sock, buffer) < 0) {
        connected = 0;
      }
    }
  }
  if (background == TRUE) {
    syslog(LOG_INFO, "Connection closed\n");
  } else {
    fprintf(stderr, "Connection closed\n");
  }
  close(sock);
  return 0;
}
Esempio n. 7
0
static void *worker_thread_or_server(void *userdata)
{
	int ready, i, n, bufsize = 128 * 1024;
	void *buf;
	struct epoll_event ev, *e;

	unsigned short port = (unsigned short)(int)(long)userdata;

	int s_listen, new_fd;
	struct sockaddr_in their_addr;
	struct sockaddr_in my_addr;
	socklen_t sin_size;

	ignore_pipe();

	if ((s_listen = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		kerror("c:%s, e:%s\n", "socket", strerror(errno));
		return NULL;
	}

	config_socket(s_listen);

	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(port);
	my_addr.sin_addr.s_addr = INADDR_ANY;
	memset(my_addr.sin_zero, '\0', sizeof(my_addr.sin_zero));
	if (bind(s_listen, (struct sockaddr *) &my_addr, sizeof(my_addr)) == -1) {
		kerror("c:%s, e:%s\n", "bind", strerror(errno));
		return NULL;
	}

	if (listen(s_listen, BACKLOG) == -1) {
		kerror("c:%s, e:%s\n", "listen", strerror(errno));
		return NULL;
	}

	__g_epoll_fd = epoll_create(__g_epoll_max);
	memset(&ev, 0, sizeof(ev));
	ev.data.fd = s_listen;
	ev.events = EPOLLIN;
	epoll_ctl(__g_epoll_fd, EPOLL_CTL_ADD, s_listen, &ev);

	buf = kmem_alloc(bufsize, char);
	for (;;) {
		do
			ready = epoll_wait(__g_epoll_fd, __g_epoll_events, __g_epoll_max, -1);
		while ((ready == -1) && (errno == EINTR));

		for (i = 0; i < ready; i++) {
			e = __g_epoll_events + i;

			if (e->data.fd == s_listen) {
				sin_size = sizeof(their_addr);
				if ((new_fd = accept(s_listen, (struct sockaddr *) &their_addr, &sin_size)) == -1) {
					kerror("c:%s, e:%s\n", "accept", strerror(errno));
					continue;
				}

				/* FIXME: non-blocking will cause orbatch bang */
				/* setnonblocking(new_fd); */

				/* XXX: new_fd can be o or w */
				if (process_connect(new_fd))
					close_connect(new_fd);

				continue;
			} else if (!(e->events & EPOLLIN)) {
				kerror("!!! Not EPOLLIN: event is %08x, fd:%d\n", e->events, e->data.fd);
				continue;
			}

			if ((n = recv(e->data.fd, buf, bufsize, 0)) > 0) {
				if (do_opt_command(e->data.fd, buf, n))
					close_connect(e->data.fd);
			} else {
				klog("Remote close socket: %d\n", e->data.fd);
				close_connect(e->data.fd);
			}
		}
	}
	kmem_free(buf);

	close(__g_epoll_fd);
	__g_epoll_fd = -1;

	return NULL;
}