Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(language): shared user dictionary per language (Closes #184) #214

Merged
merged 4 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/rime/dict/user_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ bool UserDictEntryIterator::Next() {

// UserDictionary members

UserDictionary::UserDictionary(const an<Db>& db)
: db_(db) {
UserDictionary::UserDictionary(Language language, an<Db> db)
: language_(language), db_(db) {
}

UserDictionary::~UserDictionary() {
Expand Down Expand Up @@ -519,7 +519,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(Language{dict_name}, db);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从 L500 以后 dict_name 变量实际已经变成 language_name 之类了?是不是应该使用新的命名

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我又改回去了。
Language 改成在 Memory 類裏創建。
這裏求「語言」名字還是爲了給用戶詞典取名。

}

} // namespace rime
7 changes: 4 additions & 3 deletions src/rime/dict/user_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <time.h>
#include <rime/common.h>
#include <rime/component.h>
#include <rime/language.h>
#include <rime/dict/user_db.h>
#include <rime/dict/vocabulary.h>

Expand Down Expand Up @@ -50,7 +51,7 @@ struct Ticket;

class UserDictionary : public Class<UserDictionary, const Ticket&> {
public:
explicit UserDictionary(const an<Db>& db);
UserDictionary(Language language, an<Db> db);
virtual ~UserDictionary();

void Attach(const an<Table>& table, const an<Prism>& prism);
Expand All @@ -76,7 +77,7 @@ class UserDictionary : public Class<UserDictionary, const Ticket&> {
bool RevertRecentTransaction();
bool CommitPendingTransaction();

const string& name() const { return name_; }
const Language* language() const { return &language_; }
TickCount tick() const { return tick_; }

static an<DictEntry> CreateDictEntry(const string& key,
Expand All @@ -94,7 +95,7 @@ class UserDictionary : public Class<UserDictionary, const Ticket&> {
DfsState* state);

private:
string name_;
const Language language_;
an<Db> db_;
an<Table> table_;
an<Prism> prism_;
Expand Down
9 changes: 6 additions & 3 deletions src/rime/gear/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ bool Memory::DiscardSession() {
return user_dict_ && user_dict_->RevertRecentTransaction();
}

const Language* Memory::language() const {
return user_dict_ ? user_dict_->language() : nullptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么 language 可以为空值?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更新了更新了~

}

void Memory::OnCommit(Context* ctx) {
if (!user_dict_|| user_dict_->readonly())
return;
Expand All @@ -100,7 +104,7 @@ void Memory::OnCommit(Context* ctx) {
for (auto& seg : ctx->composition()) {
auto phrase = As<Phrase>(Candidate::GetGenuineCandidate(
seg.GetSelectedCandidate()));
bool recognized = phrase && phrase->language() == language();
bool recognized = Language::intelligible(phrase, this);
if (recognized) {
commit_entry.AppendPhrase(phrase);
}
Expand All @@ -119,8 +123,7 @@ void Memory::OnDeleteEntry(Context* ctx) {
return;
auto phrase = As<Phrase>(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
Expand Down
9 changes: 3 additions & 6 deletions src/rime/gear/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Context;
class Engine;
class Dictionary;
class UserDictionary;
class Language;
class Phrase;
class Memory;

Expand All @@ -31,9 +32,6 @@ struct CommitEntry : DictEntry {
bool Save() const;
};

class Language {
};

class Memory {
public:
Memory(const Ticket& ticket);
Expand All @@ -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;

protected:
void OnCommit(Context* ctx);
void OnDeleteEntry(Context* ctx);
Expand All @@ -62,7 +60,6 @@ class Memory {
connection commit_connection_;
connection delete_connection_;
connection unhandled_key_connection_;
Language language_;
};

} // namespace rime
Expand Down
8 changes: 4 additions & 4 deletions src/rime/gear/poet.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class Language;

class Poet {
public:
Poet(Language* language) : language_(language) {}
Poet(const Language* language) : language_(language) {}

an<Sentence> MakeSentence(const WordGraph& graph, size_t total_length);

an<Sentence> MakeSentence(const WordGraph& graph,
size_t total_length);
protected:
Language* language_;
const Language* language_;
};

} // namespace rime
Expand Down
17 changes: 3 additions & 14 deletions src/rime/gear/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/rime/gear/table_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -74,7 +74,7 @@ class TableTranslation : public Translation {
}

TranslatorOptions* options_;
Language* language_;
const Language* language_;
string input_;
size_t start_;
size_t end_;
Expand Down
8 changes: 4 additions & 4 deletions src/rime/gear/translator_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<DictEntry>& entry)
: Candidate(type, start, end),
Expand All @@ -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<DictEntry> entry_;
an<PhraseSyllabifier> syllabifier_;
};
Expand All @@ -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<DictEntry>()) {
entry_->weight = 1.0;
}
Expand Down
31 changes: 31 additions & 0 deletions src/rime/language.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright RIME Developers
// Distributed under the BSD License
//
#ifndef RIME_LANGUAGE_H_
#define RIME_LANGUAGE_H_

#include <rime/common.h>

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 <class T, class U>
static bool intelligible(const T& t, const U& u) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有没有办法给 TU 类型增加一个必须有 language 方法的约束,类似 interface 或者 trait 机制?似乎直接使用 template 一旦 intelligible 方法使用出错编译器的报错就完全不能看了。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

講究。你當是寫 Rust?
雖然有,但我不想爲了類型檢查寫一堆模板庫函。滿足於代碼簡單可工作。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有道理

return t->language() && u->language() && *t->language() == *u->language();
}
};

} // namespace rime

#endif // RIME_LANGUAGE_H_