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 "rewriter/correction_rewriter.h"
31
32 #include <algorithm>
33 #include <string>
34 #include <vector>
35
36 #include "base/logging.h"
37 #include "base/util.h"
38 #include "config/config_handler.h"
39 #include "converter/segments.h"
40 #include "data_manager/data_manager_interface.h"
41 #include "protocol/config.pb.h"
42 #include "request/conversion_request.h"
43
44 namespace mozc {
45
SetCandidate(const ReadingCorrectionItem & item,Segment::Candidate * candidate)46 void CorrectionRewriter::SetCandidate(const ReadingCorrectionItem &item,
47 Segment::Candidate *candidate) {
48 candidate->prefix = "→ ";
49 candidate->attributes |= Segment::Candidate::SPELLING_CORRECTION;
50
51 candidate->description = "<もしかして: ";
52 candidate->description.append(item.correction.data(), item.correction.size());
53 candidate->description.append(1, '>');
54
55 DCHECK(candidate->IsValid());
56 }
57
LookupCorrection(const string & key,const string & value,std::vector<ReadingCorrectionItem> * results) const58 bool CorrectionRewriter::LookupCorrection(
59 const string &key,
60 const string &value,
61 std::vector<ReadingCorrectionItem> *results) const {
62 CHECK(results);
63 results->clear();
64
65 using Iter = SerializedStringArray::const_iterator;
66 std::pair<Iter, Iter> range = std::equal_range(error_array_.begin(),
67 error_array_.end(),
68 key);
69 for (; range.first != range.second; ++range.first) {
70 const StringPiece v = value_array_[range.first.index()];
71 if (value.empty() || value == v) {
72 results->emplace_back(v, *range.first,
73 correction_array_[range.first.index()]);
74 }
75 }
76 return !results->empty();
77 }
78
CorrectionRewriter(StringPiece value_array_data,StringPiece error_array_data,StringPiece correction_array_data)79 CorrectionRewriter::CorrectionRewriter(StringPiece value_array_data,
80 StringPiece error_array_data,
81 StringPiece correction_array_data) {
82 DCHECK(SerializedStringArray::VerifyData(value_array_data));
83 DCHECK(SerializedStringArray::VerifyData(error_array_data));
84 DCHECK(SerializedStringArray::VerifyData(correction_array_data));
85 value_array_.Set(value_array_data);
86 error_array_.Set(error_array_data);
87 correction_array_.Set(correction_array_data);
88 DCHECK_EQ(value_array_.size(), error_array_.size());
89 DCHECK_EQ(value_array_.size(), correction_array_.size());
90 }
91
92 // static
CreateCorrectionRewriter(const DataManagerInterface * data_manager)93 CorrectionRewriter *CorrectionRewriter::CreateCorrectionRewriter(
94 const DataManagerInterface *data_manager) {
95 StringPiece value_array_data, error_array_data, correction_array_data;
96 data_manager->GetReadingCorrectionData(&value_array_data,
97 &error_array_data,
98 &correction_array_data);
99 return new CorrectionRewriter(value_array_data, error_array_data,
100 correction_array_data);
101 }
102
103 CorrectionRewriter::~CorrectionRewriter() = default;
104
Rewrite(const ConversionRequest & request,Segments * segments) const105 bool CorrectionRewriter::Rewrite(const ConversionRequest &request,
106 Segments *segments) const {
107 if (!request.config().use_spelling_correction()) {
108 return false;
109 }
110
111 bool modified = false;
112 std::vector<ReadingCorrectionItem> results;
113
114 for (size_t i = 0; i < segments->conversion_segments_size(); ++i) {
115 Segment *segment = segments->mutable_conversion_segment(i);
116 DCHECK(segment);
117 if (segment->candidates_size() == 0) {
118 continue;
119 }
120
121 for (size_t j = 0; j < segment->candidates_size(); ++j) {
122 const Segment::Candidate &candidate = segment->candidate(j);
123 if (!LookupCorrection(candidate.content_key, candidate.content_value,
124 &results)) {
125 continue;
126 }
127 CHECK_GT(results.size(), 0);
128 // results.size() should be 1, but we don't check it here.
129 Segment::Candidate *mutable_candidate = segment->mutable_candidate(j);
130 DCHECK(mutable_candidate);
131 SetCandidate(results[0], mutable_candidate);
132 modified = true;
133 }
134
135 // TODO(taku): Want to calculate the position more accurately by
136 // taking the emission cost into consideration.
137 // The cost of mis-reading candidate can simply be obtained by adding
138 // some constant penalty to the original emission cost.
139 //
140 // TODO(taku): In order to provide all miss reading corrections
141 // defined in the tsv file, we want to add miss-read entries to
142 // the system dictionary.
143 const size_t kInsertPosition =
144 std::min(static_cast<size_t>(3), segment->candidates_size());
145 const Segment::Candidate &top_candidate = segment->candidate(0);
146 if (!LookupCorrection(top_candidate.content_key, "", &results)) {
147 continue;
148 }
149 for (size_t k = 0; k < results.size(); ++k) {
150 Segment::Candidate *mutable_candidate =
151 segment->insert_candidate(kInsertPosition);
152 DCHECK(mutable_candidate);
153 mutable_candidate->CopyFrom(top_candidate);
154 Util::ConcatStrings(results[k].error,
155 top_candidate.functional_key(),
156 &mutable_candidate->key);
157 Util::ConcatStrings(results[k].value,
158 top_candidate.functional_value(),
159 &mutable_candidate->value);
160 mutable_candidate->inner_segment_boundary.clear();
161 SetCandidate(results[k], mutable_candidate);
162 modified = true;
163 }
164 }
165
166 return modified;
167 }
168 } // namespace mozc
169