File: lib_svmlight_format.c

package info (click to toggle)
libocas 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 4,392 kB
  • ctags: 298
  • sloc: ansic: 6,290; makefile: 76
file content (97 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "lib_svmlight_format.h"

static int32_t next_occurence(char *line, int32_t start, char what)
{
  int32_t i;
  for(i=start; i < LIBSLF_MAXLINELEN && line[i] != '\0'; i++)
  {
    if(line[i] == what)
      return(i);
  }

  return(-1);
}

int32_t svmlight_format_parse_line(char *line, int32_t *label, uint32_t *feat_idx, double *feat_val)
{  
  int32_t beg, end, nnzf=0;

  end = next_occurence(line,0,' ');
  beg = end;
  if(end == -1) 
    return(-1);

  *label = (int32_t)atol(line);

  int go = 1;
  while(go) {
    end = next_occurence(line,beg,':');

    if(end == -1)
      return(nnzf);

    feat_idx[nnzf] = (uint32_t)atol(&line[beg]);

    beg = end + 1;

    end = next_occurence(line,beg,' ');
    if(end == -1) {
      end = next_occurence(line,beg,'\n');
      if(end == -1)
        return(-1);

      go = 0;
    }

    feat_val[nnzf] = atof(&line[beg]);    

    beg = end;

    nnzf++;
  }

  return(nnzf);
}


/* difference to svmlight_format_parse_line is that here the label is float */
int32_t svmlight_format_parse_line_doubley(char *line, double *label, uint32_t *feat_idx, double *feat_val)
{  
  int32_t beg, end, nnzf=0;

  end = next_occurence(line,0,' ');
  beg = end;
  if(end == -1) 
    return(-1);

  *label = (double)atof(line);

  int go = 1;
  while(go) {
    end = next_occurence(line,beg,':');

    if(end == -1)
      return(nnzf);

    feat_idx[nnzf] = (uint32_t)atol(&line[beg]);

    beg = end + 1;

    end = next_occurence(line,beg,' ');
    if(end == -1) {
      end = next_occurence(line,beg,'\n');
      if(end == -1)
        return(-1);

      go = 0;
    }

    feat_val[nnzf] = atof(&line[beg]);    

    beg = end;

    nnzf++;
  }

  return(nnzf);
}