Skip to content

Commit

Permalink
Merge branch 'google:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
shitamo authored Feb 5, 2025
2 parents 6b2f153 + fddcd2e commit 8d728c5
Show file tree
Hide file tree
Showing 56 changed files with 1,671 additions and 1,536 deletions.
45 changes: 22 additions & 23 deletions src/composer/composer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ std::pair<std::string, absl::btree_set<std::string>> GetQueriesForPrediction(
}

// auto = std::pair<std::string, absl::btree_set<std::string>>
auto[base_query, expanded] = composition.GetExpandedStrings();
auto [base_query, expanded] = composition.GetExpandedStrings();

// The above `GetExpandedStrings` generates expansion for modifier key as
// well, e.g., if the composition is "ざ", `expanded` contains "さ" too.
Expand All @@ -395,7 +395,7 @@ std::pair<std::string, absl::btree_set<std::string>> GetQueriesForPrediction(
RemoveExpandedCharsForModifier(asis, base_query, &expanded);

return std::make_pair(
japanese_util::FullWidthAsciiToHalfWidthAscii(base_query), expanded);
japanese_util::FullWidthAsciiToHalfWidthAscii(base_query), expanded);
}

std::string GetStringForTypeCorrection(const Composition &composition) {
Expand Down Expand Up @@ -460,8 +460,7 @@ void GetTransliterations(const Composition &composition,
ComposerData::ComposerData(
Composition composition, size_t position,
transliteration::TransliterationType input_mode,
commands::Context::InputFieldType input_field_type,
std::string source_text,
commands::Context::InputFieldType input_field_type, std::string source_text,
std::vector<commands::SessionCommand::CompositionEvent>
compositions_for_handwriting)
: composition_(composition),
Expand Down Expand Up @@ -552,7 +551,7 @@ std::string ComposerData::GetRawString() const {
}

std::string ComposerData::GetRawSubString(const size_t position,
const size_t size) const {
const size_t size) const {
return common::GetRawSubString(composition_, position, size);
}

Expand All @@ -570,12 +569,15 @@ void ComposerData::GetSubTransliterations(
// Composer

Composer::Composer()
: Composer(&Table::GetDefaultTable(),
&commands::Request::default_instance(),
&config::ConfigHandler::DefaultConfig()) {}
: Composer(Table::GetDefaultTable(), commands::Request::default_instance(),
config::ConfigHandler::DefaultConfig()) {}

Composer::Composer(const commands::Request &request,
const config::Config &config)
: Composer(Table::GetDefaultTable(), request, config) {}

Composer::Composer(const Table *table, const commands::Request *request,
const config::Config *config)
Composer::Composer(const Table &table, const commands::Request &request,
const config::Config &config)
: position_(0),
input_mode_(transliteration::HIRAGANA),
output_mode_(transliteration::HIRAGANA),
Expand All @@ -584,21 +586,18 @@ Composer::Composer(const Table *table, const commands::Request *request,
shifted_sequence_count_(0),
composition_(table),
max_length_(kMaxPreeditLength),
request_(request),
config_(config),
table_(table),
request_(&request),
config_(&config),
table_(&table),
is_new_input_(true) {
SetInputMode(transliteration::HIRAGANA);
if (config_ == nullptr) {
config_ = &config::ConfigHandler::DefaultConfig();
}
Reset();
}

// static
ComposerData Composer::CreateEmptyComposerData() {
static const absl::NoDestructor<Table> table;
static const absl::NoDestructor<Composition> composition(table.get());
static const absl::NoDestructor<Composition> composition(*table);
return ComposerData(*composition, 0, transliteration::HIRAGANA,
commands::Context::NORMAL, "", {});
}
Expand All @@ -625,16 +624,16 @@ void Composer::ReloadConfig() {

bool Composer::Empty() const { return (GetLength() == 0); }

void Composer::SetTable(const Table *table) {
table_ = table;
composition_.SetTable(table);
void Composer::SetTable(const Table &table) {
table_ = &table;
composition_.SetTable(*table_);
}

void Composer::SetRequest(const commands::Request *request) {
request_ = request;
void Composer::SetRequest(const commands::Request &request) {
request_ = &request;
}

void Composer::SetConfig(const config::Config *config) { config_ = config; }
void Composer::SetConfig(const config::Config &config) { config_ = &config; }

void Composer::SetInputMode(transliteration::TransliterationType mode) {
comeback_input_mode_ = mode;
Expand Down
40 changes: 20 additions & 20 deletions src/composer/composer.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ class ComposerData {
// This constructor is temporary and should be removed, when ConverterRequest
// is updated to use a const ComposerData.
ABSL_DEPRECATED("Do not use this constructor except in converter_request.h")
ComposerData(): composition_(Composition(nullptr)) {}
ComposerData() = default;

ComposerData(Composition composition,
size_t position,
ComposerData(Composition composition, size_t position,
transliteration::TransliterationType input_mode,
commands::Context::InputFieldType input_field_type,
std::string source_text,
Expand All @@ -89,8 +88,8 @@ class ComposerData {
std::string GetQueryForPrediction() const;

// Returns a expanded prediction query.
std::pair<std::string, absl::btree_set<std::string>>
GetQueriesForPrediction() const;
std::pair<std::string, absl::btree_set<std::string>> GetQueriesForPrediction()
const;

// Returns a string to be used for type correction.
std::string GetStringForTypeCorrection() const;
Expand All @@ -111,14 +110,13 @@ class ComposerData {
std::string GetRawSubString(size_t position, size_t size) const;

// Generate transliterations.
void GetTransliterations(
transliteration::Transliterations *t13ns) const;
void GetTransliterations(transliteration::Transliterations *t13ns) const;

// Generate substrings of transliterations.
void GetSubTransliterations(size_t position, size_t size,
transliteration::Transliterations *t13ns) const;

absl::string_view source_text() const {return source_text_; }
absl::string_view source_text() const { return source_text_; }

private:
// Composition copied from the Composer as a snapshot.
Expand Down Expand Up @@ -157,8 +155,11 @@ class Composer final {
};

Composer();
Composer(const Table *table, const commands::Request *request,
const config::Config *config);
Composer(const Table &table, const commands::Request &request,
const config::Config &config);
// This constructor is for testing.
ABSL_DEPRECATED("Use the constructor with Table")
Composer(const commands::Request &request, const config::Config &config);

// Copyable and movable.
Composer(const Composer &) = default;
Expand Down Expand Up @@ -187,10 +188,10 @@ class Composer final {
// Check the preedit string is empty or not.
bool Empty() const;

void SetTable(const Table *table);
void SetTable(const Table &table);

void SetRequest(const commands::Request *request);
void SetConfig(const config::Config *config);
void SetRequest(const commands::Request &request);
void SetConfig(const config::Config &config);

void SetInputMode(transliteration::TransliterationType mode);
void SetTemporaryInputMode(transliteration::TransliterationType mode);
Expand Down Expand Up @@ -228,8 +229,8 @@ class Composer final {
std::string GetQueryForPrediction() const;

// Returns a expanded prediction query.
std::pair<std::string, absl::btree_set<std::string>>
GetQueriesForPrediction() const;
std::pair<std::string, absl::btree_set<std::string>> GetQueriesForPrediction()
const;

// Returns a string to be used for type correction.
std::string GetStringForTypeCorrection() const;
Expand Down Expand Up @@ -301,8 +302,7 @@ class Composer final {
std::string GetRawSubString(size_t position, size_t size) const;

// Generate transliterations.
void GetTransliterations(
transliteration::Transliterations *t13ns) const;
void GetTransliterations(transliteration::Transliterations *t13ns) const;

// Generate substrings of specified transliteration.
std::string GetSubTransliteration(transliteration::TransliterationType type,
Expand Down Expand Up @@ -390,9 +390,9 @@ class Composer final {

size_t max_length_;

const commands::Request *request_;
const config::Config *config_;
const Table *table_;
const commands::Request *request_ = nullptr;
const config::Config *config_ = nullptr;
const Table *table_ = nullptr;

// Timestamp of last modified.
int64_t timestamp_msec_;
Expand Down
5 changes: 2 additions & 3 deletions src/composer/composer_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ int main(int argc, char **argv) {

mozc::composer::Table table;
table.LoadFromFile(absl::GetFlag(FLAGS_table).c_str());
std::unique_ptr<mozc::composer::Composer> composer(
new mozc::composer::Composer(&table, &Request::default_instance(),
&Config::default_instance()));
auto composer = std::make_unique<mozc::composer::Composer>(
table, Request::default_instance(), Config::default_instance());

std::string command;
std::string left, focused, right;
Expand Down
19 changes: 8 additions & 11 deletions src/composer/composer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ class ComposerTest : public ::testing::Test {
table_ = std::make_unique<Table>();
config_ = std::make_unique<Config>();
request_ = std::make_unique<Request>();
composer_ =
std::make_unique<Composer>(table_.get(), request_.get(), config_.get());
composer_ = std::make_unique<Composer>(*table_, *request_, *config_);
CharacterFormManager::GetCharacterFormManager()->SetDefaultRule();
}

Expand Down Expand Up @@ -855,8 +854,7 @@ TEST_F(ComposerTest, InsertCharacterKeyEventWithInputMode) {
EXPECT_EQ(composer_->GetInputMode(), transliteration::HIRAGANA);
}

composer_ =
std::make_unique<Composer>(table_.get(), request_.get(), config_.get());
composer_ = std::make_unique<Composer>(*table_, *request_, *config_);

{
// "a" → "あ" (Hiragana)
Expand Down Expand Up @@ -1258,8 +1256,7 @@ TEST_F(ComposerTest, AutoIMETurnOffEnabled) {
EXPECT_EQ(composer_->GetInputMode(), transliteration::HIRAGANA);
}

composer_ =
std::make_unique<Composer>(table_.get(), request_.get(), config_.get());
composer_ = std::make_unique<Composer>(*table_, *request_, *config_);

{ // google
InsertKey("g", composer_.get());
Expand Down Expand Up @@ -1324,8 +1321,7 @@ TEST_F(ComposerTest, AutoIMETurnOffEnabled) {
}

config_->set_shift_key_mode_switch(Config::OFF);
composer_ =
std::make_unique<Composer>(table_.get(), request_.get(), config_.get());
composer_ = std::make_unique<Composer>(*table_, *request_, *config_);

{ // Google
InsertKey("G", composer_.get());
Expand Down Expand Up @@ -1557,7 +1553,7 @@ TEST_F(ComposerTest, DisabledUpdateInputMode) {
// Set the flag disable.
commands::Request request;
request.set_update_input_mode_from_surrounding_text(false);
composer_->SetRequest(&request);
composer_->SetRequest(request);

table_->AddRule("a", "", "");
table_->AddRule("i", "", "");
Expand Down Expand Up @@ -2515,7 +2511,7 @@ TEST_F(ComposerTest, 12KeysAsciiGetQueryForPrediction) {
request.set_mixed_conversion(true);
request.set_special_romanji_table(
commands::Request::TWELVE_KEYS_TO_HALFWIDTHASCII);
composer_->SetRequest(&request);
composer_->SetRequest(request);
table_->InitializeWithRequestAndConfig(
request, config::ConfigHandler::DefaultConfig());
composer_->InsertCharacter("2");
Expand Down Expand Up @@ -2945,7 +2941,8 @@ TEST_F(ComposerTest, NBforeN_WithFullWidth) {
EXPECT_EQ(right, "");

// auto = std::pair<std::string, absl::btree_set<std::string>>
const auto [queries_base, queries_expanded] = composer_->GetQueriesForPrediction();
const auto [queries_base, queries_expanded] =
composer_->GetQueriesForPrediction();
EXPECT_EQ(queries_base, "あn");

EXPECT_EQ(composer_->GetQueryForPrediction(), "あnn");
Expand Down
1 change: 1 addition & 0 deletions src/composer/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mozc_cc_library(
"//base/strings:assign",
"//base/strings:unicode",
"//composer:table",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
Expand Down
24 changes: 13 additions & 11 deletions src/composer/internal/char_chunk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ bool GetFromPending(const Table *table, const absl::string_view key,
} // namespace

CharChunk::CharChunk(Transliterators::Transliterator transliterator,
const Table *table)
: table_(table),
transliterator_(transliterator),
attributes_(NO_TABLE_ATTRIBUTE),
local_length_cache_(std::string::npos) {
DCHECK_NE(Transliterators::LOCAL, transliterator);
const Table &table)
: table_(&table), transliterator_(transliterator) {
DCHECK_NE(transliterator, Transliterators::LOCAL);
}

CharChunk::CharChunk(Transliterators::Transliterator transliterator)
: transliterator_(transliterator) {
DCHECK_NE(transliterator, Transliterators::LOCAL);
}

void CharChunk::Clear() {
Expand Down Expand Up @@ -256,14 +258,14 @@ absl::btree_set<std::string> CharChunk::GetExpandedResults() const {
bool CharChunk::IsFixed() const { return pending_.empty(); }

bool CharChunk::IsAppendable(Transliterators::Transliterator t12r,
const Table *table) const {
const Table &table) const {
return !pending_.empty() &&
(t12r == Transliterators::LOCAL || t12r == transliterator_) &&
table == table_;
&table == table_;
}

bool CharChunk::IsConvertible(Transliterators::Transliterator t12r,
const Table *table,
const Table &table,
const absl::string_view input) const {
if (!IsAppendable(t12r, table)) {
return false;
Expand All @@ -272,7 +274,7 @@ bool CharChunk::IsConvertible(Transliterators::Transliterator t12r,
size_t key_length = 0;
bool fixed = false;
std::string key = absl::StrCat(pending_, input);
const Entry *entry = table->LookUpPrefix(key, &key_length, &fixed);
const Entry *entry = table.LookUpPrefix(key, &key_length, &fixed);

return entry && (key.size() == key_length) && fixed;
}
Expand Down Expand Up @@ -563,7 +565,7 @@ absl::StatusOr<CharChunk> CharChunk::SplitChunk(
DeleteSpecialKeys(conversion_ + pending_), &raw_lhs, &raw_rhs,
&converted_lhs, &converted_rhs);

CharChunk left_new_chunk(transliterator_, table_);
CharChunk left_new_chunk(transliterator_, *table_);
left_new_chunk.set_raw(raw_lhs);
set_raw(std::move(raw_rhs));

Expand Down
Loading

0 comments on commit 8d728c5

Please sign in to comment.