-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathMain.java
More file actions
138 lines (122 loc) · 3.93 KB
/
Copy pathMain.java
File metadata and controls
138 lines (122 loc) · 3.93 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
package fasttext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
public static void printUsage() {
System.out.print("usage: java -jar fasttext.jar <command> <args>\n\n"
+ "The commands supported by fasttext are:\n\n"
+ " supervised train a supervised classifier\n"
+ " test evaluate a supervised classifier\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-vectors print vectors given a trained model\n");
}
public static void printTestUsage() {
System.out.print("usage: java -jar fasttext.jar test <model> <test-data> [<k>]\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");
}
public static void printPredictUsage() {
System.out.print("usage: java -jar fasttext.jar predict[-prob] <model> <test-data> [<k>]\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");
}
public static void printPrintVectorsUsage() {
System.out.print("usage: java -jar fasttext.jar print-vectors <model>\n\n"
+ " <model> model filename\n");
}
public void test(String[] args) throws IOException, Exception {
int k = 1;
if (args.length == 3) {
k = 1;
} else if (args.length == 4) {
k = Integer.parseInt(args[3]);
} else {
printTestUsage();
System.exit(1);
}
FastText fasttext = new FastText();
fasttext.loadModel(args[1]);
String infile = args[2];
if ("-".equals(infile)) {
fasttext.test(System.in, k);
} else {
File file = new File(infile);
if (!(file.exists() && file.isFile() && file.canRead())) {
throw new IOException("Test file cannot be opened!");
}
fasttext.test(new FileInputStream(file), k);
}
}
public void predict(String[] args) throws IOException, Exception {
int k = 1;
if (args.length == 3) {
k = 1;
} else if (args.length == 4) {
k = Integer.parseInt(args[3]);
} else {
printPredictUsage();
System.exit(1);
}
boolean print_prob = "predict-prob".equalsIgnoreCase(args[0]);
FastText fasttext = new FastText();
fasttext.loadModel(args[1]);
String infile = args[2];
if ("-".equals(infile)) {
fasttext.predict(System.in, k, print_prob);
} else {
File file = new File(infile);
if (!(file.exists() && file.isFile() && file.canRead())) {
throw new IOException("Input file cannot be opened!");
}
fasttext.predict(new FileInputStream(file), k, print_prob);
}
}
public void printVectors(String[] args) throws IOException {
if (args.length != 2) {
printPrintVectorsUsage();
System.exit(1);
}
FastText fasttext = new FastText();
fasttext.loadModel(args[1]);
fasttext.printVectors();
}
public void train(String[] args) throws IOException, Exception {
Args a = new Args();
a.parseArgs(args);
FastText fasttext = new FastText();
fasttext.train(a);
}
public static void main(String[] args) {
Main op = new Main();
if (args.length == 0) {
printUsage();
System.exit(1);
}
try {
String command = args[0];
if ("skipgram".equalsIgnoreCase(command) || "cbow".equalsIgnoreCase(command)
|| "supervised".equalsIgnoreCase(command)) {
op.train(args);
} else if ("test".equalsIgnoreCase(command)) {
op.test(args);
} else if ("print-vectors".equalsIgnoreCase(command)) {
op.printVectors(args);
} else if ("predict".equalsIgnoreCase(command) || "predict-prob".equalsIgnoreCase(command)) {
op.predict(args);
} else {
printUsage();
System.exit(1);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.exit(0);
}
}