| 36 | // ---------------------------------------------------------------------------------------- |
| 37 | |
| 38 | void test_defaults() |
| 39 | { |
| 40 | tfidf_transformer<sparse_type> tfidf; |
| 41 | DLIB_TEST(tfidf.vocabulary_size() == 0); |
| 42 | DLIB_TEST(tfidf.min_document_frequency() == 1); |
| 43 | DLIB_TEST(tfidf.uses_smooth_idf()); |
| 44 | DLIB_TEST(!tfidf.uses_sublinear_tf()); |
| 45 | DLIB_TEST(tfidf.normalizes_to_unit_length()); |
| 46 | |
| 47 | tfidf.train(corpus()); |
| 48 | DLIB_TEST(tfidf.vocabulary() == std::vector<unsigned long>({0,1,2,3})); |
| 49 | // smooth idf: log((1+n)/(1+df)) + 1 with n == 3 |
| 50 | DLIB_TEST(near(tfidf.idf()[0], std::log(2.0) + 1)); |
| 51 | DLIB_TEST(near(tfidf.idf()[1], 1)); |
| 52 | DLIB_TEST(near(tfidf.idf()[2], std::log(2.0) + 1)); |
| 53 | DLIB_TEST(near(tfidf.idf()[3], std::log(2.0) + 1)); |
| 54 | |
| 55 | const sparse_type r = tfidf(corpus()[0]); |
| 56 | const double a = std::log(2.0) + 1; |
| 57 | const double b = 2; |
| 58 | const double norm = std::sqrt(a*a + b*b); |
| 59 | DLIB_TEST(r.size() == 2); |
| 60 | DLIB_TEST(r[0].first == 0 && near(r[0].second, a/norm)); |
| 61 | DLIB_TEST(r[1].first == 1 && near(r[1].second, b/norm)); |
| 62 | DLIB_TEST(near(length(r), 1)); |
| 63 | } |
| 64 | |
| 65 | // ---------------------------------------------------------------------------------------- |
| 66 |
no test coverage detected