in src/main.cc [212:250]
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);
}