1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chromeos/components/local_search_service/test_utils.h"
6 
7 #include <cmath>
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 namespace chromeos {
14 namespace local_search_service {
15 
16 namespace {
17 
18 // (content-id, content).
19 using ContentWithId = std::pair<std::string, std::string>;
20 
21 // (content-id, content, weight).
22 using WeightedContentWithId = std::tuple<std::string, std::string, float>;
23 
24 }  // namespace
25 
CreateTestData(const std::map<std::string,std::vector<ContentWithId>> & input)26 std::vector<Data> CreateTestData(
27     const std::map<std::string, std::vector<ContentWithId>>& input) {
28   std::vector<Data> output;
29   for (const auto& item : input) {
30     Data data;
31     data.id = item.first;
32     // Hardcode to "en" because it's unclear what config locale will be when
33     // running a test.
34     // TODO(jiameng): allow locale to be passed in if there's a need to use
35     // non-en data in tests.
36     data.locale = "en";
37     std::vector<Content>& contents = data.contents;
38     for (const auto& content_with_id : item.second) {
39       const Content content(content_with_id.first,
40                             base::UTF8ToUTF16(content_with_id.second));
41       contents.push_back(content);
42     }
43     output.push_back(data);
44   }
45   return output;
46 }
47 
CreateTestData(const std::map<std::string,std::vector<std::tuple<std::string,std::string,float>>> & input)48 std::vector<Data> CreateTestData(
49     const std::map<std::string,
50                    std::vector<std::tuple<std::string, std::string, float>>>&
51         input) {
52   std::vector<Data> output;
53   for (const auto& item : input) {
54     Data data;
55     data.id = item.first;
56     // Hardcode to "en" because it's unclear what config locale will be when
57     // running a test.
58     // TODO(jiameng): allow locale to be passed in if there's a need to use
59     // non-en data in tests.
60     data.locale = "en";
61     std::vector<Content>& contents = data.contents;
62     for (const auto& weighted_content_with_id : item.second) {
63       const Content content(
64           std::get<0>(weighted_content_with_id),
65           base::UTF8ToUTF16(std::get<1>(weighted_content_with_id)),
66           std::get<2>(weighted_content_with_id));
67       contents.push_back(content);
68     }
69     output.push_back(data);
70   }
71   return output;
72 }
73 
CheckResult(const Result & result,const std::string & expected_id,float expected_score,size_t expected_number_positions)74 void CheckResult(const Result& result,
75                  const std::string& expected_id,
76                  float expected_score,
77                  size_t expected_number_positions) {
78   EXPECT_EQ(result.id, expected_id);
79   EXPECT_NEAR(result.score, expected_score, 0.001);
80   EXPECT_EQ(result.positions.size(), expected_number_positions);
81 }
82 
TfIdfScore(size_t num_docs,size_t num_docs_with_term,float weighted_num_term_occurrence_in_doc,size_t doc_length)83 float TfIdfScore(size_t num_docs,
84                  size_t num_docs_with_term,
85                  float weighted_num_term_occurrence_in_doc,
86                  size_t doc_length) {
87   const float idf = 1.0 + log((1.0 + num_docs) / (1.0 + num_docs_with_term));
88 
89   const float tf = weighted_num_term_occurrence_in_doc / doc_length;
90   return tf * idf;
91 }
92 
93 }  // namespace local_search_service
94 }  // namespace chromeos
95