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/suppression_dictionary.h"
31 
32 #include "base/logging.h"
33 #include "base/mutex.h"
34 
35 namespace mozc {
36 namespace dictionary {
37 namespace {
38 const char kDelimiter = '\t';
39 }  // namespace
40 
SuppressionDictionary()41 SuppressionDictionary::SuppressionDictionary()
42     : locked_(false), has_key_empty_(false), has_value_empty_(false) {}
43 
~SuppressionDictionary()44 SuppressionDictionary::~SuppressionDictionary() {}
45 
AddEntry(const string & key,const string & value)46 bool SuppressionDictionary::AddEntry(
47     const string &key, const string &value) {
48   if (!locked_) {
49     LOG(ERROR) << "Dictionary is not locked";
50     return false;
51   }
52 
53   if (key.empty() && value.empty()) {
54     LOG(WARNING) << "Both key and value are empty";
55     return false;
56   }
57 
58   if (key.empty()) {
59     has_key_empty_ = true;
60   }
61 
62   if (value.empty()) {
63     has_value_empty_ = true;
64   }
65 
66   dic_.insert(key + kDelimiter + value);
67 
68   return true;
69 }
70 
Clear()71 void SuppressionDictionary::Clear() {
72   if (!locked_) {
73     LOG(ERROR) << "Dictionary is not locked";
74     return;
75   }
76   has_key_empty_ = false;
77   has_value_empty_ = false;
78   dic_.clear();
79 }
80 
Lock()81 void SuppressionDictionary::Lock() {
82   scoped_lock l(&mutex_);
83   locked_ = true;
84 }
85 
UnLock()86 void SuppressionDictionary::UnLock() {
87   scoped_lock l(&mutex_);
88   locked_ = false;
89 }
90 
IsEmpty() const91 bool SuppressionDictionary::IsEmpty() const {
92   if (locked_) {
93     return true;
94   }
95   return dic_.empty();
96 }
97 
SuppressEntry(const string & key,const string & value) const98 bool SuppressionDictionary::SuppressEntry(
99     const string &key, const string &value) const {
100   if (dic_.empty()) {
101     // Almost all users don't use word supresssion function.
102     // We can return false as early as possible
103     return false;
104   }
105 
106   if (locked_) {
107     VLOG(2) << "Dictionary is locked now";
108     return false;
109   }
110 
111   string lookup_key = key;
112   lookup_key.append(1, kDelimiter).append(value);
113   if (dic_.find(lookup_key) != dic_.end()) {
114     return true;
115   }
116 
117   if (has_key_empty_) {
118     lookup_key.assign(1, kDelimiter).append(value);
119     if (dic_.find(lookup_key) != dic_.end()) {
120       return true;
121     }
122   }
123 
124   if (has_value_empty_) {
125     lookup_key.assign(key).append(1, kDelimiter);
126     if (dic_.find(lookup_key) != dic_.end()) {
127       return true;
128     }
129   }
130 
131   return false;
132 }
133 
134 }  // namespace dictionary
135 }  // namespace mozc
136