Skip to content

Commit b30921c

Browse files
taku910hiroyuki-komatsu
authored andcommitted
Factor out ConversionRequest::Options to options.h
Factor out the ConversionRequest::Options structure and its dependent enums into a new header file options.h, while maintaining full backward compatibility. PiperOrigin-RevId: 923211193
1 parent 67cede2 commit b30921c

3 files changed

Lines changed: 151 additions & 86 deletions

File tree

src/request/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ load(
3535

3636
package(default_visibility = ["//visibility:private"])
3737

38+
mozc_cc_library(
39+
name = "options",
40+
hdrs = ["options.h"],
41+
visibility = [
42+
"//:__subpackages__",
43+
"//converter:__pkg__",
44+
"//dictionary:__subpackages__",
45+
"//engine:__pkg__",
46+
"//prediction:__pkg__",
47+
"//rewriter:__pkg__",
48+
],
49+
)
50+
3851
mozc_cc_library(
3952
name = "conversion_request",
4053
hdrs = ["conversion_request.h"],
@@ -47,6 +60,7 @@ mozc_cc_library(
4760
"//rewriter:__pkg__",
4861
],
4962
deps = [
63+
":options",
5064
"//base:util",
5165
"//base/strings:assign",
5266
"//composer",

src/request/conversion_request.h

Lines changed: 8 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
#include "prediction/result.h"
5151
#include "protocol/commands.pb.h"
5252
#include "protocol/config.pb.h"
53+
#include "request/options.h"
5354

5455
namespace mozc {
55-
inline constexpr size_t kMaxConversionCandidatesSize = 200;
5656

5757
namespace internal {
5858

@@ -131,91 +131,13 @@ class copy_or_view_ptr {
131131
// detailed context information such as commands::Context.
132132
class ConversionRequest {
133133
public:
134-
enum RequestType {
135-
CONVERSION, // normal conversion
136-
REVERSE_CONVERSION, // reverse conversion
137-
PREDICTION, // show prediction with user tab key
138-
SUGGESTION, // show prediction automatically
139-
PARTIAL_PREDICTION, // show prediction using the text before cursor
140-
PARTIAL_SUGGESTION, // show suggestion using the text before cursor
141-
};
142-
143-
enum ComposerKeySelection {
144-
// Use Composer::GetQueryForConversion() to generate conversion key. This
145-
// option uses the exact composition which user sees, e.g., "とうk".
146-
CONVERSION_KEY,
147-
148-
// Use Composer::GetQueryForPrediction() to generate conversion key. This
149-
// option trims the trailing unresolved romaji. For example, if composition
150-
// is "とうk", the conversion key becomes "とう". This option is mainly used
151-
// in dictionary_predictor.cc for realtime conversion.
152-
PREDICTION_KEY,
153-
154-
// TODO(team): We may want to implement other options. For instance,
155-
// Composer::GetQueriesForPrediction() expands the trailing romaji to a set
156-
// of possible hiragana.
157-
};
158-
159-
// Options must be trivially copyable to get hash value directly.
160-
struct Options {
161-
RequestType request_type = CONVERSION;
162-
163-
// Which composer's method to use for conversion key; see the comment around
164-
// the definition of ComposerKeySelection above.
165-
ComposerKeySelection composer_key_selection = CONVERSION_KEY;
166-
167-
int max_conversion_candidates_size = kMaxConversionCandidatesSize;
168-
int max_user_history_prediction_candidates_size = 3;
169-
int max_user_history_prediction_candidates_size_for_zero_query = 4;
170-
int max_dictionary_prediction_candidates_size = 20;
171-
172-
// If true, insert a top candidate from the actual (non-immutable) converter
173-
// to realtime conversion results. Note that setting this true causes a big
174-
// performance loss (3 times slower).
175-
bool use_actual_converter_for_realtime_conversion = false;
176-
177-
// Don't use this flag directly. This flag is used by DictionaryPredictor to
178-
// skip some heavy rewriters, such as UserBoundaryHistoryRewriter and
179-
// TransliterationRewriter.
180-
// TODO(noriyukit): Fix such a hacky handling for realtime conversion.
181-
bool skip_slow_rewriters = false;
182-
183-
// If true, partial candidates are created on prediction/suggestion.
184-
// For example, "私の" is created from composition "わたしのなまえ".
185-
bool create_partial_candidates = false;
186-
187-
// If false, stop using user history for conversion.
188-
// This is used for supporting CONVERT_WITHOUT_HISTORY command.
189-
// Please refer to session/internal/keymap.h
190-
bool enable_user_history_for_conversion = true;
191-
192-
// If true, enable kana modifier insensitive conversion.
193-
bool kana_modifier_insensitive_conversion = true;
194-
195-
// If true, use conversion_segment(0).key() instead of ComposerData.
196-
// TODO(b/365909808): Create a new string field to store the key.
197-
bool use_already_typing_corrected_key = false;
198-
199-
// Enables incognito mode even when Config.incognito_mode() or
200-
// Request.is_incognito_mode() is false. Use this flag to dynamically change
201-
// the incognito_mode per client request.
202-
bool incognito_mode = false;
203-
204-
// Overrides the bos_id to a specific POS id to specify the context
205-
// information. Note that the bos_id must be in the valid range.
206-
// POS id 0 is reserved for the default BOS/EOS.
207-
uint16_t bos_id = 0;
208-
209-
// Disables to add prefix penalty.
210-
// Used to calculate the cost of a suffix of a word.
211-
bool disable_prefix_penalty = false;
212-
213-
// This conversion request is called by predictor for realtime conversion.
214-
bool used_in_predictor_realtime_conversion = false;
215-
};
216-
217-
static_assert(std::is_trivially_copyable<Options>::value,
218-
"Options must be trivially copyable");
134+
using RequestType = ::mozc::RequestType;
135+
using enum ::mozc::RequestType;
136+
137+
using ComposerKeySelection = ::mozc::ComposerKeySelection;
138+
using enum ::mozc::ComposerKeySelection;
139+
140+
using Options = ::mozc::ConversionOptions;
219141

220142
// Default constructor stores the view.
221143
// All default variable returns global reference.

src/request/options.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2010-2021, 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_REQUEST_OPTIONS_H_
31+
#define MOZC_REQUEST_OPTIONS_H_
32+
33+
#include <cstddef>
34+
#include <cstdint>
35+
#include <type_traits>
36+
37+
namespace mozc {
38+
39+
inline constexpr size_t kMaxConversionCandidatesSize = 200;
40+
41+
enum RequestType {
42+
CONVERSION, // normal conversion
43+
REVERSE_CONVERSION, // reverse conversion
44+
PREDICTION, // show prediction with user tab key
45+
SUGGESTION, // show prediction automatically
46+
PARTIAL_PREDICTION, // show prediction using the text before cursor
47+
PARTIAL_SUGGESTION, // show suggestion using the text before cursor
48+
};
49+
50+
enum ComposerKeySelection {
51+
// Use Composer::GetQueryForConversion() to generate conversion key. This
52+
// option uses the exact composition which user sees, e.g., "とうk".
53+
CONVERSION_KEY,
54+
55+
// Use Composer::GetQueryForPrediction() to generate conversion key. This
56+
// option trims the trailing unresolved romaji. For example, if composition
57+
// is "とうk", the conversion key becomes "とう". This option is mainly used
58+
// in dictionary_predictor.cc for realtime conversion.
59+
PREDICTION_KEY,
60+
61+
// TODO(team): We may want to implement other options. For instance,
62+
// Composer::GetQueriesForPrediction() expands the trailing romaji to a set
63+
// of possible hiragana.
64+
};
65+
66+
// Options must be trivially copyable to get hash value directly.
67+
struct ConversionOptions {
68+
RequestType request_type = CONVERSION;
69+
70+
// Which composer's method to use for conversion key; see the comment around
71+
// the definition of ComposerKeySelection above.
72+
ComposerKeySelection composer_key_selection = CONVERSION_KEY;
73+
74+
int max_conversion_candidates_size = kMaxConversionCandidatesSize;
75+
int max_user_history_prediction_candidates_size = 3;
76+
int max_user_history_prediction_candidates_size_for_zero_query = 4;
77+
int max_dictionary_prediction_candidates_size = 20;
78+
79+
// If true, insert a top candidate from the actual (non-immutable) converter
80+
// to realtime conversion results. Note that setting this true causes a big
81+
// performance loss (3 times slower).
82+
bool use_actual_converter_for_realtime_conversion = false;
83+
84+
// Don't use this flag directly. This flag is used by DictionaryPredictor to
85+
// skip some heavy rewriters, such as UserBoundaryHistoryRewriter and
86+
// TransliterationRewriter.
87+
// TODO(noriyukit): Fix such a hacky handling for realtime conversion.
88+
bool skip_slow_rewriters = false;
89+
90+
// If true, partial candidates are created on prediction/suggestion.
91+
// For example, "私の" is created from composition "わたしのなまえ".
92+
bool create_partial_candidates = false;
93+
94+
// If false, stop using user history for conversion.
95+
// This is used for supporting CONVERT_WITHOUT_HISTORY command.
96+
// Please refer to session/internal/keymap.h
97+
bool enable_user_history_for_conversion = true;
98+
99+
// If true, enable kana modifier insensitive conversion.
100+
bool kana_modifier_insensitive_conversion = true;
101+
102+
// If true, use conversion_segment(0).key() instead of ComposerData.
103+
// TODO(b/365909808): Create a new string field to store the key.
104+
bool use_already_typing_corrected_key = false;
105+
106+
// Enables incognito mode even when Config.incognito_mode() or
107+
// Request.is_incognito_mode() is false. Use this flag to dynamically change
108+
// the incognito_mode per client request.
109+
bool incognito_mode = false;
110+
111+
// Overrides the bos_id to a specific POS id to specify the context
112+
// information. Note that the bos_id must be in the valid range.
113+
// POS id 0 is reserved for the default BOS/EOS.
114+
uint16_t bos_id = 0;
115+
116+
// Disables to add prefix penalty.
117+
// Used to calculate the cost of a suffix of a word.
118+
bool disable_prefix_penalty = false;
119+
120+
// This conversion request is called by predictor for realtime conversion.
121+
bool used_in_predictor_realtime_conversion = false;
122+
};
123+
124+
static_assert(std::is_trivially_copyable<ConversionOptions>::value,
125+
"ConversionOptions must be trivially copyable");
126+
127+
} // namespace mozc
128+
129+
#endif // MOZC_REQUEST_OPTIONS_H_

0 commit comments

Comments
 (0)