caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] sscanf help
@ 2004-04-18  3:59 mohammad siddiqui
  2004-04-20  9:56 ` Pierre Weis
  0 siblings, 1 reply; 2+ messages in thread
From: mohammad siddiqui @ 2004-04-18  3:59 UTC (permalink / raw)
  To: skaller; +Cc: rich, caml-list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] sscanf help
  2004-04-18  3:59 [Caml-list] sscanf help mohammad siddiqui
@ 2004-04-20  9:56 ` Pierre Weis
  0 siblings, 0 replies; 2+ messages in thread
From: Pierre Weis @ 2004-04-20  9:56 UTC (permalink / raw)
  To: mohammad siddiqui; +Cc: skaller, rich, caml-list

> 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?

[...]

The Caml's sscanf and fscanf functions are fully documented and are
easier to use and much more powerful than their C counterparts; so,
unless the documentation is unclear (and then please report it to me),
I think your problem is more related to C programming than to some
O'Caml specific scanf difficulties.

In effect, the C function you gave and have to translate to Caml looks
extremely stateful and imperative style in spirit, while our
programming language and its scanf facility are functional (or
stateless). So, even if it is possible to mimic the C program in Caml,
the resulting program is likely to be inelegant and contrived by an
unatural one-to-one translation.

In conclusion, you had better to understand the specification of the C
parsing function and then reimplement it into a (simpler) functional
Caml program.

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-04-20  9:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-18  3:59 [Caml-list] sscanf help mohammad siddiqui
2004-04-20  9:56 ` Pierre Weis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).