-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlosbflib.c
More file actions
788 lines (663 loc) · 18.6 KB
/
losbflib.c
File metadata and controls
788 lines (663 loc) · 18.6 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*
* OSBF-Lua - library for text classification
*
* This software is licensed to the public under the Free Software
* Foundation's GNU GPL, version 2. You may obtain a copy of the
* GPL by visiting the Free Software Foundations web site at
* www.fsf.org, and a copy is included in this distribution.
*
* Copyright 2005, 2006, 2007 Fidelis Assis, all rights reserved.
* Copyright 2005, 2006, 2007 Williams Yerazunis, all rights reserved.
*
* Read the HISTORY_AND_AGREEMENT for details.
*
*/
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <dirent.h>
#include <inttypes.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "osbflib.h"
extern int luaopen_osbf (lua_State * L);
/* configurable constants */
extern uint32_t microgroom_chain_length;
extern uint32_t microgroom_stop_after;
extern double K1, K2, K3;
extern uint32_t max_token_size, max_long_tokens;
extern uint32_t limit_token_size;
/* macro to `unsign' a character */
#ifndef uchar
#define uchar(c) ((unsigned char)(c))
#endif
/* db key names */
static char key_classes[] = "classes";
static char key_ncfs[] = "ncfs";
static char key_delimiters[] = "delimiters";
/* pR scale calibration factor - pR_SCF
This value is used to calibrate the pR scale so that
values in the interval [-20, 20] indicate the need
of reinforcement training, even if the classification
is correct.
The default pR_SCF was determined experimentally,
but can be changed using the osbf.config call.
*/
static double pR_SCF = 0.59;
/**********************************************************/
static int
lua_osbf_config (lua_State * L)
{
int options_set = 0;
luaL_checktype (L, 1, LUA_TTABLE);
lua_pushstring (L, "max_chain");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
microgroom_chain_length = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushstring (L, "stop_after");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
microgroom_stop_after = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushstring (L, "K1");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
K1 = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushstring (L, "K2");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
K2 = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushstring (L, "K3");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
K3 = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushstring (L, "limit_token_size");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
limit_token_size = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushstring (L, "max_token_size");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
max_token_size = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushstring (L, "max_long_tokens");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
max_long_tokens = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushstring (L, "pR_SCF");
lua_gettable (L, 1);
if (lua_isnumber (L, -1))
{
pR_SCF = luaL_checknumber (L, -1);
options_set++;
}
lua_pop (L, 1);
lua_pushnumber (L, (lua_Number) options_set);
return 1;
}
/**********************************************************/
static int
lua_osbf_createdb (lua_State * L)
{
const char *cfcname;
uint32_t buckets;
uint32_t minor = 0;
char errmsg[OSBF_ERROR_MESSAGE_LEN] = { '\0' };
int32_t num_classes;
/* check if the second arg is a table */
luaL_checktype (L, 1, LUA_TTABLE);
/* get the number of classes to create */
num_classes = (int) lua_rawlen (L, 1);
/* get number of buckets */
buckets = luaL_checknumber (L, 2);
lua_pushnil (L); /* first key */
while (lua_next (L, 1) != 0)
{
cfcname = luaL_checkstring (L, -1);
lua_pop (L, 1);
if (osbf_create_cfcfile (cfcname, buckets, OSBF_VERSION,
minor, errmsg) != EXIT_SUCCESS)
{
num_classes = -1;
break;
}
}
if (num_classes >= 0)
lua_pushnumber (L, (lua_Number) num_classes);
else
lua_pushnil (L);
lua_pushstring (L, errmsg);
return 2;
}
/**********************************************************/
/* removes all classes (files) in a database */
/* returns the number of files removed or error */
/* and the number of the last file removed */
static int
lua_osbf_removedb (lua_State * L)
{
const char *cfcname;
char errmsg[OSBF_ERROR_MESSAGE_LEN] = { '\0' };
int num_classes;
int save_errno, removed;
/* check if the second arg is a table */
luaL_checktype (L, 1, LUA_TTABLE);
/* get the number of classes to remove */
num_classes = (int) lua_rawlen (L, 1);
removed = 0;
lua_pushnil (L); /* first key */
while (lua_next (L, 1) != 0)
{
cfcname = luaL_checkstring (L, -1);
lua_pop (L, 1);
if (remove (cfcname) == 0)
removed++;
else
{
save_errno = errno;
strncat (errmsg, cfcname, OSBF_ERROR_MESSAGE_LEN);
strncat (errmsg, ": ", OSBF_ERROR_MESSAGE_LEN);
/* append err message after file name */
strncat (errmsg, strerror (save_errno), OSBF_ERROR_MESSAGE_LEN);
break;
}
}
if (errmsg[0] != '\0')
{
lua_pushnil (L);
lua_pushstring (L, errmsg);
return 2;
}
else
{
/* return the number of files deleted */
lua_pushnumber (L, (lua_Number) removed);
return 1;
}
}
/**********************************************************/
static int
lua_osbf_classify (lua_State * L)
{
const unsigned char *text;
size_t text_len;
const char *delimiters; /* extra token delimiters */
size_t delimiters_len;
const char *classes[OSBF_MAX_CLASSES + 1]; /* set of classes */
unsigned ncfs; /* defines a partition with 2 subsets of the set */
/* "classes". The first "ncfs" classes form the */
/* first subset. The others form the second one. */
uint32_t flags = 0; /* default value */
double min_p_ratio; /* min pmax/p,in ratio */
/* class probabilities are returned in p_classes */
double p_classes[OSBF_MAX_CLASSES];
uint32_t p_trainings[OSBF_MAX_CLASSES];
char errmsg[OSBF_ERROR_MESSAGE_LEN] = { '\0' };
unsigned i, i_pmax, num_classes;
double p_first_subset, p_second_subset;
/* get text pointer and text len */
text = (unsigned char *) luaL_checklstring (L, 1, &text_len);
/* check if the second arg is a table */
luaL_checktype (L, 2, LUA_TTABLE);
/* extract the class table from inside the db table */
lua_pushstring (L, key_classes);
lua_gettable (L, 2);
/* extract the classes */
/* check if the arg in the top is a table */
luaL_checktype (L, -1, LUA_TTABLE);
lua_pushnil (L);
num_classes = 0;
while (num_classes < OSBF_MAX_CLASSES && lua_next (L, -2) != 0)
{
classes[num_classes++] = luaL_checkstring (L, -1);
lua_pop (L, 1);
}
classes[num_classes] = NULL;
/* remove last index of the class table and the table itself */
lua_pop (L, 1);
if (num_classes < 1)
return luaL_error (L, "at least one class must be given");
/* extract the number of classes in the first subset */
lua_pushstring (L, key_ncfs);
lua_gettable (L, 2);
ncfs = luaL_checknumber (L, -1);
lua_pop (L, 1);
if (ncfs > num_classes)
ncfs = num_classes;
/* extract the extra token delimiters */
lua_pushstring (L, key_delimiters);
lua_gettable (L, 2);
delimiters = luaL_checklstring (L, -1, &delimiters_len);
lua_pop (L, 1);
/* extract flags, if any */
flags = (uint32_t) luaL_optnumber (L, 3, 0);
/* extract p_min_ratio if any */
min_p_ratio = (double) luaL_optnumber (L, 4, OSBF_MIN_PMAX_PMIN_RATIO);
/* call osbf_classify */
if (osbf_bayes_classify (text, text_len, delimiters, classes,
flags, min_p_ratio, p_classes, p_trainings,
errmsg) < 0)
{
lua_pushnil (L);
lua_pushstring (L, errmsg);
return 2;
}
else
{
lua_newtable (L);
i_pmax = 0;
p_first_subset = p_second_subset = 10 * DBL_MIN;
for (i = 0; i < num_classes; i++)
{
lua_pushnumber (L, (lua_Number) p_classes[i]);
lua_rawseti (L, -2, i + 1);
if (p_classes[i] > p_classes[i_pmax])
i_pmax = i;
if (i < ncfs)
p_first_subset += p_classes[i];
else
p_second_subset += p_classes[i];
}
/*
* return pR, log10 of the ratio between the sum of the
* probabilities in the first subset and the sum of the
* probabilities in the second one.
*/
lua_pushnumber (L,
(lua_Number) pR_SCF *
log10 (p_first_subset / p_second_subset));
/* exchange array and pR positions on the stack */
lua_insert (L, -2);
/* return index to the class with highest probability */
lua_pushnumber (L, (lua_Number) i_pmax + 1);
/* push table with number of trainings per class */
lua_newtable (L);
for (i = 0; i < num_classes; i++)
{
lua_pushnumber (L, (lua_Number) p_trainings[i]);
lua_rawseti (L, -2, i + 1);
}
}
return 4;
}
/**********************************************************/
static int
osbf_train (lua_State * L, int sense)
{
const unsigned char *text;
size_t text_len;
const char *delimiters; /* extra token delimiters */
size_t delimiters_len;
const char *classes[OSBF_MAX_CLASSES + 1];
int num_classes;
size_t ctbt; /* index of the class to be trained */
uint32_t flags = 0; /* default value */
char errmsg[OSBF_ERROR_MESSAGE_LEN] = { '\0' };
/* get text pointer and text len */
text = (unsigned char *) luaL_checklstring (L, 1, &text_len);
/* check if the second arg is a table */
luaL_checktype (L, 2, LUA_TTABLE);
/* extract the class table from inside the db table */
lua_pushstring (L, key_classes);
lua_gettable (L, 2);
/* extract the classes */
/* check if the arg in the top is a table */
luaL_checktype (L, -1, LUA_TTABLE);
lua_pushnil (L);
num_classes = 0;
while (num_classes < OSBF_MAX_CLASSES && lua_next (L, -2) != 0)
{
classes[num_classes++] = luaL_checkstring (L, -1);
lua_pop (L, 1);
}
classes[num_classes] = NULL;
/* remove last index of the class table and the table itself */
lua_pop (L, 1);
if (num_classes < 1)
return luaL_error (L, "at least one class must be given");
/* extract the extra token delimiters */
lua_pushstring (L, key_delimiters);
lua_gettable (L, 2);
delimiters = luaL_checklstring (L, -1, &delimiters_len);
lua_pop (L, 1);
/* get the index of the class to be trained */
ctbt = luaL_checknumber (L, 3) - 1;
/* get flags */
if (lua_isnumber (L, 4))
flags = (uint32_t) luaL_checknumber (L, 4);
if (osbf_bayes_learn (text, text_len, delimiters, classes,
ctbt, sense, flags, errmsg) < 0)
{
lua_pushnil (L);
lua_pushstring (L, errmsg);
return 2;
}
else
{
lua_pushboolean (L, 1);
return 1;
}
}
/**********************************************************/
static int
lua_osbf_learn (lua_State * L)
{
return osbf_train (L, 1);
}
/**********************************************************/
static int
lua_osbf_unlearn (lua_State * L)
{
return osbf_train (L, -1);
}
/**********************************************************/
static int
lua_osbf_dump (lua_State * L)
{
const char *cfcfile, *csvfile;
char errmsg[OSBF_ERROR_MESSAGE_LEN];
cfcfile = luaL_checkstring (L, 1);
csvfile = luaL_checkstring (L, 2);
if (osbf_dump (cfcfile, csvfile, errmsg) == 0)
{
lua_pushboolean (L, 1);
return 1;
}
else
{
lua_pushnil (L);
lua_pushstring (L, errmsg);
return 2;
}
}
/**********************************************************/
static int
lua_osbf_restore (lua_State * L)
{
const char *cfcfile, *csvfile;
char errmsg[OSBF_ERROR_MESSAGE_LEN];
cfcfile = luaL_checkstring (L, 1);
csvfile = luaL_checkstring (L, 2);
if (osbf_restore (cfcfile, csvfile, errmsg) == 0)
{
lua_pushboolean (L, 1);
return 1;
}
else
{
lua_pushnil (L);
lua_pushstring (L, errmsg);
return 2;
}
}
/**********************************************************/
static int
lua_osbf_import (lua_State * L)
{
const char *cfcfile, *csvfile;
char errmsg[OSBF_ERROR_MESSAGE_LEN];
cfcfile = luaL_checkstring (L, 1);
csvfile = luaL_checkstring (L, 2);
if (osbf_import (cfcfile, csvfile, errmsg) == 0)
{
lua_pushboolean (L, 1);
return 1;
}
else
{
lua_pushnil (L);
lua_pushstring (L, errmsg);
return 2;
}
}
/**********************************************************/
static int
lua_osbf_stats (lua_State * L)
{
const char *cfcfile;
STATS_STRUCT class;
char errmsg[OSBF_ERROR_MESSAGE_LEN];
int full = 1;
cfcfile = luaL_checkstring (L, 1);
if (lua_isboolean (L, 2))
{
full = lua_toboolean (L, 2);
}
if (osbf_stats (cfcfile, &class, errmsg, full) == 0)
{
lua_newtable (L);
lua_pushliteral (L, "version");
lua_pushnumber (L, (lua_Number) class.version);
lua_settable (L, -3);
lua_pushliteral (L, "buckets");
lua_pushnumber (L, (lua_Number) class.total_buckets);
lua_settable (L, -3);
lua_pushliteral (L, "bucket_size");
lua_pushnumber (L, (lua_Number) class.bucket_size);
lua_settable (L, -3);
lua_pushliteral (L, "header_size");
lua_pushnumber (L, (lua_Number) class.header_size);
lua_settable (L, -3);
lua_pushliteral (L, "learnings");
lua_pushnumber (L, (lua_Number) class.learnings);
lua_settable (L, -3);
lua_pushliteral (L, "extra_learnings");
lua_pushnumber (L, (lua_Number) class.extra_learnings);
lua_settable (L, -3);
lua_pushliteral (L, "mistakes");
lua_pushnumber (L, (lua_Number) class.mistakes);
lua_settable (L, -3);
lua_pushliteral (L, "classifications");
lua_pushnumber (L, (lua_Number) class.classifications);
lua_settable (L, -3);
if (full == 1)
{
lua_pushliteral (L, "chains");
lua_pushnumber (L, (lua_Number) class.num_chains);
lua_settable (L, -3);
lua_pushliteral (L, "max_chain");
lua_pushnumber (L, (lua_Number) class.max_chain);
lua_settable (L, -3);
lua_pushliteral (L, "avg_chain");
lua_pushnumber (L, (lua_Number) class.avg_chain);
lua_settable (L, -3);
lua_pushliteral (L, "max_displacement");
lua_pushnumber (L, (lua_Number) class.max_displacement);
lua_settable (L, -3);
lua_pushliteral (L, "unreachable");
lua_pushnumber (L, (lua_Number) class.unreachable);
lua_settable (L, -3);
lua_pushliteral (L, "used_buckets");
lua_pushnumber (L, (lua_Number) class.used_buckets);
lua_settable (L, -3);
lua_pushliteral (L, "use");
if (class.total_buckets > 0)
lua_pushnumber (L, (lua_Number) ((double) class.used_buckets /
class.total_buckets));
else
lua_pushnumber (L, (lua_Number) 100);
lua_settable (L, -3);
}
return 1;
}
else
{
lua_pushnil (L);
lua_pushstring (L, errmsg);
return 2;
}
}
/**********************************************************/
/*
** Assumes the table is on top of the stack.
*/
static void
set_info (lua_State * L)
{
lua_pushliteral (L, "_COPYRIGHT");
lua_pushliteral (L, "Copyright (C) 2005, 2006 Fidelis Assis");
lua_settable (L, -3);
lua_pushliteral (L, "_DESCRIPTION");
lua_pushliteral (L, "OSBF-Lua is a Lua library for text classification.");
lua_settable (L, -3);
lua_pushliteral (L, "_NAME");
lua_pushliteral (L, "OSBF-Lua");
lua_settable (L, -3);
lua_pushliteral (L, "_VERSION");
lua_pushliteral (L, LIB_VERSION);
lua_settable (L, -3);
}
/**********************************************************/
/* auxiliary functions */
#define MAX_DIR_SIZE 256
static int
lua_osbf_changedir (lua_State * L)
{
const char *newdir = luaL_checkstring (L, 1);
if (chdir (newdir) != 0)
{
lua_pushboolean (L, 1);
return 1;
}
else
{
lua_pushnil (L);
lua_pushfstring (L, "can't change dir to '%s'\n", newdir);
return 2;
}
}
/**********************************************************/
static int
lua_osbf_getdir (lua_State * L)
{
char cur_dir[MAX_DIR_SIZE + 1];
if (getcwd (cur_dir, MAX_DIR_SIZE) != NULL)
{
lua_pushstring (L, cur_dir);
return 1;
}
else
{
lua_pushnil (L);
lua_pushstring (L, "can't get current dir");
return 2;
}
}
/**********************************************************/
/* Directory Iterator - from the PIL book */
/* forward declaration for the iterator function */
static int dir_iter (lua_State * L);
static int
l_dir (lua_State * L)
{
const char *path = luaL_checkstring (L, 1);
/* create a userdatum to store a DIR address */
DIR **d = (DIR **) lua_newuserdata (L, sizeof (DIR *));
/* set its metatable */
luaL_getmetatable (L, "LuaBook.dir");
lua_setmetatable (L, -2);
/* try to open the given directory */
*d = opendir (path);
if (*d == NULL) /* error opening the directory? */
luaL_error (L, "cannot open %s: %s", path, strerror (errno));
/* creates and returns the iterator function
(its sole upvalue, the directory userdatum,
is already on the stack top */
lua_pushcclosure (L, dir_iter, 1);
return 1;
}
static int
dir_iter (lua_State * L)
{
DIR *d = *(DIR **) lua_touserdata (L, lua_upvalueindex (1));
struct dirent *entry;
if ((entry = readdir (d)) != NULL)
{
lua_pushstring (L, entry->d_name);
return 1;
}
else
return 0; /* no more values to return */
}
static int
dir_gc (lua_State * L)
{
DIR *d = *(DIR **) lua_touserdata (L, 1);
if (d)
closedir (d);
return 0;
}
/**********************************************************/
static const struct luaL_Reg osbf[] = {
{"create_db", lua_osbf_createdb},
{"remove_db", lua_osbf_removedb},
{"config", lua_osbf_config},
{"classify", lua_osbf_classify},
{"learn", lua_osbf_learn},
{"unlearn", lua_osbf_unlearn},
{"dump", lua_osbf_dump},
{"restore", lua_osbf_restore},
{"import", lua_osbf_import},
{"stats", lua_osbf_stats},
{"getdir", lua_osbf_getdir},
{"chdir", lua_osbf_changedir},
{"dir", l_dir},
{NULL, NULL}
};
/*
** Open OSBF library
*/
int
luaopen_osbf (lua_State * L)
{
size_t n_funcs;
/* Open dir function */
luaL_newmetatable (L, "LuaBook.dir");
/* set its __gc field */
lua_pushstring (L, "__gc");
lua_pushcfunction (L, dir_gc);
lua_settable (L, -3);
n_funcs = sizeof(osbf)/sizeof(*osbf) - 1;
lua_createtable( L, 0, n_funcs );
luaL_setfuncs( L, osbf, 0 );
lua_pushvalue( L, -1 );
lua_setglobal( L, "osbf" );
set_info (L);
return 1;
}