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(single_char_filter): add char_only filter #891

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 63 additions & 2 deletions src/rime/gear/single_char_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//
#include <utf8.h>
#include <rime/candidate.h>
#include <rime/context.h>
#include <rime/engine.h>
#include <rime/schema.h>
#include <rime/translation.h>
#include <rime/gear/single_char_filter.h>
#include <rime/gear/translator_commons.h>
Expand Down Expand Up @@ -55,11 +58,69 @@ bool SingleCharFirstTranslation::Rearrange() {
return !cache_.empty();
}

SingleCharFilter::SingleCharFilter(const Ticket& ticket) : Filter(ticket) {}
class SingleCharOnlyTranslation : public Translation {
public:
SingleCharOnlyTranslation(an<Translation> translation);

bool Next() override;
an<Candidate> Peek() override;

private:
an<Translation> translation_;
an<Candidate> current_;
};

SingleCharOnlyTranslation::SingleCharOnlyTranslation(
an<Translation> translation)
: translation_(translation) {}
Copy link
Member

Choose a reason for hiding this comment

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

The library expects a Translation to be ready to query with Peek() or exhausted() in its initial state.
Not only in Next(), but also in the constructor, we need to locate the next available single-char candidate and set exhausted_ depending on whether there is any single-char candidate available. Usually I extract a function from Next and call it in both places.

The interface will be improved in r2ime. Migrating the class to C++ iterator seems to be a large change.

Copy link
Member Author

Choose a reason for hiding this comment

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

should be fixed. PTAL


an<Candidate> SingleCharOnlyTranslation::Peek() {
return current_;
}

bool SingleCharOnlyTranslation::Next() {
if (exhausted())
return false;
while (true) {
if (translation_->exhausted() || !translation_->Next()) {
set_exhausted(true);
current_.reset();
return false;
}
current_ = translation_->Peek();
if (unistrlen(current_->text()) == 1) {
return true;
}
}
assert(false); // unreachable
}

SingleCharFilter::SingleCharFilter(const Ticket& ticket) : Filter(ticket) {
if (name_space_ == "") {
name_space_ = "single_char_filter";
}
if (Config* config = engine_->schema()->config()) {
string type;
if (config->GetString(name_space_ + "/type", &type)) {
type_ = (type == "char_only") ? kCharOnly : kCharFirst;
} else {
type_ = kCharFirst;
}
config->GetString(name_space_ + "/option_name", &option_name_);
}
}

an<Translation> SingleCharFilter::Apply(an<Translation> translation,
CandidateList* candidates) {
return New<SingleCharFirstTranslation>(translation);
// for backward compatibility, always enable filter if no option_name is given
if (option_name_ != "" && !engine_->context()->get_option(option_name_)) {
return translation;
}
if (type_ == kCharFirst) {
return New<SingleCharFirstTranslation>(translation);
} else {
return New<SingleCharOnlyTranslation>(translation);
}
}

} // namespace rime
7 changes: 7 additions & 0 deletions src/rime/gear/single_char_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class SingleCharFilter : public Filter {

virtual an<Translation> Apply(an<Translation> translation,
CandidateList* candidates);

protected:
enum FilterType { kCharOnly, kCharFirst };

std::string name_space_;
std::string option_name_ = "";
FilterType type_ = kCharFirst;
};

} // namespace rime
Expand Down
Loading