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 #include "dictionary/suffix_dictionary.h"
31 
32 #include <algorithm>
33 #include <string>
34 
35 #include "base/logging.h"
36 #include "base/serialized_string_array.h"
37 #include "base/util.h"
38 #include "dictionary/dictionary_token.h"
39 
40 namespace mozc {
41 namespace dictionary {
42 namespace {
43 
44 class ComparePrefix {
45  public:
ComparePrefix(size_t max_len)46   explicit ComparePrefix(size_t max_len) : max_len_(max_len) {}
47 
operator ()(StringPiece x,StringPiece y) const48   bool operator()(StringPiece x, StringPiece y) const {
49     return x.substr(0, max_len_) < y.substr(0, max_len_);
50   }
51 
52  private:
53   const size_t max_len_;
54 };
55 
56 }  // namespace
57 
SuffixDictionary(StringPiece key_array_data,StringPiece value_array_data,const uint32 * token_array)58 SuffixDictionary::SuffixDictionary(StringPiece key_array_data,
59                                    StringPiece value_array_data,
60                                    const uint32 *token_array)
61     : token_array_(token_array) {
62   DCHECK(SerializedStringArray::VerifyData(key_array_data));
63   DCHECK(SerializedStringArray::VerifyData(value_array_data));
64   DCHECK(token_array_);
65   key_array_.Set(key_array_data);
66   value_array_.Set(value_array_data);
67 }
68 
~SuffixDictionary()69 SuffixDictionary::~SuffixDictionary() {}
70 
HasKey(StringPiece key) const71 bool SuffixDictionary::HasKey(StringPiece key) const {
72   // SuffixDictionary::HasKey() is never called and unnecessary to
73   // implement. To avoid accidental calls of this method, the method simply dies
74   // so that we can immediately notice this unimplemented method during
75   // development.
76   LOG(FATAL) << "bool SuffixDictionary::HasKey() is not implemented";
77   return false;
78 }
79 
HasValue(StringPiece value) const80 bool SuffixDictionary::HasValue(StringPiece value) const {
81   // SuffixDictionary::HasValue() is never called and unnecessary to
82   // implement. To avoid accidental calls of this method, the method simply dies
83   // so that we can immediately notice this unimplemented method during
84   // development.
85   LOG(FATAL) << "bool SuffixDictionary::HasValue() is not implemented";
86   return false;
87 }
88 
LookupPredictive(StringPiece key,const ConversionRequest & conversion_request,Callback * callback) const89 void SuffixDictionary::LookupPredictive(
90     StringPiece key,
91     const ConversionRequest &conversion_request,
92     Callback *callback) const {
93   using Iter = SerializedStringArray::const_iterator;
94   std::pair<Iter, Iter> range = std::equal_range(key_array_.begin(),
95                                             key_array_.end(),
96                                             key, ComparePrefix(key.size()));
97   Token token;
98   token.attributes = Token::NONE;  // Common for all suffix tokens.
99   for (; range.first != range.second; ++range.first) {
100     token.key.assign((*range.first).data(), (*range.first).size());
101     switch (callback->OnKey(token.key)) {
102       case Callback::TRAVERSE_DONE:
103         return;
104       case Callback::TRAVERSE_NEXT_KEY:
105         continue;
106       case Callback::TRAVERSE_CULL:
107         LOG(FATAL) << "Culling is not supported.";
108         continue;
109       default:
110         break;
111     }
112     const size_t index = range.first - key_array_.begin();
113     if (value_array_[index].empty()) {
114       token.value = token.key;
115     } else {
116       token.value.assign(value_array_[index].data(),
117                          value_array_[index].size());
118     }
119     token.lid = token_array_[3 * index];
120     token.rid = token_array_[3 * index + 1];
121     token.cost = token_array_[3 * index + 2];
122     if (callback->OnToken(token.key, token.key, token) !=
123         Callback::TRAVERSE_CONTINUE) {
124       break;
125     }
126   }
127 }
128 
LookupPrefix(StringPiece key,const ConversionRequest & conversion_request,Callback * callback) const129 void SuffixDictionary::LookupPrefix(
130     StringPiece key,
131     const ConversionRequest &conversion_request,
132     Callback *callback) const {
133 }
134 
LookupExact(StringPiece key,const ConversionRequest & conversion_request,Callback * callback) const135 void SuffixDictionary::LookupExact(
136     StringPiece key,
137     const ConversionRequest &conversion_request,
138     Callback *callback) const {
139 }
140 
LookupReverse(StringPiece key,const ConversionRequest & conversion_request,Callback * callback) const141 void SuffixDictionary::LookupReverse(
142     StringPiece key,
143     const ConversionRequest &conversion_request,
144     Callback *callback) const {
145 }
146 
147 }  // namespace dictionary
148 }  // namespace mozc
149