/
fnb_judge.c
168 lines (151 loc) · 4.77 KB
/
fnb_judge.c
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
/*
* Copyright (C) 2008-2011 Trever L. Adams
*
* This file is part of srv_classify c-icap module and accompanying tools.
*
* srv_classify is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srv_classify is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#if (_FILE_OFFSET_BITS != 64)
#undef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#define NOT_CICAP
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <langinfo.h>
#include <wchar.h>
#include <wctype.h>
#include <time.h>
#include <dirent.h>
#include <string.h>
#include "hash.c"
#include "bayes.c"
char *judge_file;
char *fbc_dir;
int readArguments(int argc, char *argv[])
{
int i;
if(argc < 9)
{
printf("Format of arguments is:\n");
printf("\t-p PRIMARY_HASH_SEED\n");
printf("\t-s SECONDARY_HASH_SEED\n");
printf("\t-i INPUT_FILE_TO_JUDGE\n");
printf("\t-d CATEGORY_FNB_FILES_DIR\n");
printf("Spaces and case matter.\n");
return -1;
}
for(i=1; i<9; i+=2)
{
if(strcmp(argv[i], "-p") == 0) sscanf(argv[i+1], "%"PRIx32, &HASHSEED1);
else if(strcmp(argv[i], "-s") == 0) sscanf(argv[i+1], "%"PRIx32, &HASHSEED2);
else if(strcmp(argv[i], "-i") == 0)
{
judge_file = malloc(strlen(argv[i+1]) + 1);
sscanf(argv[i+1], "%s", judge_file);
}
else if(strcmp(argv[i], "-d") == 0)
{
int len = strlen(argv[i+1]);
fbc_dir = malloc(len + 1);
sscanf(argv[i+1], "%s", fbc_dir);
if(fbc_dir[len-1] == '/') fbc_dir[len-1] = '\0';
}
}
/* printf("Primary Seed: %"PRIX32"\n", HASHSEED1);
printf("Secondary Seed: %"PRIX32"\n", HASHSEED2);
printf("Learn File: %s\n", judge_file);*/
return 0;
}
void checkMakeUTF8(void)
{
setlocale(LC_ALL, "");
int utf8_mode = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0);
if(!utf8_mode) setlocale(LC_ALL, "en_US.UTF-8");
}
wchar_t *makeData(char *input_file)
{
int data=0;
struct stat stat_buf;
char *tempData=NULL;
wchar_t *myData=NULL;
int32_t realLen;
data=open(input_file, O_RDONLY);
fstat(data, &stat_buf);
tempData=malloc(stat_buf.st_size+1);
if(tempData==NULL) exit(-1);
read(data, tempData, stat_buf.st_size);
close(data);
tempData[stat_buf.st_size] = '\0';
myData=malloc((stat_buf.st_size+1) * UTF32_CHAR_SIZE);
realLen=mbstowcs(myData, tempData, stat_buf.st_size);
if(realLen!=-1) myData[realLen] = L'\0';
else printf("Bad character data in %s\n", input_file);
free(tempData);
return myData;
}
int main (int argc, char *argv[])
{
regexHead myRegexHead = {.head = NULL, .tail = NULL, .dirty = 0, . main_memory = NULL, .arrays = NULL, .lastarray = NULL};
wchar_t *myData;
HashList myHashes;
clock_t start, end;
HTMLClassification classification;
checkMakeUTF8();
initHTML();
initBayesClassifier();
if(readArguments(argc, argv)==-1) exit(-1);
myData=makeData(judge_file);
printf("Loading hashes -- be patient!\n");
loadMassBayesCategories(fbc_dir);
optimizeFBC(&NBJudgeHashList);
printf("Classifying\n");
start=clock();
mkRegexHead(&myRegexHead, myData, 0);
removeHTML(&myRegexHead);
regexMakeSingleBlock(&myRegexHead);
normalizeCurrency(&myRegexHead);
regexMakeSingleBlock(&myRegexHead);
// printf("%ld: %.*ls\n", myRegexHead.head->rm_eo - myRegexHead.head->rm_so, myRegexHead.head->rm_eo - myRegexHead.head->rm_so, myRegexHead.main_memory);
myHashes.hashes = malloc(sizeof(HTMLFeature) * HTML_MAX_FEATURE_COUNT);
myHashes.slots = HTML_MAX_FEATURE_COUNT;
myHashes.used = 0;
computeOSBHashes(&myRegexHead, HASHSEED1, HASHSEED2, &myHashes);
classification=doBayesPrepandClassify(&myHashes);
end=clock();
printf("Classification took %lf milliseconds\n", (double)((end-start)/(CLOCKS_PER_SEC/1000)));
printf("Best match: %s prob: %lf pR: %lf\n", classification.primary_name, classification.primary_probability, classification.primary_probScaled);
if(classification.secondary_name != NULL)
printf("Best match: %s prob: %lf pR: %lf\n", classification.secondary_name, classification.secondary_probability, classification.secondary_probScaled);
free(myHashes.hashes);
freeRegexHead(&myRegexHead);
free(judge_file);
free(fbc_dir);
deinitBayesClassifier();
deinitHTML();
return 0;
}