-
Notifications
You must be signed in to change notification settings - Fork 6
/
tagcorpus.cxx
290 lines (245 loc) · 7.27 KB
/
tagcorpus.cxx
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
#include "document.h"
#include "match_handlers.h"
#include "meta_handlers.h"
#include "print_handlers.h"
#include "score_handlers.h"
#include "segment_handlers.h"
#include "threaded_batch_tagger.h"
#include <fstream>
#include <iostream>
extern "C"
{
#include <getopt.h>
}
#define MAXFILENAMELEN 256
#define VERSION "1.1"
using namespace std;
int validate_req (char* var, const char name[]) {
if (strcmp(var, "") == 0) {
printf("Must specify %s file with --%s=filename\n", name, name);
exit(1);
}
return 1;
}
int validate_opt(char* var) {
return strcmp(var, "");
}
int main (int argc, char *argv[])
{
MetaBatchHandler batch_handler;
ThreadedBatchTagger batch_tagger;
// some default values for the command line arguments
char types[MAXFILENAMELEN] = "";
char entities[MAXFILENAMELEN] = "";
char names[MAXFILENAMELEN] = "";
char documents[MAXFILENAMELEN] = "";
char groups[MAXFILENAMELEN] = "";
char type_pairs[MAXFILENAMELEN] = "";
char stopwords[MAXFILENAMELEN] = "";
char localstopwords[MAXFILENAMELEN] = "";
bool autodetect = false;
bool tokenize_characters = false;
float document_weight = 1;
float paragraph_weight = 2;
float sentence_weight = 0.2;
float normalization_factor = 0.6;
int threads = 1;
char out_matches[MAXFILENAMELEN] = "";
char out_pairs[MAXFILENAMELEN] = "";
char out_segments[MAXFILENAMELEN] = "";
int c; // parse command line arguments
while (1) {
static struct option long_options[] =
{
{"types", required_argument, 0, 'y'},
{"entities", required_argument, 0, 'e'},
{"names", required_argument, 0, 'n'},
{"documents", optional_argument, 0, 'i'},
{"groups", optional_argument, 0, 'g'},
{"type-pairs", optional_argument, 0, 'p'},
{"stopwords", optional_argument, 0, 's'},
{"local-stopwords", optional_argument, 0, 'l'},
{"autodetect", no_argument, 0, 'u'},
{"tokenize-characters", no_argument, 0, 'z'},
{"document-weight", optional_argument, 0, 'd'},
{"paragraph-weight", optional_argument, 0, 'r'},
{"sentence-weight", optional_argument, 0, 'c'},
{"normalization-factor", optional_argument, 0, 'f'},
{"threads", optional_argument, 0, 't'},
{"out-matches", optional_argument, 0, 'm'},
{"out-pairs", optional_argument, 0, 'a'},
{"out-segments", optional_argument, 0, 'b'},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "y:e:n:i:g:p:s:l:u:d:r:c:f:t:m:a:h:", long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c) {
case 'h':
printf("Usage: %s [OPTIONS]\n", argv[0]);
printf("Required Arguments\n");
printf("\t--types=filename\n");
printf("\t--entities=filename\n");
printf("\t--names=filename\n");
printf("Optional Arguments\n");
printf("\t--documents=filename\tRead input from file instead of from STDIN\n");
printf("\t--groups=filename\n");
printf("\t--type-pairs=filename\tTypes of pairs that are allowed\n");
printf("\t--stopwords=filename\n");
printf("\t--local-stopwords=filename\n");
printf("\t--autodetect Turn autodetect on\n");
printf("\t--tokenize-characters Turn single-character tokenization on\n");
printf("\t--document-weight=%1.2f\n", document_weight);
printf("\t--paragraph-weight=%1.2f\n", paragraph_weight);
printf("\t--sentence-weight=%1.2f\n", sentence_weight);
printf("\t--normalization-factor=%1.2f\n", normalization_factor);
printf("\t--threads=%d\n", threads);
printf("\t--out-matches=filename\n");
printf("\t--out-pairs=filename\n");
printf("\t--out-segments=filename\n");
exit(0);
case 'v':
printf("JensenLab Tagger version %s\n", VERSION);
exit(0);
case 'y':
if (optarg) {
strncpy(types, optarg, min(MAXFILENAMELEN, int(sizeof(types))));
}
break;
case 'e':
if (optarg) {
strncpy(entities, optarg, min(MAXFILENAMELEN, int(sizeof(entities))));
}
break;
case 'l':
if (optarg) {
strncpy(localstopwords, optarg, min(MAXFILENAMELEN, int(sizeof(localstopwords))));
}
break;
case 'n':
if (optarg) {
strncpy(names, optarg, min(MAXFILENAMELEN, int(sizeof(names))));
}
break;
case 'i':
if (optarg) {
strncpy(documents, optarg, min(MAXFILENAMELEN, int(sizeof(documents))));
}
break;
case 'g':
if (optarg) {
strncpy(groups, optarg, min(MAXFILENAMELEN, int(sizeof(groups))));
}
break;
case 'p':
if (optarg) {
strncpy(type_pairs, optarg, min(MAXFILENAMELEN, int(sizeof(type_pairs))));
}
break;
case 's':
if (optarg) {
strncpy(stopwords, optarg, min(MAXFILENAMELEN, int(sizeof(stopwords))));
}
break;
case 'u':
autodetect = true;
break;
case 'z':
tokenize_characters = true;
case 'd':
if (optarg) {
document_weight = atof(optarg);
}
break;
case 'r':
if (optarg) {
paragraph_weight = atof(optarg);
}
break;
case 'c':
if (optarg) {
sentence_weight = atof(optarg);
}
break;
case 'f':
if (optarg) {
normalization_factor = atof(optarg);
}
break;
case 't':
if (optarg) {
threads = atoi(optarg);
}
break;
case 'm':
if (optarg) {
strncpy(out_matches, optarg, min(MAXFILENAMELEN, int(sizeof(out_matches))));
}
break;
case 'a':
if (optarg) {
strncpy(out_pairs, optarg, min(MAXFILENAMELEN, int(sizeof(out_pairs))));
}
break;
case 'b':
if (optarg) {
strncpy(out_segments, optarg, min(MAXFILENAMELEN, int(sizeof(out_segments))));
}
break;
case '?':
/* getopt_long already printed an error message. */
break;
default:
abort ();
}
}
validate_req(types, "types");
validate_req(entities, "entities");
validate_req(names, "names");
cerr << "# Loading data ...";
batch_tagger.load_names(entities, names);
if (validate_opt(groups)) {
batch_tagger.load_groups(groups);
}
if (validate_opt(stopwords)) {
batch_tagger.load_global(stopwords);
}
if (validate_opt(localstopwords)) {
batch_tagger.load_local(localstopwords);
}
cerr << " done." << endl;
TsvDocumentReader *document_reader;
if (validate_opt(documents)) {
document_reader = new TsvDocumentReader(documents);
}
else {
document_reader = new TsvDocumentReader(stdin);
}
if (validate_opt(out_matches)) {
batch_handler.push_back(new PrintBatchHandler(out_matches));
}
else {
batch_handler.push_back(new PrintBatchHandler(stdout));
}
if (validate_opt(out_pairs)) {
if (validate_opt(type_pairs)) {
batch_handler.push_back(new SelectiveScoreBatchHandler(out_pairs, batch_tagger.entity_type_map, document_weight, paragraph_weight, sentence_weight, normalization_factor, type_pairs));
}
else {
batch_handler.push_back(new ScoreBatchHandler(out_pairs, batch_tagger.entity_type_map, document_weight, paragraph_weight, sentence_weight, normalization_factor));
}
}
if (validate_opt(out_segments)) {
batch_handler.push_back(new SegmentBatchHandler(out_segments));
}
GetMatchesParams params(types);
params.auto_detect = autodetect;
params.tokenize_characters = tokenize_characters;
batch_tagger.process(threads, document_reader, params, &batch_handler);
cerr << endl << "# Batch done." << endl;
exit(0);
}