helloworld.c

Go to the documentation of this file.
00001 /* File: helloworld.c
00002  *
00003  * Time-stamp: <Wed 03 Aug 2005-- 14:15:43 Paolo Frasconi>
00004  * $Id: helloworld.c,v 1.1 2006/05/31 09:21:19 andrea Exp $
00005  *
00006  * Author: Alessio Ceroni and Paolo Frasconi
00007  *
00008  * Description: Very simple data type and associated kernel
00009  *              The purpose is just to demonstrate how to program
00010  *              a plugin
00011  */
00012 # include <stdio.h>
00013 # include <stdlib.h>
00014 # include <ctype.h>
00015 # include "helloworld.h"
00016 
00017 # include "svector.h"
00018 
00019 # define MULTK 0
00020 # define MINK 1
00021 
00022 int min_or_mult=0;
00023 
00024 int space_or_null(int c) {
00025   if (c==0)
00026     return 1;
00027   return isspace(c);
00028 }
00029 
00037 void* plugin_null(void) 
00038 {
00039   DATUM* nulldatum = (DATUM *)malloc(sizeof(DATUM));
00040   nulldatum->x = 0;
00041   return nulldatum;
00042 }
00043 
00044 
00049 void* plugin_copy_constructor(void* _words)
00050 {
00051 
00052   DATUM* words = (DATUM *)malloc(sizeof(DATUM));
00053   words->x = ((DATUM*)_words)->x;
00054   return(words);
00055 }
00056 
00057 
00066 double plugin_kernel(void *_a, void *_b) 
00067 {
00068   SVECTOR* va = (SVECTOR*) _a; 
00069   SVECTOR* vb = (SVECTOR*) _b; 
00070   double a = ((DATUM*)va->words)->x;
00071   double b = ((DATUM*)vb->words)->x;
00072   /* printf("Calculating kernel between %f and %f\n",a, b); */
00073   if (min_or_mult==MULTK)
00074     return a * b;
00075   else
00076     return a<b? a:b;
00077 }
00078 
00079 
00080 
00090 void* 
00091 plugin_parse_document(char *line, double *label,
00092                       long *queryid, long *slackid, double *costfactor,
00093                       long int *highest_feature, 
00094                       long int max_words_doc,
00095                       char **comment)
00096 {
00097   DATUM* words = (DATUM *)malloc(sizeof(DATUM));
00098 
00099   register long pos;
00100   char buffer[1024];
00101 
00102   (*queryid)=0;
00103   (*slackid)=0;
00104   (*costfactor)=1;
00105   (*highest_feature) = 1;
00106 
00107   pos=0;
00108   (*comment)=NULL;
00109   while(line[pos] ) {      /* cut off comments */
00110     if((line[pos] == '#') && (!(*comment))) {
00111       line[pos]=0;
00112       (*comment)=&(line[pos+1]);
00113     }
00114     if(line[pos] == '\n') { /* strip the CR */
00115       line[pos]=0;
00116     }
00117     pos++;
00118   }
00119   if(!(*comment)) (*comment)=&(line[pos]);
00120   /* printf("Comment: '%s'\n",(*comment)); */
00121 
00122 
00123   if(sscanf(line,"%s",buffer) == EOF) return(NULL);
00124   pos=0;
00125   while((buffer[pos] != ':') && buffer[pos]) pos++;
00126   if(buffer[pos] == ':') {
00127         perror ("Line must start with label or 0!!!\n"); 
00128         printf("LINE: %s\n",line);
00129         exit (1); 
00130   }
00131   /* read the target value */
00132   if(sscanf(line,"%lf",label) == EOF) return(NULL);
00133   /* printf("label: '%f'\n",(float)(*label)); */
00134   pos=0;
00135   while(space_or_null((int)line[pos])) pos++;
00136   while((!space_or_null((int)line[pos])) && line[pos]) pos++;
00137   if (sscanf(line+pos,"%s",buffer) != 1) {
00138         perror ("x value expected\n"); 
00139         printf("LINE: %s\n",line);
00140         exit (1); 
00141   }
00142   words->x = atof(buffer);
00143   /* printf("x: '%f'\n",(float)words->x); */
00144   return(words);
00145 }
00146 
00147 
00154 void   
00155 plugin_write(FILE* os, void* _words) 
00156 {
00157   fprintf(os,"%.8g ", (double)((DATUM*)_words)->x);
00158 }
00159 
00160 
00164 void
00165 plugin_destructor(void* _words)
00166 {
00167   free(_words);
00168 }
00169 
00170 
00182 void
00183 plugin_clip(void* _words, int totwords)
00184 {
00185 
00186 }
00187 
00192 void
00193 plugin_kernel_setparm(char* kernel_parm)
00194 {
00195   if (strcmp(kernel_parm,"empty")==0 || strcmp(kernel_parm,"multk")==0)
00196     min_or_mult = MULTK;
00197   else if (strcmp(kernel_parm,"mink")==0)
00198     min_or_mult = MINK;
00199   else {
00200     fprintf(stderr,"invalid argument to -u: %s\n", kernel_parm); exit(1);
00201   }
00202 }

Generated on Wed Sep 5 17:26:48 2007 for SVM-Dlight by  doxygen 1.5.1