diff --git a/src/rime/dict/user_dictionary.cc b/src/rime/dict/user_dictionary.cc index 078d3d984..2c7b9869a 100644 --- a/src/rime/dict/user_dictionary.cc +++ b/src/rime/dict/user_dictionary.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -116,8 +117,8 @@ bool UserDictEntryIterator::Next() { // UserDictionary members -UserDictionary::UserDictionary(const an& db) - : db_(db) { +UserDictionary::UserDictionary(const string& name, an db) + : name_(name), db_(db) { } UserDictionary::~UserDictionary() { @@ -494,10 +495,8 @@ UserDictionary* UserDictionaryComponent::Create(const Ticket& ticket) { // user specified name } else if (config->GetString(ticket.name_space + "/dictionary", &dict_name)) { - // {dictionary: lunapinyin.extra} implies {user_dict: luna_pinyin} - size_t dot = dict_name.find('.'); - if (dot != string::npos && dot != 0) - dict_name.resize(dot); + // {dictionary: luna_pinyin.extra} implies {user_dict: luna_pinyin} + dict_name = Language::get_language_component(dict_name); } else { LOG(ERROR) << ticket.name_space << "/dictionary not specified in schema '" @@ -519,7 +518,7 @@ UserDictionary* UserDictionaryComponent::Create(const Ticket& ticket) { db.reset(component->Create(dict_name)); db_pool_[dict_name] = db; } - return new UserDictionary(db); + return new UserDictionary(dict_name, db); } } // namespace rime diff --git a/src/rime/dict/user_dictionary.h b/src/rime/dict/user_dictionary.h index 67f1427fb..4601a22f6 100644 --- a/src/rime/dict/user_dictionary.h +++ b/src/rime/dict/user_dictionary.h @@ -50,7 +50,7 @@ struct Ticket; class UserDictionary : public Class { public: - explicit UserDictionary(const an& db); + UserDictionary(const string& name, an db); virtual ~UserDictionary(); void Attach(const an& table, const an& prism); diff --git a/src/rime/gear/memory.cc b/src/rime/gear/memory.cc index 644bfd7bb..bde9a4785 100644 --- a/src/rime/gear/memory.cc +++ b/src/rime/gear/memory.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -65,6 +66,13 @@ Memory::Memory(const Ticket& ticket) { } } + // user dictionary is named after language; dictionary name may have an + // optional suffix separated from the language component by dot. + language_.reset(new Language{ + user_dict_ ? user_dict_->name() : + Language::get_language_component(dict_->name()) + }); + Context* ctx = ticket.engine->context(); commit_connection_ = ctx->commit_notifier().connect( [this](Context* ctx) { OnCommit(ctx); }); @@ -100,7 +108,7 @@ void Memory::OnCommit(Context* ctx) { for (auto& seg : ctx->composition()) { auto phrase = As(Candidate::GetGenuineCandidate( seg.GetSelectedCandidate())); - bool recognized = phrase && phrase->language() == language(); + bool recognized = Language::intelligible(phrase, this); if (recognized) { commit_entry.AppendPhrase(phrase); } @@ -119,8 +127,7 @@ void Memory::OnDeleteEntry(Context* ctx) { return; auto phrase = As(Candidate::GetGenuineCandidate( ctx->GetSelectedCandidate())); - bool recognized = phrase && phrase->language() == language(); - if (recognized) { + if (Language::intelligible(phrase, this)) { const DictEntry& entry(phrase->entry()); LOG(INFO) << "deleting entry: '" << entry.text << "'."; user_dict_->UpdateEntry(entry, -1); // mark as deleted in user dict diff --git a/src/rime/gear/memory.h b/src/rime/gear/memory.h index 5ec8145f7..0b065491f 100644 --- a/src/rime/gear/memory.h +++ b/src/rime/gear/memory.h @@ -17,6 +17,7 @@ class Context; class Engine; class Dictionary; class UserDictionary; +class Language; class Phrase; class Memory; @@ -31,9 +32,6 @@ struct CommitEntry : DictEntry { bool Save() const; }; -class Language { -}; - class Memory { public: Memory(const Ticket& ticket); @@ -45,11 +43,11 @@ class Memory { bool FinishSession(); bool DiscardSession(); - Language* language() { return &language_; } - Dictionary* dict() const { return dict_.get(); } UserDictionary* user_dict() const { return user_dict_.get(); } + const Language* language() const { return language_.get(); } + protected: void OnCommit(Context* ctx); void OnDeleteEntry(Context* ctx); @@ -57,12 +55,12 @@ class Memory { the dict_; the user_dict_; + the language_; private: connection commit_connection_; connection delete_connection_; connection unhandled_key_connection_; - Language language_; }; } // namespace rime diff --git a/src/rime/gear/poet.h b/src/rime/gear/poet.h index 55f0c36b8..df27da549 100644 --- a/src/rime/gear/poet.h +++ b/src/rime/gear/poet.h @@ -21,12 +21,12 @@ class Language; class Poet { public: - Poet(Language* language) : language_(language) {} + Poet(const Language* language) : language_(language) {} + + an MakeSentence(const WordGraph& graph, size_t total_length); - an MakeSentence(const WordGraph& graph, - size_t total_length); protected: - Language* language_; + const Language* language_; }; } // namespace rime diff --git a/src/rime/gear/table_translator.cc b/src/rime/gear/table_translator.cc index 6f6d0c892..988a21577 100644 --- a/src/rime/gear/table_translator.cc +++ b/src/rime/gear/table_translator.cc @@ -28,21 +28,10 @@ static const char* kUnitySymbol = " \xe2\x98\xaf "; // TableTranslation TableTranslation::TableTranslation(TranslatorOptions* options, - Language* language, + const Language* language, const string& input, - size_t start, size_t end, - const string& preedit) - : options_(options), language_(language), - input_(input), start_(start), end_(end), preedit_(preedit) { - if (options_) - options_->preedit_formatter().Apply(&preedit_); - set_exhausted(true); -} - -TableTranslation::TableTranslation(TranslatorOptions* options, - Language* language, - const string& input, - size_t start, size_t end, + size_t start, + size_t end, const string& preedit, const DictEntryIterator& iter, const UserDictEntryIterator& uter) diff --git a/src/rime/gear/table_translator.h b/src/rime/gear/table_translator.h index 6b5767310..cd4b01dbe 100644 --- a/src/rime/gear/table_translator.h +++ b/src/rime/gear/table_translator.h @@ -50,13 +50,13 @@ class TableTranslator : public Translator, class TableTranslation : public Translation { public: - TableTranslation(TranslatorOptions* options, Language* language, - const string& input, size_t start, size_t end, - const string& preedit); - TableTranslation(TranslatorOptions* options, Language* language, - const string& input, size_t start, size_t end, + TableTranslation(TranslatorOptions* options, + const Language* language, + const string& input, + size_t start, + size_t end, const string& preedit, - const DictEntryIterator& iter, + const DictEntryIterator& iter = DictEntryIterator(), const UserDictEntryIterator& uter = UserDictEntryIterator()); virtual bool Next(); @@ -74,7 +74,7 @@ class TableTranslation : public Translation { } TranslatorOptions* options_; - Language* language_; + const Language* language_; string input_; size_t start_; size_t end_; diff --git a/src/rime/gear/translator_commons.h b/src/rime/gear/translator_commons.h index 566a522d2..3367bf41e 100644 --- a/src/rime/gear/translator_commons.h +++ b/src/rime/gear/translator_commons.h @@ -70,7 +70,7 @@ class Language; class Phrase : public Candidate { public: - Phrase(Language* language, + Phrase(const Language* language, const string& type, size_t start, size_t end, const an& entry) : Candidate(type, start, end), @@ -93,14 +93,14 @@ class Phrase : public Candidate { double weight() const { return entry_->weight; } Code& code() const { return entry_->code; } const DictEntry& entry() const { return *entry_; } - Language* language() const { return language_; } + const Language* language() const { return language_; } Spans spans() { return syllabifier_ ? syllabifier_->Syllabify(this) : Spans(); } protected: - Language* language_; + const Language* language_; an entry_; an syllabifier_; }; @@ -109,7 +109,7 @@ class Phrase : public Candidate { class Sentence : public Phrase { public: - Sentence(Language* language) + Sentence(const Language* language) : Phrase(language, "sentence", 0, 0, New()) { entry_->weight = 1.0; } diff --git a/src/rime/language.cc b/src/rime/language.cc new file mode 100644 index 000000000..5626499f8 --- /dev/null +++ b/src/rime/language.cc @@ -0,0 +1,18 @@ +// +// Copyright RIME Developers +// Distributed under the BSD License +// +#include +#include + +namespace rime { + +// "luna_pinyin.extra" has language component "luna_pinyin". +string Language::get_language_component(const string& name) { + size_t dot = name.find('.'); + if (dot != string::npos && dot != 0) + return name.substr(0, dot); + return name; +} + +} // namespace rime diff --git a/src/rime/language.h b/src/rime/language.h new file mode 100644 index 000000000..dbc8c0a2b --- /dev/null +++ b/src/rime/language.h @@ -0,0 +1,34 @@ +// +// Copyright RIME Developers +// Distributed under the BSD License +// +#ifndef RIME_LANGUAGE_H_ +#define RIME_LANGUAGE_H_ + +#include + +namespace rime { + +class Language { + const string name_; + + public: + Language(const string& name) : name_(name) {} + string name() const { return name_; } + + bool operator== (const Language& other) const { + return name_ == other.name_; + } + + template + static bool intelligible(const T& t, const U& u) { + return t && t->language() && u && u->language() && + *t->language() == *u->language(); + } + + static string get_language_component(const string& name); +}; + +} // namespace rime + +#endif // RIME_LANGUAGE_H_