Skip to content

Commit

Permalink
增加用户词典下的长词联想
Browse files Browse the repository at this point in the history
  • Loading branch information
siuze committed Feb 18, 2024
1 parent fca11ef commit 49fe060
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/rime/dict/user_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ bool UserDictEntryIterator::Next() {

UserDictionary::UserDictionary(const string& name, an<Db> db)
: name_(name), db_(db) {}
UserDictionary::UserDictionary(const string& name,
an<Db> db,
bool enable_completion)
: name_(name), db_(db), enable_completion_(enable_completion) {}

UserDictionary::~UserDictionary() {
if (loaded()) {
Expand Down Expand Up @@ -224,7 +228,14 @@ void UserDictionary::DfsLookup(const SyllableGraph& syll_graph,
if (!state->ForwardScan(prefix)) // reached the end of db
continue;
}
while (state->IsExactMatch(prefix)) { // 'b |e ' vs. 'b e \tBe'
while (
state->IsExactMatch(prefix) ||
(enable_completion_ && state->IsPrefixMatch(prefix) &&
state->code.size() > 2 &&
syll_graph.indices.size() ==
current_pos +
2)) { // 'b |e ' vs. 'b e \tBe'
// 当enable_completion_开启,且打出3个及以上音节且当前已经检查到最后一个编码时才允许加入长词联想预测候选项
DLOG(INFO) << "match found for '" << prefix << "'.";
state->RecruitEntry(end_pos);
if (!state->NextEntry()) // reached the end of db
Expand Down Expand Up @@ -499,7 +510,21 @@ UserDictionary* UserDictionaryComponent::Create(const string& dict_name,
}
return new UserDictionary(dict_name, db);
}

UserDictionary* UserDictionaryComponent::Create(const string& dict_name,
const string& db_class,
bool enable_completion_) {
auto db = db_pool_[dict_name].lock();
if (!db) {
auto component = Db::Require(db_class);
if (!component) {
LOG(ERROR) << "undefined db class '" << db_class << "'.";
return NULL;
}
db.reset(component->Create(dict_name));
db_pool_[dict_name] = db;
}
return new UserDictionary(dict_name, db, enable_completion_);
}
UserDictionary* UserDictionaryComponent::Create(const Ticket& ticket) {
if (!ticket.schema)
return NULL;
Expand All @@ -508,6 +533,9 @@ UserDictionary* UserDictionaryComponent::Create(const Ticket& ticket) {
config->GetBool(ticket.name_space + "/enable_user_dict", &enable_user_dict);
if (!enable_user_dict)
return NULL;
bool enable_completion_ = false;
config->GetBool(ticket.name_space + "/enable_completion",
&enable_completion_);
string dict_name;
if (config->GetString(ticket.name_space + "/user_dict", &dict_name)) {
// user specified name
Expand All @@ -524,7 +552,7 @@ UserDictionary* UserDictionaryComponent::Create(const Ticket& ticket) {
// user specified db class
}
// obtain userdb object
return Create(dict_name, db_class);
return Create(dict_name, db_class, enable_completion_);
}

} // namespace rime
5 changes: 5 additions & 0 deletions src/rime/dict/user_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct Ticket;
class UserDictionary : public Class<UserDictionary, const Ticket&> {
public:
UserDictionary(const string& name, an<Db> db);
UserDictionary(const string& name, an<Db> db, bool enable_completion);
virtual ~UserDictionary();

void Attach(const an<Table>& table, const an<Prism>& prism);
Expand Down Expand Up @@ -100,13 +101,17 @@ class UserDictionary : public Class<UserDictionary, const Ticket&> {
an<Prism> prism_;
TickCount tick_ = 0;
time_t transaction_time_ = 0;
bool enable_completion_ = false;
};

class UserDictionaryComponent : public UserDictionary::Component {
public:
UserDictionaryComponent();
UserDictionary* Create(const Ticket& ticket);
UserDictionary* Create(const string& dict_name, const string& db_class);
UserDictionary* Create(const string& dict_name,
const string& db_class,
bool enable_completion_);

private:
map<string, weak<Db>> db_pool_;
Expand Down

0 comments on commit 49fe060

Please sign in to comment.