1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef MOZC_CONVERTER_QUALITY_REGRESSION_UTIL_H_
31 #define MOZC_CONVERTER_QUALITY_REGRESSION_UTIL_H_
32 
33 #include <memory>
34 #include <string>
35 #include <vector>
36 
37 #include "base/port.h"
38 #include "protocol/config.pb.h"
39 
40 namespace mozc {
41 class Segments;
42 class ConverterInterface;
43 
44 namespace commands {
45 class Request;
46 }  // namespace commands
47 
48 namespace quality_regression {
49 
50 class QualityRegressionUtil {
51  public:
52   // bit fields
53   enum Platform {
54     DESKTOP = 1,
55     OSS = 2,
56     MOBILE = 4,
57     MOBILE_AMBIGUOUS = 8,
58     CHROMEOS = 16,
59   };
60 
61   struct TestItem {
62     string label;
63     string key;
64     string expected_value;
65     string command;
66     int    expected_rank;
67     double accuracy;
68     // Target platform. Can set multiple platform defined in enum |Platform|.
69     uint32 platform;
70     string OutputAsTSV() const;
71     bool ParseFromTSV(const string &tsv_line);
72   };
73 
74   explicit QualityRegressionUtil(ConverterInterface *converter);
75   virtual ~QualityRegressionUtil();
76 
77   // Pase |filename| and save the all test items into |outputs|.
78   static bool ParseFile(const string &filename,
79                         std::vector<TestItem> *outputs);
80 
81   bool ConvertAndTest(const TestItem &item,
82                       string *actual_value);
83 
84   void SetRequest(const commands::Request &request);
85   void SetConfig(const config::Config &config);
86   static string GetPlatformString(uint32 platform_bitfiled);
87 
88  private:
89   ConverterInterface *converter_;
90   std::unique_ptr<commands::Request> request_;
91   std::unique_ptr<config::Config> config_;
92   std::unique_ptr<Segments> segments_;
93 
94   DISALLOW_COPY_AND_ASSIGN(QualityRegressionUtil);
95 };
96 
97 }  // namespace quality_regression
98 }  // namespace mozc
99 
100 #endif  // MOZC_CONVERTER_QUALITY_REGRESSION_UTIL_H_
101