Skip to content

Commit

Permalink
feat(language): shared user dictionary per language (Closes #184)
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Jul 9, 2018
1 parent b86b647 commit f9f9976
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 44 deletions.
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);
}

} // 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;
}

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) {
return t->language() && u->language() && *t->language() == *u->language();
}
};

} // namespace rime

#endif // RIME_LANGUAGE_H_

0 comments on commit f9f9976

Please sign in to comment.