eluka 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. data/.document +5 -0
  2. data/DOCUMENTATION_STANDARDS +39 -0
  3. data/Gemfile +13 -0
  4. data/Gemfile.lock +20 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +19 -0
  7. data/Rakefile +69 -0
  8. data/VERSION +1 -0
  9. data/examples/example.rb +59 -0
  10. data/ext/libsvm/COPYRIGHT +31 -0
  11. data/ext/libsvm/FAQ.html +1749 -0
  12. data/ext/libsvm/Makefile +25 -0
  13. data/ext/libsvm/Makefile.win +33 -0
  14. data/ext/libsvm/README +733 -0
  15. data/ext/libsvm/extconf.rb +1 -0
  16. data/ext/libsvm/heart_scale +270 -0
  17. data/ext/libsvm/java/Makefile +25 -0
  18. data/ext/libsvm/java/libsvm.jar +0 -0
  19. data/ext/libsvm/java/libsvm/svm.java +2776 -0
  20. data/ext/libsvm/java/libsvm/svm.m4 +2776 -0
  21. data/ext/libsvm/java/libsvm/svm_model.java +21 -0
  22. data/ext/libsvm/java/libsvm/svm_node.java +6 -0
  23. data/ext/libsvm/java/libsvm/svm_parameter.java +47 -0
  24. data/ext/libsvm/java/libsvm/svm_print_interface.java +5 -0
  25. data/ext/libsvm/java/libsvm/svm_problem.java +7 -0
  26. data/ext/libsvm/java/svm_predict.java +163 -0
  27. data/ext/libsvm/java/svm_scale.java +350 -0
  28. data/ext/libsvm/java/svm_toy.java +471 -0
  29. data/ext/libsvm/java/svm_train.java +318 -0
  30. data/ext/libsvm/java/test_applet.html +1 -0
  31. data/ext/libsvm/python/Makefile +4 -0
  32. data/ext/libsvm/python/README +331 -0
  33. data/ext/libsvm/python/svm.py +259 -0
  34. data/ext/libsvm/python/svmutil.py +242 -0
  35. data/ext/libsvm/svm-predict.c +226 -0
  36. data/ext/libsvm/svm-scale.c +353 -0
  37. data/ext/libsvm/svm-toy/gtk/Makefile +22 -0
  38. data/ext/libsvm/svm-toy/gtk/callbacks.cpp +423 -0
  39. data/ext/libsvm/svm-toy/gtk/callbacks.h +54 -0
  40. data/ext/libsvm/svm-toy/gtk/interface.c +164 -0
  41. data/ext/libsvm/svm-toy/gtk/interface.h +14 -0
  42. data/ext/libsvm/svm-toy/gtk/main.c +23 -0
  43. data/ext/libsvm/svm-toy/gtk/svm-toy.glade +238 -0
  44. data/ext/libsvm/svm-toy/qt/Makefile +17 -0
  45. data/ext/libsvm/svm-toy/qt/svm-toy.cpp +413 -0
  46. data/ext/libsvm/svm-toy/windows/svm-toy.cpp +456 -0
  47. data/ext/libsvm/svm-train.c +376 -0
  48. data/ext/libsvm/svm.cpp +3060 -0
  49. data/ext/libsvm/svm.def +19 -0
  50. data/ext/libsvm/svm.h +105 -0
  51. data/ext/libsvm/svm.o +0 -0
  52. data/ext/libsvm/tools/README +149 -0
  53. data/ext/libsvm/tools/checkdata.py +108 -0
  54. data/ext/libsvm/tools/easy.py +79 -0
  55. data/ext/libsvm/tools/grid.py +359 -0
  56. data/ext/libsvm/tools/subset.py +146 -0
  57. data/ext/libsvm/windows/libsvm.dll +0 -0
  58. data/ext/libsvm/windows/svm-predict.exe +0 -0
  59. data/ext/libsvm/windows/svm-scale.exe +0 -0
  60. data/ext/libsvm/windows/svm-toy.exe +0 -0
  61. data/ext/libsvm/windows/svm-train.exe +0 -0
  62. data/lib/eluka.rb +10 -0
  63. data/lib/eluka/bijection.rb +23 -0
  64. data/lib/eluka/data_point.rb +36 -0
  65. data/lib/eluka/document.rb +47 -0
  66. data/lib/eluka/feature_vector.rb +86 -0
  67. data/lib/eluka/features.rb +31 -0
  68. data/lib/eluka/model.rb +129 -0
  69. data/lib/fselect.rb +321 -0
  70. data/lib/grid.rb +25 -0
  71. data/test/helper.rb +18 -0
  72. data/test/test_eluka.rb +7 -0
  73. metadata +214 -0
