Skip to content

Commit 9210895

Browse files
cpuhrschfacebook-github-bot
authored andcommitted
remove cerr/cout from fasttext test
Summary: See title. Reviewed By: EdouardGrave Differential Revision: D6621580 fbshipit-source-id: 271e9c3c2bc01bb6904c6b7ef829c2574569a6d9
1 parent fce60af commit 9210895

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/fasttext.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ void FastText::skipgram(Model& model, real lr,
360360
}
361361
}
362362

363-
void FastText::test(std::istream& in, int32_t k) {
363+
std::tuple<int64_t, double, double> FastText::test(
364+
std::istream& in,
365+
int32_t k) {
364366
int32_t nexamples = 0, nlabels = 0;
365367
double precision = 0.0;
366368
std::vector<int32_t> line, labels;
@@ -379,11 +381,8 @@ void FastText::test(std::istream& in, int32_t k) {
379381
nlabels += labels.size();
380382
}
381383
}
382-
std::cout << "N" << "\t" << nexamples << std::endl;
383-
std::cout << std::setprecision(3);
384-
std::cout << "P@" << k << "\t" << precision / (k * nexamples) << std::endl;
385-
std::cout << "R@" << k << "\t" << precision / nlabels << std::endl;
386-
std::cerr << "Number of examples: " << nexamples << std::endl;
384+
return std::tuple<int64_t, double, double>(
385+
nexamples, precision / (k * nexamples), precision / nlabels);
387386
}
388387

389388
void FastText::predict(std::istream& in, int32_t k,

src/fasttext.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <set>
1717
#include <chrono>
1818
#include <iostream>
19+
#include <queue>
20+
#include <tuple>
1921

2022
#include "args.h"
2123
#include "dictionary.h"
@@ -91,7 +93,7 @@ class FastText {
9193
std::vector<int32_t> selectEmbeddings(int32_t) const;
9294
void getSentenceVector(std::istream&, Vector&);
9395
void quantize(const Args);
94-
void test(std::istream&, int32_t);
96+
std::tuple<int64_t, double, double> test(std::istream&, int32_t);
9597
void predict(std::istream&, int32_t, bool);
9698
void predict(
9799
std::istream&,

src/main.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99

1010
#include <iostream>
11+
#include <queue>
12+
#include <iomanip>
1113
#include "fasttext.h"
1214
#include "args.h"
1315

@@ -132,19 +134,24 @@ void test(const std::vector<std::string>& args) {
132134
FastText fasttext;
133135
fasttext.loadModel(args[2]);
134136

137+
std::tuple<int64_t, double, double> result;
135138
std::string infile = args[3];
136139
if (infile == "-") {
137-
fasttext.test(std::cin, k);
140+
result = fasttext.test(std::cin, k);
138141
} else {
139142
std::ifstream ifs(infile);
140143
if (!ifs.is_open()) {
141144
std::cerr << "Test file cannot be opened!" << std::endl;
142145
exit(EXIT_FAILURE);
143146
}
144-
fasttext.test(ifs, k);
147+
result = fasttext.test(ifs, k);
145148
ifs.close();
146149
}
147-
exit(0);
150+
std::cout << "N" << "\t" << std::get<0>(result) << std::endl;
151+
std::cout << std::setprecision(3);
152+
std::cout << "P@" << k << "\t" << std::get<1>(result) << std::endl;
153+
std::cout << "R@" << k << "\t" << std::get<2>(result) << std::endl;
154+
std::cerr << "Number of examples: " << std::get<0>(result) << std::endl;
148155
}
149156

150157
void predict(const std::vector<std::string>& args) {

0 commit comments

Comments
 (0)