Skip to content

Commit

Permalink
feat(single_char_filter): add char_only filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ksqsf committed Jun 10, 2024
1 parent a735e42 commit 60e0610
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
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) {}

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

0 comments on commit 60e0610

Please sign in to comment.