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 <memory>
33 #include <string>
34
35 #include "base/port.h"
36 #include "base/serialized_string_array.h"
37 #include "config/config_handler.h"
38 #include "converter/segments.h"
39 #include "protocol/commands.pb.h"
40 #include "protocol/config.pb.h"
41 #include "request/conversion_request.h"
42 #include "testing/base/public/gunit.h"
43
44 namespace mozc {
45 namespace {
46
AddSegment(const string & key,Segments * segments)47 Segment *AddSegment(const string &key, Segments *segments) {
48 Segment *segment = segments->push_back_segment();
49 segment->set_key(key);
50 return segment;
51 }
52
AddCandidate(const string & key,const string & value,const string & content_key,const string & content_value,Segment * segment)53 Segment::Candidate *AddCandidate(
54 const string &key, const string &value,
55 const string &content_key, const string &content_value,
56 Segment *segment) {
57 Segment::Candidate *candidate = segment->add_candidate();
58 candidate->Init();
59 candidate->key = key;
60 candidate->value = value;
61 candidate->content_key = content_key;
62 candidate->content_value = content_value;
63 return candidate;
64 }
65 } // namespace
66
67 class CorrectionRewriterTest : public testing::Test {
68 protected:
CorrectionRewriterTest()69 CorrectionRewriterTest() {
70 convreq_.set_request(&request_);
71 convreq_.set_config(&config_);
72 }
73
SetUp()74 void SetUp() override {
75 // Create a rewriter with one entry: (TSUKIGIME, gekkyoku, tsukigime)
76 const std::vector<StringPiece> values = {"TSUKIGIME"};
77 const std::vector<StringPiece> errors = {"gekkyoku"};
78 const std::vector<StringPiece> corrections = {"tsukigime"};
79 rewriter_.reset(new CorrectionRewriter(
80 SerializedStringArray::SerializeToBuffer(values, &values_buf_),
81 SerializedStringArray::SerializeToBuffer(errors, &errors_buf_),
82 SerializedStringArray::SerializeToBuffer(corrections,
83 &corrections_buf_)));
84 config::ConfigHandler::GetDefaultConfig(&config_);
85 config_.set_use_spelling_correction(true);
86 }
87
88 std::unique_ptr<CorrectionRewriter> rewriter_;
89 ConversionRequest convreq_;
90 commands::Request request_;
91 config::Config config_;
92
93 private:
94 std::unique_ptr<uint32[]> values_buf_;
95 std::unique_ptr<uint32[]> errors_buf_;
96 std::unique_ptr<uint32[]> corrections_buf_;
97 };
98
TEST_F(CorrectionRewriterTest,CapabilityTest)99 TEST_F(CorrectionRewriterTest, CapabilityTest) {
100 EXPECT_EQ(RewriterInterface::ALL, rewriter_->capability(convreq_));
101 }
102
TEST_F(CorrectionRewriterTest,RewriteTest)103 TEST_F(CorrectionRewriterTest, RewriteTest) {
104 Segments segments;
105
106 Segment *segment = AddSegment("gekkyokuwo", &segments);
107 Segment::Candidate *candidate =
108 AddCandidate("gekkyokuwo", "TSUKIGIMEwo", "gekkyoku", "TSUKIGIME",
109 segment);
110 candidate->attributes |= Segment::Candidate::RERANKED;
111
112 AddCandidate("gekkyokuwo", "GEKKYOKUwo", "gekkyoku", "GEKKYOKU", segment);
113
114 config_.set_use_spelling_correction(false);
115
116 EXPECT_FALSE(rewriter_->Rewrite(convreq_, &segments));
117
118 config_.set_use_spelling_correction(true);
119 EXPECT_TRUE(rewriter_->Rewrite(convreq_, &segments));
120
121 // candidate 0
122 EXPECT_EQ(
123 (Segment::Candidate::RERANKED | Segment::Candidate::SPELLING_CORRECTION),
124 segments.conversion_segment(0).candidate(0).attributes);
125 EXPECT_EQ(
126 "<もしかして: tsukigime>",
127 segments.conversion_segment(0).candidate(0).description);
128
129 // candidate 1
130 EXPECT_EQ(Segment::Candidate::DEFAULT_ATTRIBUTE,
131 segments.conversion_segment(0).candidate(1).attributes);
132 EXPECT_TRUE(segments.conversion_segment(0).candidate(1).description.empty());
133 }
134
135 } // namespace mozc
136