@@ -0,0 +1,22 @@
1
+ CC? = gcc
2
+ CXX? = g++
3
+ CFLAGS = -Wall -O3 -g `pkg-config --libs gtk+-2.0`
4
+ LIBS = `pkg-config --libs gtk+-2.0`
5
+
6
+ svm-toy: main.o interface.o callbacks.o ../../svm.o
7
+ $(CXX) $(CFLAGS) main.o interface.o callbacks.o ../../svm.o -o svm-toy $(LIBS)
8
+
9
+ main.o: main.c
10
+ $(CC) $(CFLAGS) -c main.c
11
+
12
+ interface.o: interface.c interface.h
13
+ $(CC) $(CFLAGS) -c interface.c
14
+
15
+ callbacks.o: callbacks.cpp callbacks.h
16
+ $(CXX) $(CFLAGS) -c callbacks.cpp
17
+
18
+ ../../svm.o:
19
+ cd ../..; make svm.o
20
+
21
+ clean:
22
+ rm -f *~ callbacks.o svm-toy main.o interface.o callbacks.o ../../svm.o
@@ -0,0 +1,423 @@
1
+ #include <gtk/gtk.h>
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <ctype.h>
5
+ #include <list>
6
+ #include "callbacks.h"
7
+ #include "interface.h"
8
+ #include "../../svm.h"
9
+ using namespace std;
10
+
11
+ #define DEFAULT_PARAM "-t 2 -c 100"
12
+ #define XLEN 500
13
+ #define YLEN 500
14
+
15
+ GdkColor colors[] =
16
+ {
17
+ {0,0,0,0},
18
+ {0,0,120<<8,120<<8},
19
+ {0,120<<8,120<<8,0},
20
+ {0,120<<8,0,120<<8},
21
+ {0,0,200<<8,200<<8},
22
+ {0,200<<8,200<<8,0},
23
+ {0,200<<8,0,200<<8},
24
+ };
25
+
26
+ GdkGC *gc;
27
+ GdkPixmap *pixmap;
28
+ extern "C" GtkWidget *draw_main;
29
+ GtkWidget *draw_main;
30
+ extern "C" GtkWidget *entry_option;
31
+ GtkWidget *entry_option;
32
+
33
+ typedef struct {
34
+ double x, y;
35
+ signed char value;
36
+ } point;
37
+
38
+ list<point> point_list;
39
+ int current_value = 1;
40
+
41
+ extern "C" void svm_toy_initialize()
42
+ {
43
+ gboolean success[7];
44
+
45
+ gdk_colormap_alloc_colors(
46
+ gdk_colormap_get_system(),
47
+ colors,
48
+ 7,
49
+ FALSE,
50
+ TRUE,
51
+ success);
52
+
53
+ gc = gdk_gc_new(draw_main->window);
54
+ pixmap = gdk_pixmap_new(draw_main->window,XLEN,YLEN,-1);
55
+ gdk_gc_set_foreground(gc,&colors[0]);
56
+ gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN);
57
+ gtk_entry_set_text(GTK_ENTRY(entry_option),DEFAULT_PARAM);
58
+ }
59
+
60
+ void redraw_area(GtkWidget* widget, int x, int y, int w, int h)
61
+ {
62
+ gdk_draw_pixmap(widget->window,
63
+ gc,
64
+ pixmap,
65
+ x,y,x,y,w,h);
66
+ }
67
+
68
+ void draw_point(const point& p)
69
+ {
70
+ gdk_gc_set_foreground(gc,&colors[p.value+3]);
71
+ gdk_draw_rectangle(pixmap, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4);
72
+ gdk_draw_rectangle(draw_main->window, gc, TRUE,int(p.x*XLEN),int(p.y*YLEN),4,4);
73
+ }
74
+
75
+ void draw_all_points()
76
+ {
77
+ for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
78
+ draw_point(*p);
79
+ }
80
+
81
+ void clear_all()
82
+ {
83
+ point_list.clear();
84
+ gdk_gc_set_foreground(gc,&colors[0]);
85
+ gdk_draw_rectangle(pixmap,gc,TRUE,0,0,XLEN,YLEN);
86
+ redraw_area(draw_main,0,0,XLEN,YLEN);
87
+ }
88
+
89
+ void
90
+ on_button_change_clicked (GtkButton *button,
91
+ gpointer user_data)
92
+ {
93
+ ++current_value;
94
+ if(current_value > 3) current_value = 1;
95
+ }
96
+
97
+ void
98
+ on_button_run_clicked (GtkButton *button,
99
+ gpointer user_data)
100
+ {
101
+ // guard
102
+ if(point_list.empty()) return;
103
+
104
+ svm_parameter param;
105
+ int i,j;
106
+
107
+ // default values
108
+ param.svm_type = C_SVC;
109
+ param.kernel_type = RBF;
110
+ param.degree = 3;
111
+ param.gamma = 0;
112
+ param.coef0 = 0;
113
+ param.nu = 0.5;
114
+ param.cache_size = 100;
115
+ param.C = 1;
116
+ param.eps = 1e-3;
117
+ param.p = 0.1;
118
+ param.shrinking = 1;
119
+ param.probability = 0;
120
+ param.nr_weight = 0;
121
+ param.weight_label = NULL;
122
+ param.weight = NULL;
123
+
124
+ // parse options
125
+ const char *p = gtk_entry_get_text(GTK_ENTRY(entry_option));
126
+
127
+ while (1) {
128
+ while (*p && *p != '-')
129
+ p++;
130
+
131
+ if (*p == '\0')
132
+ break;
133
+
134
+ p++;
135
+ switch (*p++) {
136
+ case 's':
137
+ param.svm_type = atoi(p);
138
+ break;
139
+ case 't':
140
+ param.kernel_type = atoi(p);
141
+ break;
142
+ case 'd':
143
+ param.degree = atoi(p);
144
+ break;
145
+ case 'g':
146
+ param.gamma = atof(p);
147
+ break;
148
+ case 'r':
149
+ param.coef0 = atof(p);
150
+ break;
151
+ case 'n':
152
+ param.nu = atof(p);
153
+ break;
154
+ case 'm':
155
+ param.cache_size = atof(p);
156
+ break;
157
+ case 'c':
158
+ param.C = atof(p);
159
+ break;
160
+ case 'e':
161
+ param.eps = atof(p);
162
+ break;
163
+ case 'p':
164
+ param.p = atof(p);
165
+ break;
166
+ case 'h':
167
+ param.shrinking = atoi(p);
168
+ break;
169
+ case 'b':
170
+ param.probability = atoi(p);
171
+ break;
172
+ case 'w':
173
+ ++param.nr_weight;
174
+ param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight);
175
+ param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight);
176
+ param.weight_label[param.nr_weight-1] = atoi(p);
177
+ while(*p && !isspace(*p)) ++p;
178
+ param.weight[param.nr_weight-1] = atof(p);
179
+ break;
180
+ }
181
+ }
182
+
183
+ // build problem
184
+ svm_problem prob;
185
+
186
+ prob.l = point_list.size();
187
+ prob.y = new double[prob.l];
188
+
189
+ if(param.kernel_type == PRECOMPUTED)
190
+ {
191
+ }
192
+ else if(param.svm_type == EPSILON_SVR ||
193
+ param.svm_type == NU_SVR)
194
+ {
195
+ if(param.gamma == 0) param.gamma = 1;
196
+ svm_node *x_space = new svm_node[2 * prob.l];
197
+ prob.x = new svm_node *[prob.l];
198
+
199
+ i = 0;
200
+ for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
201
+ {
202
+ x_space[2 * i].index = 1;
203
+ x_space[2 * i].value = q->x;
204
+ x_space[2 * i + 1].index = -1;
205
+ prob.x[i] = &x_space[2 * i];
206
+ prob.y[i] = q->y;
207
+ }
208
+
209
+ // build model & classify
210
+ svm_model *model = svm_train(&prob, &param);
211
+ svm_node x[2];
212
+ x[0].index = 1;
213
+ x[1].index = -1;
214
+ int *j = new int[XLEN];
215
+
216
+ for (i = 0; i < XLEN; i++)
217
+ {
218
+ x[0].value = (double) i / XLEN;
219
+ j[i] = (int)(YLEN*svm_predict(model, x));
220
+ }
221
+
222
+ gdk_gc_set_foreground(gc,&colors[0]);
223
+ gdk_draw_line(pixmap,gc,0,0,0,YLEN-1);
224
+ gdk_draw_line(draw_main->window,gc,0,0,0,YLEN-1);
225
+
226
+ int p = (int)(param.p * YLEN);
227
+ for(i = 1; i < XLEN; i++)
228
+ {
229
+ gdk_gc_set_foreground(gc,&colors[0]);
230
+ gdk_draw_line(pixmap,gc,i,0,i,YLEN-1);
231
+ gdk_draw_line(draw_main->window,gc,i,0,i,YLEN-1);
232
+
233
+ gdk_gc_set_foreground(gc,&colors[5]);
234
+ gdk_draw_line(pixmap,gc,i-1,j[i-1],i,j[i]);
235
+ gdk_draw_line(draw_main->window,gc,i-1,j[i-1],i,j[i]);
236
+
237
+ if(param.svm_type == EPSILON_SVR)
238
+ {
239
+ gdk_gc_set_foreground(gc,&colors[2]);
240
+ gdk_draw_line(pixmap,gc,i-1,j[i-1]+p,i,j[i]+p);
241
+ gdk_draw_line(draw_main->window,gc,i-1,j[i-1]+p,i,j[i]+p);
242
+
243
+ gdk_gc_set_foreground(gc,&colors[2]);
244
+ gdk_draw_line(pixmap,gc,i-1,j[i-1]-p,i,j[i]-p);
245
+ gdk_draw_line(draw_main->window,gc,i-1,j[i-1]-p,i,j[i]-p);
246
+ }
247
+ }
248
+
249
+ svm_free_and_destroy_model(&model);
250
+ delete[] j;
251
+ delete[] x_space;
252
+ delete[] prob.x;
253
+ delete[] prob.y;
254
+ }
255
+ else
256
+ {
257
+ if(param.gamma == 0) param.gamma = 0.5;
258
+ svm_node *x_space = new svm_node[3 * prob.l];
259
+ prob.x = new svm_node *[prob.l];
260
+
261
+ i = 0;
262
+ for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
263
+ {
264
+ x_space[3 * i].index = 1;
265
+ x_space[3 * i].value = q->x;
266
+ x_space[3 * i + 1].index = 2;
267
+ x_space[3 * i + 1].value = q->y;
268
+ x_space[3 * i + 2].index = -1;
269
+ prob.x[i] = &x_space[3 * i];
270
+ prob.y[i] = q->value;
271
+ }
272
+
273
+ // build model & classify
274
+ svm_model *model = svm_train(&prob, &param);
275
+ svm_node x[3];
276
+ x[0].index = 1;
277
+ x[1].index = 2;
278
+ x[2].index = -1;
279
+
280
+ for (i = 0; i < XLEN; i++)
281
+ for (j = 0; j < YLEN; j++) {
282
+ x[0].value = (double) i / XLEN;
283
+ x[1].value = (double) j / YLEN;
284
+ double d = svm_predict(model, x);
285
+ if (param.svm_type == ONE_CLASS && d<0) d=2;
286
+ gdk_gc_set_foreground(gc,&colors[(int)d]);
287
+ gdk_draw_point(pixmap,gc,i,j);
288
+ gdk_draw_point(draw_main->window,gc,i,j);
289
+ }
290
+
291
+ svm_free_and_destroy_model(&model);
292
+ delete[] x_space;
293
+ delete[] prob.x;
294
+ delete[] prob.y;
295
+ }
296
+ free(param.weight_label);
297
+ free(param.weight);
298
+ draw_all_points();
299
+ }
300
+
301
+ void
302
+ on_button_clear_clicked (GtkButton *button,
303
+ gpointer user_data)
304
+ {
305
+ clear_all();
306
+ }
307
+
308
+ void
309
+ on_window1_destroy (GtkObject *object,
310
+ gpointer user_data)
311
+ {
312
+ gtk_exit(0);
313
+ }
314
+
315
+ gboolean
316
+ on_draw_main_button_press_event (GtkWidget *widget,
317
+ GdkEventButton *event,
318
+ gpointer user_data)
319
+ {
320
+ point p = {(double)event->x/XLEN, (double)event->y/YLEN, current_value};
321
+ point_list.push_back(p);
322
+ draw_point(p);
323
+ return FALSE;
324
+ }
325
+
326
+ gboolean
327
+ on_draw_main_expose_event (GtkWidget *widget,
328
+ GdkEventExpose *event,
329
+ gpointer user_data)
330
+ {
331
+ redraw_area(widget,
332
+ event->area.x, event->area.y,
333
+ event->area.width, event->area.height);
334
+ return FALSE;
335
+ }
336
+
337
+ GtkWidget *fileselection;
338
+ static enum { SAVE, LOAD } fileselection_flag;
339
+
340
+ void show_fileselection()
341
+ {
342
+ fileselection = create_fileselection();
343
+ gtk_signal_connect_object(
344
+ GTK_OBJECT(GTK_FILE_SELECTION(fileselection)->ok_button),
345
+ "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
346
+ (GtkObject *) fileselection);
347
+
348
+ gtk_signal_connect_object (GTK_OBJECT
349
+ (GTK_FILE_SELECTION(fileselection)->cancel_button),
350
+ "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
351
+ (GtkObject *) fileselection);
352
+
353
+ gtk_widget_show(fileselection);
354
+ }
355
+
356
+ void
357
+ on_button_save_clicked (GtkButton *button,
358
+ gpointer user_data)
359
+ {
360
+ fileselection_flag = SAVE;
361
+ show_fileselection();
362
+ }
363
+
364
+
365
+ void
366
+ on_button_load_clicked (GtkButton *button,
367
+ gpointer user_data)
368
+ {
369
+ fileselection_flag = LOAD;
370
+ show_fileselection();
371
+ }
372
+
373
+ void
374
+ on_filesel_ok_clicked (GtkButton *button,
375
+ gpointer user_data)
376
+ {
377
+ gtk_widget_hide(fileselection);
378
+ const char *filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(fileselection));
379
+
380
+ if(fileselection_flag == SAVE)
381
+ {
382
+ FILE *fp = fopen(filename,"w");
383
+ if(fp)
384
+ {
385
+ for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
386
+ fprintf(fp,"%d 1:%f 2:%f\n", p->value, p->x, p->y);
387
+ fclose(fp);
388
+ }
389
+
390
+ }
391
+ else if(fileselection_flag == LOAD)
392
+ {
393
+ FILE *fp = fopen(filename,"r");
394
+ if(fp)
395
+ {
396
+ clear_all();
397
+ char buf[4096];
398
+ while(fgets(buf,sizeof(buf),fp))
399
+ {
400
+ int v;
401
+ double x,y;
402
+ if(sscanf(buf,"%d%*d:%lf%*d:%lf",&v,&x,&y)!=3)
403
+ break;
404
+ point p = {x,y,v};
405
+ point_list.push_back(p);
406
+ }
407
+ fclose(fp);
408
+ draw_all_points();
409
+ }
410
+ }
411
+ }
412
+
413
+ void
414
+ on_fileselection_destroy (GtkObject *object,
415
+ gpointer user_data)
416
+ {
417
+ }
418
+
419
+ void
420
+ on_filesel_cancel_clicked (GtkButton *button,
421
+ gpointer user_data)
422
+ {
423
+ }
@@ -0,0 +1,54 @@
1
+ #include <gtk/gtk.h>
2
+
3
+ #ifdef __cplusplus
4
+ extern "C" {
5
+ #endif
6
+
7
+ void
8
+ on_window1_destroy (GtkObject *object,
9
+ gpointer user_data);
10
+
11
+ gboolean
12
+ on_draw_main_button_press_event (GtkWidget *widget,
13
+ GdkEventButton *event,
14
+ gpointer user_data);
15
+
16
+ gboolean
17
+ on_draw_main_expose_event (GtkWidget *widget,
18
+ GdkEventExpose *event,
19
+ gpointer user_data);
20
+
21
+ void
22
+ on_button_change_clicked (GtkButton *button,
23
+ gpointer user_data);
24
+
25
+ void
26
+ on_button_run_clicked (GtkButton *button,
27
+ gpointer user_data);
28
+
29
+ void
30
+ on_button_clear_clicked (GtkButton *button,
31
+ gpointer user_data);
32
+
33
+ void
34
+ on_button_save_clicked (GtkButton *button,
35
+ gpointer user_data);
36
+
37
+ void
38
+ on_button_load_clicked (GtkButton *button,
39
+ gpointer user_data);
40
+
41
+ void
42
+ on_fileselection_destroy (GtkObject *object,
43
+ gpointer user_data);
44
+
45
+ void
46
+ on_filesel_ok_clicked (GtkButton *button,
47
+ gpointer user_data);
48
+
49
+ void
50
+ on_filesel_cancel_clicked (GtkButton *button,
51
+ gpointer user_data);
52
+ #ifdef __cplusplus
53
+ }
54
+ #endif