forked from facebookresearch/fastText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
435 lines (391 loc) · 12.1 KB
/
main.cc
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
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <iomanip>
#include <iostream>
#include <queue>
#include <stdexcept>
#include "args.h"
#include "fasttext.h"
using namespace fasttext;
void printUsage() {
std::cerr
<< "usage: fasttext <command> <args>\n\n"
<< "The commands supported by fasttext are:\n\n"
<< " supervised train a supervised classifier\n"
<< " quantize quantize a model to reduce the memory usage\n"
<< " test evaluate a supervised classifier\n"
<< " test-label print labels with precision and recall scores\n"
<< " predict predict most likely labels\n"
<< " predict-prob predict most likely labels with probabilities\n"
<< " skipgram train a skipgram model\n"
<< " cbow train a cbow model\n"
<< " print-word-vectors print word vectors given a trained model\n"
<< " print-sentence-vectors print sentence vectors given a trained model\n"
<< " print-ngrams print ngrams given a trained model and word\n"
<< " nn query for nearest neighbors\n"
<< " analogies query for analogies\n"
<< " dump dump arguments,dictionary,input/output vectors\n"
<< std::endl;
}
void printQuantizeUsage() {
std::cerr << "usage: fasttext quantize <args>" << std::endl;
}
void printTestUsage() {
std::cerr
<< "usage: fasttext test <model> <test-data> [<k>] [<th>]\n\n"
<< " <model> model filename\n"
<< " <test-data> test data filename (if -, read from stdin)\n"
<< " <k> (optional; 1 by default) predict top k labels\n"
<< " <th> (optional; 0.0 by default) probability threshold\n"
<< std::endl;
}
void printPredictUsage() {
std::cerr
<< "usage: fasttext predict[-prob] <model> <test-data> [<k>] [<th>]\n\n"
<< " <model> model filename\n"
<< " <test-data> test data filename (if -, read from stdin)\n"
<< " <k> (optional; 1 by default) predict top k labels\n"
<< " <th> (optional; 0.0 by default) probability threshold\n"
<< std::endl;
}
void printTestLabelUsage() {
std::cerr
<< "usage: fasttext test-label <model> <test-data> [<k>] [<th>]\n\n"
<< " <model> model filename\n"
<< " <test-data> test data filename\n"
<< " <k> (optional; 1 by default) predict top k labels\n"
<< " <th> (optional; 0.0 by default) probability threshold\n"
<< std::endl;
}
void printPrintWordVectorsUsage() {
std::cerr << "usage: fasttext print-word-vectors <model>\n\n"
<< " <model> model filename\n"
<< std::endl;
}
void printPrintSentenceVectorsUsage() {
std::cerr << "usage: fasttext print-sentence-vectors <model>\n\n"
<< " <model> model filename\n"
<< std::endl;
}
void printPrintNgramsUsage() {
std::cerr << "usage: fasttext print-ngrams <model> <word>\n\n"
<< " <model> model filename\n"
<< " <word> word to print\n"
<< std::endl;
}
void quantize(const std::vector<std::string>& args) {
Args a = Args();
if (args.size() < 3) {
printQuantizeUsage();
a.printHelp();
exit(EXIT_FAILURE);
}
a.parseArgs(args);
FastText fasttext;
// parseArgs checks if a->output is given.
fasttext.loadModel(a.output + ".bin");
fasttext.quantize(a);
fasttext.saveModel(a.output + ".ftz");
exit(0);
}
void printNNUsage() {
std::cout << "usage: fasttext nn <model> <k>\n\n"
<< " <model> model filename\n"
<< " <k> (optional; 10 by default) predict top k labels\n"
<< std::endl;
}
void printAnalogiesUsage() {
std::cout << "usage: fasttext analogies <model> <k>\n\n"
<< " <model> model filename\n"
<< " <k> (optional; 10 by default) predict top k labels\n"
<< std::endl;
}
void printDumpUsage() {
std::cout << "usage: fasttext dump <model> <option>\n\n"
<< " <model> model filename\n"
<< " <option> option from args,dict,input,output" << std::endl;
}
void test(const std::vector<std::string>& args) {
bool perLabel = args[1] == "test-label";
if (args.size() < 4 || args.size() > 6) {
perLabel ? printTestLabelUsage() : printTestUsage();
exit(EXIT_FAILURE);
}
const auto& model = args[2];
const auto& input = args[3];
int32_t k = args.size() > 4 ? std::stoi(args[4]) : 1;
real threshold = args.size() > 5 ? std::stof(args[5]) : 0.0;
FastText fasttext;
fasttext.loadModel(model);
Meter meter;
if (input == "-") {
fasttext.test(std::cin, k, threshold, meter);
} else {
std::ifstream ifs(input);
if (!ifs.is_open()) {
std::cerr << "Test file cannot be opened!" << std::endl;
exit(EXIT_FAILURE);
}
fasttext.test(ifs, k, threshold, meter);
}
if (perLabel) {
std::cout << std::fixed << std::setprecision(6);
auto writeMetric = [](const std::string& name, double value) {
std::cout << name << " : ";
if (std::isfinite(value)) {
std::cout << value;
} else {
std::cout << "--------";
}
std::cout << " ";
};
std::shared_ptr<const Dictionary> dict = fasttext.getDictionary();
for (int32_t labelId = 0; labelId < dict->nlabels(); labelId++) {
writeMetric("F1-Score", meter.f1Score(labelId));
writeMetric("Precision", meter.precision(labelId));
writeMetric("Recall", meter.recall(labelId));
std::cout << " " << dict->getLabel(labelId) << std::endl;
}
}
meter.writeGeneralMetrics(std::cout, k);
exit(0);
}
void printPredictions(
const std::vector<std::pair<real, std::string>>& predictions,
bool printProb,
bool multiline) {
bool first = true;
for (const auto& prediction : predictions) {
if (!first && !multiline) {
std::cout << " ";
}
first = false;
std::cout << prediction.second;
if (printProb) {
std::cout << " " << prediction.first;
}
if (multiline) {
std::cout << std::endl;
}
}
if (!multiline) {
std::cout << std::endl;
}
}
void predict(const std::vector<std::string>& args) {
if (args.size() < 4 || args.size() > 6) {
printPredictUsage();
exit(EXIT_FAILURE);
}
int32_t k = 1;
real threshold = 0.0;
if (args.size() > 4) {
k = std::stoi(args[4]);
if (args.size() == 6) {
threshold = std::stof(args[5]);
}
}
bool printProb = args[1] == "predict-prob";
FastText fasttext;
fasttext.loadModel(std::string(args[2]));
std::ifstream ifs;
std::string infile(args[3]);
bool inputIsStdIn = infile == "-";
if (!inputIsStdIn) {
ifs.open(infile);
if (!inputIsStdIn && !ifs.is_open()) {
std::cerr << "Input file cannot be opened!" << std::endl;
exit(EXIT_FAILURE);
}
}
std::istream& in = inputIsStdIn ? std::cin : ifs;
std::vector<std::pair<real, std::string>> predictions;
while (fasttext.predictLine(in, predictions, k, threshold)) {
printPredictions(predictions, printProb, false);
}
if (ifs.is_open()) {
ifs.close();
}
exit(0);
}
void printWordVectors(const std::vector<std::string> args) {
if (args.size() != 3) {
printPrintWordVectorsUsage();
exit(EXIT_FAILURE);
}
FastText fasttext;
fasttext.loadModel(std::string(args[2]));
std::string word;
Vector vec(fasttext.getDimension());
while (std::cin >> word) {
fasttext.getWordVector(vec, word);
std::cout << word << " " << vec << std::endl;
}
exit(0);
}
void printSentenceVectors(const std::vector<std::string> args) {
if (args.size() != 3) {
printPrintSentenceVectorsUsage();
exit(EXIT_FAILURE);
}
FastText fasttext;
fasttext.loadModel(std::string(args[2]));
Vector svec(fasttext.getDimension());
while (std::cin.peek() != EOF) {
fasttext.getSentenceVector(std::cin, svec);
// Don't print sentence
std::cout << svec << std::endl;
}
exit(0);
}
void printNgrams(const std::vector<std::string> args) {
if (args.size() != 4) {
printPrintNgramsUsage();
exit(EXIT_FAILURE);
}
FastText fasttext;
fasttext.loadModel(std::string(args[2]));
std::string word(args[3]);
std::vector<std::pair<std::string, Vector>> ngramVectors =
fasttext.getNgramVectors(word);
for (const auto& ngramVector : ngramVectors) {
std::cout << ngramVector.first << " " << ngramVector.second << std::endl;
}
exit(0);
}
void nn(const std::vector<std::string> args) {
int32_t k;
if (args.size() == 3) {
k = 10;
} else if (args.size() == 4) {
k = std::stoi(args[3]);
} else {
printNNUsage();
exit(EXIT_FAILURE);
}
FastText fasttext;
fasttext.loadModel(std::string(args[2]));
std::string prompt("Query word? ");
std::cout << prompt;
std::string queryWord;
while (std::cin >> queryWord) {
printPredictions(fasttext.getNN(queryWord, k), true, true);
std::cout << prompt;
}
exit(0);
}
void analogies(const std::vector<std::string> args) {
int32_t k;
if (args.size() == 3) {
k = 10;
} else if (args.size() == 4) {
k = std::stoi(args[3]);
} else {
printAnalogiesUsage();
exit(EXIT_FAILURE);
}
if (k <= 0) {
throw std::invalid_argument("k needs to be 1 or higher!");
}
FastText fasttext;
std::string model(args[2]);
std::cout << "Loading model " << model << std::endl;
fasttext.loadModel(model);
std::string prompt("Query triplet (A - B + C)? ");
std::string wordA, wordB, wordC;
std::cout << prompt;
while (true) {
std::cin >> wordA;
std::cin >> wordB;
std::cin >> wordC;
printPredictions(fasttext.getAnalogies(k, wordA, wordB, wordC), true, true);
std::cout << prompt;
}
exit(0);
}
void train(const std::vector<std::string> args) {
Args a = Args();
a.parseArgs(args);
FastText fasttext;
std::string outputFileName(a.output + ".bin");
std::ofstream ofs(outputFileName);
if (!ofs.is_open()) {
throw std::invalid_argument(
outputFileName + " cannot be opened for saving.");
}
ofs.close();
fasttext.train(a);
fasttext.saveModel(outputFileName);
fasttext.saveVectors(a.output + ".vec");
if (a.saveOutput) {
fasttext.saveOutput(a.output + ".output");
}
}
void dump(const std::vector<std::string>& args) {
if (args.size() < 4) {
printDumpUsage();
exit(EXIT_FAILURE);
}
std::string modelPath = args[2];
std::string option = args[3];
FastText fasttext;
fasttext.loadModel(modelPath);
if (option == "args") {
fasttext.getArgs().dump(std::cout);
} else if (option == "dict") {
fasttext.getDictionary()->dump(std::cout);
} else if (option == "input") {
if (fasttext.isQuant()) {
std::cerr << "Not supported for quantized models." << std::endl;
} else {
fasttext.getInputMatrix()->dump(std::cout);
}
} else if (option == "output") {
if (fasttext.isQuant()) {
std::cerr << "Not supported for quantized models." << std::endl;
} else {
fasttext.getOutputMatrix()->dump(std::cout);
}
} else {
printDumpUsage();
exit(EXIT_FAILURE);
}
}
int main(int argc, char** argv) {
std::vector<std::string> args(argv, argv + argc);
if (args.size() < 2) {
printUsage();
exit(EXIT_FAILURE);
}
std::string command(args[1]);
if (command == "skipgram" || command == "cbow" || command == "supervised") {
train(args);
} else if (command == "test" || command == "test-label") {
test(args);
} else if (command == "quantize") {
quantize(args);
} else if (command == "print-word-vectors") {
printWordVectors(args);
} else if (command == "print-sentence-vectors") {
printSentenceVectors(args);
} else if (command == "print-ngrams") {
printNgrams(args);
} else if (command == "nn") {
nn(args);
} else if (command == "analogies") {
analogies(args);
} else if (command == "predict" || command == "predict-prob") {
predict(args);
} else if (command == "dump") {
dump(args);
} else {
printUsage();
exit(EXIT_FAILURE);
}
return 0;
}