Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] sscanf help
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: mohammad siddiqui <writetosafi@h...>
Subject: [Caml-list] sscanf help
Hello,
I am having hardtime dealing with sscanf and fscanf fucntion. I am actually 
converting code from C to OCAML. The functions uses multiple sscanf 
fucntions. I have no clue how to deal with this. Can anyone please help me 
out?

Attached is the function in C. The application is used for text 
classification.
i

nt parse_document(char *line, DOC *doc, double *label,
		   long int *numwords, long int max_words_doc)
{
  register long wpos,pos;
  long wnum;
  double weight;
  int numread;
  char featurepair[1000],junk[1000];

  doc->queryid=0;
  doc->costfactor=1;

  pos=0;
  while(line[pos]) {      /* cut off comments */
    if(line[pos] == '#') {
      line[pos]=0;
    }
    else {
      pos++;
    }
  }
  wpos=0;
  if(sscanf(line,"%lf",label) == EOF) return(0);
  pos=0;
  while(isspace((int)line[pos])) pos++;
  while((!isspace((int)line[pos])) && line[pos]) pos++;
  while(((numread=sscanf(line+pos,"%s",featurepair)) != EOF) &&
	(wpos<max_words_doc)) {
    /* printf("%s\n",featurepair); */
    while(isspace((int)line[pos])) pos++;
    while((!isspace((int)line[pos])) && line[pos]) pos++;
    if(sscanf(featurepair,"qid:%ld%s",&wnum,junk)==1) {
      /* it is the query id */
      doc->queryid=(long)wnum;
    }
    else if(sscanf(featurepair,"cost:%lf%s",&weight,junk)==1) {
      /* it is the example-dependent cost factor */
      doc->costfactor=(double)weight;
    }
    else if(sscanf(featurepair,"%ld:%lf%s",&wnum,&weight,junk)==2) {
      /* it is a regular feature */
      if(wnum<=0) {
	perror ("Feature numbers must be larger or equal to 1!!!\n");
	printf("LINE: %s\n",line);
	exit (1);
      }
      if((wpos>0) && ((doc->words[wpos-1]).wnum >= wnum)) {
	perror ("Features must be in increasing order!!!\n");
	printf("LINE: %s\n",line);
	exit (1);
      }
      (doc->words[wpos]).wnum=wnum;
      (doc->words[wpos]).weight=(FVAL)weight;
      wpos++;
    }
    else {
      perror ("Cannot parse feature/value pair!!!\n");
      printf("'%s' in LINE: %s\n",featurepair,line);
      exit (1);
    }
  }
  (doc->words[wpos]).wnum=0;
  (*numwords)=wpos+1;
  doc->docnum=-1;
  doc->twonorm_sq=sprod_ss(doc->words,doc->words);
  return(1);
}



thanks,
Mohammad S Siddiqui.

_________________________________________________________________
MSN Toolbar provides one-click access to Hotmail from any Web page – FREE 
download! http://toolbar.msn.com/go/onm00200413ave/direct/01/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners