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_USER_DICTIONARY_H_ 31 #define MOZC_DICTIONARY_USER_DICTIONARY_H_ 32 33 #include <memory> 34 #include <string> 35 #include <vector> 36 37 #include "base/port.h" 38 #include "base/string_piece.h" 39 #include "dictionary/dictionary_interface.h" 40 #include "dictionary/pos_matcher.h" 41 #include "dictionary/suppression_dictionary.h" 42 #include "dictionary/user_pos_interface.h" 43 #include "protocol/user_dictionary_storage.pb.h" 44 45 namespace mozc { 46 47 class ReaderWriterMutex; 48 49 namespace dictionary { 50 51 class UserDictionary : public DictionaryInterface { 52 public: 53 UserDictionary(const UserPOSInterface *user_pos, 54 POSMatcher pos_matcher, 55 SuppressionDictionary *suppression_dictionary); 56 ~UserDictionary() override; 57 58 bool HasKey(StringPiece key) const override; 59 bool HasValue(StringPiece value) const override; 60 61 // Lookup methods don't support kana modifier insensitive lookup, i.e., 62 // Callback::OnActualKey() is never called. 63 void LookupPredictive(StringPiece key, 64 const ConversionRequest &conversion_request, 65 Callback *callback) const override; 66 void LookupPrefix(StringPiece key, 67 const ConversionRequest &conversion_request, 68 Callback *callback) const override; 69 void LookupExact(StringPiece key, 70 const ConversionRequest &conversion_request, 71 Callback *callback) const override; 72 void LookupReverse(StringPiece str, 73 const ConversionRequest &conversion_request, 74 Callback *callback) const override; 75 76 // Looks up a user comment from a pair of key and value. When (key, value) 77 // doesn't exist in this dictionary or user comment is empty, bool is 78 // returned and string is kept as-is. 79 bool LookupComment(StringPiece key, StringPiece value, 80 const ConversionRequest &conversion_request, 81 string *comment) const override; 82 83 // Loads dictionary from UserDictionaryStorage. 84 // mainly for unittesting 85 bool Load(const user_dictionary::UserDictionaryStorage &storage); 86 87 // Reloads dictionary asynchronously 88 bool Reload() override; 89 90 // Waits until reloader finishes 91 void WaitForReloader(); 92 93 // Adds new word to auto registered dictionary and reload asynchronously. 94 // Note that this method will not guarantee that 95 // new word is added successfully, since the actual 96 // dictionary modification is done by other thread. 97 // Also, this method should be called by the main converter thread which 98 // is executed synchronously with user input. 99 bool AddToAutoRegisteredDictionary( 100 const string &key, const string &value, 101 const ConversionRequest &conversion_request, 102 user_dictionary::UserDictionary::PosType pos); 103 104 // Sets user dicitonary filename for unittesting 105 static void SetUserDictionaryName(const string &filename); 106 107 private: 108 class TokensIndex; 109 class UserDictionaryReloader; 110 111 // Swaps internal tokens index to |new_tokens|. 112 void Swap(TokensIndex *new_tokens); 113 114 std::unique_ptr<UserDictionaryReloader> reloader_; 115 std::unique_ptr<const UserPOSInterface> user_pos_; 116 const POSMatcher pos_matcher_; 117 SuppressionDictionary *suppression_dictionary_; 118 TokensIndex *tokens_; 119 mutable std::unique_ptr<ReaderWriterMutex> mutex_; 120 121 friend class UserDictionaryTest; 122 DISALLOW_COPY_AND_ASSIGN(UserDictionary); 123 }; 124 125 } // namespace dictionary 126 } // namespace mozc 127 128 #endif // MOZC_DICTIONARY_USER_DICTIONARY_H_ 129