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_DICTIONARY_DICTIONARY_IMPL_H_
31 #define MOZC_DICTIONARY_DICTIONARY_IMPL_H_
32 
33 #include <memory>
34 #include <vector>
35 
36 #include "base/port.h"
37 #include "base/string_piece.h"
38 #include "dictionary/dictionary_interface.h"
39 #include "dictionary/pos_matcher.h"
40 #include "dictionary/suppression_dictionary.h"
41 
42 namespace mozc {
43 namespace dictionary {
44 
45 class DictionaryImpl : public DictionaryInterface {
46  public:
47   // Initializes a dictionary with given dictionaries and POS data.  The system
48   // and value dictionaries are owned by this class but the user dictionary is
49   // just a reference and to be deleted by the caller. Note that the user
50   // dictionary is not a const reference because this class may reload the user
51   // dictionary.
52   // TODO(noriyukit): Currently DictionaryInterface::Reload() is not used and
53   // thus user_dictionary can be const as well. We can make it const after
54   // clarifying the ownership of the user dictionary and changing code so that
55   // the owner reloads it.
56   DictionaryImpl(const DictionaryInterface *system_dictionary,
57                  const DictionaryInterface *value_dictionary,
58                  DictionaryInterface *user_dictionary,
59                  const SuppressionDictionary *suppression_dictionary,
60                  const POSMatcher *pos_matcher);
61 
62   virtual ~DictionaryImpl();
63 
64   virtual bool HasKey(StringPiece key) const;
65   virtual bool HasValue(StringPiece value) const;
66   virtual void LookupPredictive(StringPiece key,
67                                 const ConversionRequest &conversion_request,
68                                 Callback *callback) const;
69   virtual void LookupPrefix(StringPiece key,
70                             const ConversionRequest &conversion_request,
71                             Callback *callback) const;
72 
73   virtual void LookupExact(StringPiece key,
74                            const ConversionRequest &conversion_request,
75                            Callback *callback) const;
76 
77   virtual void LookupReverse(StringPiece str,
78                              const ConversionRequest &conversion_request,
79                              Callback *callback) const;
80 
81   virtual bool LookupComment(StringPiece key, StringPiece value,
82                              const ConversionRequest &conversion_request,
83                              string *comment) const;
84   virtual bool Reload();
85   virtual void PopulateReverseLookupCache(StringPiece str) const;
86   virtual void ClearReverseLookupCache() const;
87 
88  private:
89   enum LookupType {
90     PREDICTIVE,
91     PREFIX,
92     REVERSE,
93     EXACT,
94   };
95 
96   // Used to check POS IDs.
97   const POSMatcher *pos_matcher_;
98 
99   // Main three dictionaries.
100   std::unique_ptr<const DictionaryInterface> system_dictionary_;
101   std::unique_ptr<const DictionaryInterface> value_dictionary_;
102   DictionaryInterface *user_dictionary_;
103 
104   // Convenient container to handle the above three dictionaries as one
105   // composite dictionary.
106   std::vector<const DictionaryInterface *> dics_;
107 
108   // Suppression dictionary is used to suppress entries.
109   const SuppressionDictionary *suppression_dictionary_;
110 
111   DISALLOW_COPY_AND_ASSIGN(DictionaryImpl);
112 };
113 
114 }  // namespace dictionary
115 }  // namespace mozc
116 
117 #endif  // MOZC_DICTIONARY_DICTIONARY_IMPL_H_
118