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_CONVERTER_CONVERTER_H_
31 #define MOZC_CONVERTER_CONVERTER_H_
32 
33 #include <memory>
34 #include <string>
35 
36 #include "converter/converter_interface.h"
37 #include "dictionary/pos_matcher.h"
38 #include "dictionary/suppression_dictionary.h"
39 //  for FRIEND_TEST()
40 #include "testing/base/public/gunit_prod.h"
41 
42 namespace mozc {
43 
44 class ConversionRequest;
45 class ImmutableConverterInterface;
46 class PredictorInterface;
47 class RewriterInterface;
48 class Segments;
49 
50 class ConverterImpl : public ConverterInterface {
51  public:
52   ConverterImpl();
53   virtual ~ConverterImpl();
54 
55   // Lazily initializes the internal members. Must be called before the use.
56   void Init(const dictionary::POSMatcher *pos_matcher,
57             const dictionary::SuppressionDictionary *suppression_dictionary,
58             PredictorInterface *predictor,
59             RewriterInterface *rewriter,
60             ImmutableConverterInterface *immutable_converter);
61 
62   bool Predict(const ConversionRequest &request,
63                const string &key,
64                const Segments::RequestType request_type,
65                Segments *segments) const;
66 
67   virtual bool StartConversionForRequest(const ConversionRequest &request,
68                                          Segments *segments) const;
69   virtual bool StartConversion(Segments *segments,
70                                const string &key) const;
71   virtual bool StartReverseConversion(Segments *segments,
72                                       const string &key) const;
73   virtual bool StartPredictionForRequest(const ConversionRequest &request,
74                                          Segments *segments) const;
75   virtual bool StartPrediction(Segments *segments,
76                                const string &key) const;
77   virtual bool StartSuggestionForRequest(const ConversionRequest &request,
78                                          Segments *segments) const;
79   virtual bool StartSuggestion(Segments *segments,
80                                const string &key) const;
81   virtual bool StartPartialPredictionForRequest(
82       const ConversionRequest &request, Segments *segments) const;
83   virtual bool StartPartialPrediction(Segments *segments,
84                                       const string &key) const;
85   virtual bool StartPartialSuggestionForRequest(
86       const ConversionRequest &request, Segments *segments) const;
87   virtual bool StartPartialSuggestion(Segments *segments,
88                                       const string &key) const;
89 
90   virtual bool FinishConversion(const ConversionRequest &request,
91                                 Segments *segments) const;
92   virtual bool CancelConversion(Segments *segments) const;
93   virtual bool ResetConversion(Segments *segments) const;
94   virtual bool RevertConversion(Segments *segments) const;
95   virtual bool ReconstructHistory(Segments *segments,
96                                   const string &preceding_text) const;
97 
98   virtual bool CommitSegmentValue(Segments *segments,
99                                   size_t segment_index,
100                                   int candidate_index) const;
101   virtual bool CommitPartialSuggestionSegmentValue(
102       Segments *segments,
103       size_t segment_index,
104       int candidate_index,
105       const string &current_segment_key,
106       const string &new_segment_key) const;
107   virtual bool FocusSegmentValue(Segments *segments,
108                                  size_t segment_index,
109                                  int candidate_index) const;
110   virtual bool FreeSegmentValue(Segments *segments,
111                                 size_t segment_index) const;
112   virtual bool CommitSegments(Segments *segments,
113                               const std::vector<size_t> &candidate_index) const;
114   virtual bool ResizeSegment(Segments *segments,
115                              const ConversionRequest &requset,
116                              size_t segment_index,
117                              int offset_length) const;
118   virtual bool ResizeSegment(Segments *segments,
119                              const ConversionRequest &requset,
120                              size_t start_segment_index,
121                              size_t segments_size,
122                              const uint8 *new_size_array,
123                              size_t array_size) const;
124 
125  private:
126   FRIEND_TEST(ConverterTest, CompletePOSIds);
127   FRIEND_TEST(ConverterTest, DefaultPredictor);
128   FRIEND_TEST(ConverterTest, MaybeSetConsumedKeySizeToSegment);
129   FRIEND_TEST(ConverterTest, GetLastConnectivePart);
130 
131   // Complete Left id/Right id if they are not defined.
132   // Some users don't push conversion button but directly
133   // input hiragana sequence only with composition mode. Converter
134   // cannot know which POS ids should be used for these directly-
135   // input strings. This function estimates IDs from value heuristically.
136   void CompletePOSIds(Segment::Candidate *candidate) const;
137 
138   bool CommitSegmentValueInternal(Segments *segments,
139                                   size_t segment_index,
140                                   int candidate_index,
141                                   Segment::SegmentType segment_type) const;
142 
143   // Sets all the candidates' attribute PARTIALLY_KEY_CONSUMED
144   // and consumed_key_size if the attribute is not set.
145   static void MaybeSetConsumedKeySizeToCandidate(size_t consumed_key_size,
146                                                  Segment::Candidate* candidate);
147 
148   // Sets all the candidates' attribute PARTIALLY_KEY_CONSUMED
149   // and consumed_key_size if the attribute is not set.
150   static void MaybeSetConsumedKeySizeToSegment(size_t consumed_key_size,
151                                                Segment* segment);
152 
153   // Rewrites and applies the suppression dictionary.
154   void RewriteAndSuppressCandidates(const ConversionRequest &request,
155                                     Segments *segments) const;
156 
157   // Limits the number of candidates based on a request.
158   // This method doesn't drop meta candidates for T13n conversion.
159   void TrimCandidates(const ConversionRequest &request,
160                       Segments *segments) const;
161 
162   // Commits usage stats for committed text.
163   // |begin_segment_index| is a index of whole segments. (history and conversion
164   // segments)
165   void CommitUsageStats(const Segments *segments,
166                         size_t begin_segment_index,
167                         size_t segment_length) const;
168 
169   // Returns the substring of |str|. This substring consists of similar script
170   // type and you can use it as preceding text for conversion.
171   bool GetLastConnectivePart(const string &preceding_text,
172                              string *key,
173                              string *value,
174                              uint16 *id) const;
175 
176   const dictionary::POSMatcher *pos_matcher_;
177   const dictionary::SuppressionDictionary *suppression_dictionary_;
178   std::unique_ptr<PredictorInterface> predictor_;
179   std::unique_ptr<RewriterInterface> rewriter_;
180   const ImmutableConverterInterface *immutable_converter_;
181   uint16 general_noun_id_;
182 };
183 
184 }  // namespace mozc
185 
186 #endif  // MOZC_CONVERTER_CONVERTER_H_
187