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 all commits
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
71 changes: 69 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,75 @@ 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:
bool SkipToNextChar();

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

SingleCharOnlyTranslation::SingleCharOnlyTranslation(
an<Translation> translation)
: translation_(translation) {
SkipToNextChar();
}

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

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

bool SingleCharOnlyTranslation::Next() {
if (exhausted())
return false;
return SkipToNextChar();
}

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