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_COMPOSER_INTERNAL_TYPING_MODEL_H_
31 #define MOZC_COMPOSER_INTERNAL_TYPING_MODEL_H_
32 
33 #include <memory>
34 
35 #include "base/port.h"
36 #include "base/string_piece.h"
37 #include "data_manager/data_manager_interface.h"
38 #include "protocol/commands.pb.h"
39 // for FRIEND_TEST()
40 #include "testing/base/public/gunit_prod.h"
41 
42 
43 namespace mozc {
44 namespace composer {
45 
46 // TypingModel, with key-value like interface.
47 // The only exported interface is GetCost, which takes key
48 // and returns cost value.
49 // The key-value data source should be provided via the constructor.
50 // Typically the constructor taks data generated by gen_typing_model.py.
51 class TypingModel {
52  public:
53   TypingModel(const char *characters, size_t characters_size,
54               const uint8 *cost_table, size_t cost_table_size,
55               const int32 *mapping_table);
56 
57   virtual ~TypingModel();
58 
59   // Gets cost value from key.
60   // virtual for mocking.
61   virtual int GetCost(StringPiece key) const;
62 
63   // Creates a TypingModel based on SpecialRomanjiTable.
64   // nullptr if no corresponding model is available.
65   static std::unique_ptr<const TypingModel> CreateTypingModel(
66       const mozc::commands::Request::SpecialRomanjiTable &special_romanji_table,
67       const DataManagerInterface& data_manager);
68 
69   // No data means its const is infinity.
70   static const int kInfinity;
71 
72   // In typing model, meaning no corresponding data.
73   static const uint8 kNoData;
74 
75  private:
76   FRIEND_TEST(TypingModelTest, Constructor);
77   FRIEND_TEST(TypingModelTest, GetIndex);
78 
79   // Gets index from key.
80   // Given cost table via the constructor is simple array
81   // so when accessing to cost value, index should be
82   // calculated by this method.
83   // c.f. gen_typing_model.py's GetIndexFromKey.
84   size_t GetIndex(StringPiece key) const;
85 
86   // Radix table, needed by GetIndex.
87   std::unique_ptr<unsigned char[]> character_to_radix_table_;
88   const size_t characters_size_;
89   const uint8 *cost_table_;
90   const size_t cost_table_size_;
91   const int32 *mapping_table_;
92 };
93 
94 }  // namespace composer
95 }  // namespace mozc
96 
97 #endif  // MOZC_COMPOSER_INTERNAL_TYPING_MODEL_H_
98