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 // Classes managing dictionary entries.
31 
32 #ifndef MOZC_REWRITER_DICTIONARY_GENERATOR_H_
33 #define MOZC_REWRITER_DICTIONARY_GENERATOR_H_
34 
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <vector>
39 
40 #include "base/port.h"
41 #include "data_manager/data_manager_interface.h"
42 
43 namespace mozc {
44 
45 template <class T> class ObjectPool;
46 class UserPOSInterface;
47 
48 namespace rewriter {
49 
50 class Token {
51  public:
52   Token();
53   virtual ~Token();
54 
55   // Merge the values of the token.
56   void MergeFrom(const Token &token);
57 
58   // Return the fingerprint of the keys (key, value and pos).
59   uint64 GetID() const;
60 
61   // Accessors
sorting_key()62   int sorting_key() const { return sorting_key_; }
set_sorting_key(int sorting_key)63   void set_sorting_key(int sorting_key) { sorting_key_ = sorting_key; }
64 
key()65   const string &key() const { return key_; }
set_key(const string & key)66   void set_key(const string &key) { key_ = key; }
67 
value()68   const string &value() const { return value_; }
set_value(const string & value)69   void set_value(const string &value) { value_ = value; }
70 
pos()71   const string &pos() const { return pos_; }
set_pos(const string & pos)72   void set_pos(const string &pos) { pos_ = pos; }
73 
description()74   const string &description() const {
75     return description_;
76   }
additional_description()77   const string &additional_description() const {
78     return additional_description_;
79   }
set_description(const string & description)80   void set_description(const string &description) {
81     description_ = description;
82   }
set_additional_description(const string & additional_description)83   void set_additional_description(const string &additional_description) {
84     additional_description_ = additional_description;
85   }
86 
87  private:
88   int sorting_key_;
89   string key_;
90   string value_;
91   string pos_;
92   string description_;
93   string additional_description_;
94   // NOTE(komatsu): When new arguments are added, MergeFrom function
95   // should be updated too.
96 
97   DISALLOW_COPY_AND_ASSIGN(Token);
98 };
99 
100 
101 class DictionaryGenerator {
102  public:
103   explicit DictionaryGenerator(const DataManagerInterface &data_manager);
104   virtual ~DictionaryGenerator();
105 
106   // Add the token into the pool.
107   void AddToken(const Token &token);
108 
109   // Output the tokens into the filename.
110   bool Output(const string &filename) const;
111 
112  private:
113   std::unique_ptr<ObjectPool<Token>> token_pool_;
114   std::unique_ptr<std::map<uint64, Token *>> token_map_;
115   std::unique_ptr<const UserPOSInterface> user_pos_;
116   uint16 open_bracket_id_;
117   uint16 close_bracket_id_;
118 
119   DISALLOW_COPY_AND_ASSIGN(DictionaryGenerator);
120 };
121 
122 }  // namespace rewriter
123 }  // namespace mozc
124 
125 #endif  // MOZC_REWRITER_DICTIONARY_GENERATOR_H_
126