Skip to content

Commit

Permalink
feat(punctuator): translate digit separator
Browse files Browse the repository at this point in the history
supporting full-shape form
  • Loading branch information
lotem committed Jan 17, 2025
1 parent a6a4423 commit 14ccab0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
52 changes: 33 additions & 19 deletions src/rime/gear/punctuator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ Punctuator::Punctuator(const Ticket& ticket) : Processor(ticket) {
config_.LoadConfig(engine_);
}

static bool punctuation_is_translated(Context* ctx) {
static bool punctuation_is_translated(Context* ctx, const string& tag) {
Composition& comp = ctx->composition();
if (comp.empty() || !comp.back().HasTag("punct")) {
if (comp.empty() || !comp.back().HasTag(tag)) {
return false;
}
auto cand = comp.back().GetSelectedCandidate();
Expand Down Expand Up @@ -79,7 +79,8 @@ static bool is_after_number(Context* ctx) {

static bool is_after_digit_separator(Context* ctx) {
const auto& comp = ctx->composition();
return !comp.empty() && comp.back().HasTag("punct_number");
return !comp.empty() && comp[0].HasTag("punct_number") &&
comp[0].length == ctx->input().length();
}

ProcessResult Punctuator::ProcessKeyEvent(const KeyEvent& key_event) {
Expand Down Expand Up @@ -110,29 +111,33 @@ ProcessResult Punctuator::ProcessKeyEvent(const KeyEvent& key_event) {
if (AlternatePunct(key, punct_definition)) {
return kAccepted;
}
if (ToggleNumberMode(key) || ctx->PushInput(ch)) {
if (punctuation_is_translated(ctx)) {
if (ToggleNumberMode(ch) || ctx->PushInput(ch)) {
if (punctuation_is_translated(ctx, "punct")) {
ConfirmUniquePunct(punct_definition) ||
AutoCommitPunct(punct_definition) || PairPunct(punct_definition);
}
}
return kAccepted;
}

bool Punctuator::ToggleNumberMode(const string& key) {
bool Punctuator::ToggleNumberMode(char ch) {
Context* ctx = engine_->context();
if (ctx->input() == key) {
Composition& comp = ctx->composition();
if (!comp.empty()) {
Segment& segment(comp.back());
if (segment.HasTag("punct_number")) {
segment.tags.erase("punct_number");
segment.tags.insert("punct");
segment.status = Segment::kVoid;
DLOG(INFO) << "exit number mode, key = " << key;
ctx->update_notifier()(ctx);
return true;
}
Composition& comp = ctx->composition();
if (comp.empty()) {
if (is_digit_separator(ch) && is_after_number(ctx)) {
ctx->PushInput(ch) && punctuation_is_translated(ctx, "punct_number") &&
comp.Forward();
return true;
}
} else if (ctx->input() == string(1, ch)) {
Segment& segment = comp[0];
if (segment.HasTag("punct_number")) {
segment.tags.erase("punct_number");
segment.tags.insert("punct");
segment.status = Segment::kVoid;
DLOG(INFO) << "exit number mode, key = " << ch;
ctx->ReopenPreviousSegment();
return true;
}
}
return false;
Expand Down Expand Up @@ -234,7 +239,8 @@ bool PunctSegmentor::Proceed(Segmentation* segmentation) {
return false; // exclusive
}

PunctTranslator::PunctTranslator(const Ticket& ticket) : Translator(ticket) {
PunctTranslator::PunctTranslator(const Ticket& ticket)
: Translator(ticket), formatter_(ticket) {
const bool load_symbols = true;
config_.LoadConfig(engine_, load_symbols);
}
Expand Down Expand Up @@ -282,6 +288,14 @@ an<Candidate> CreatePunctCandidate(const string& punct,

an<Translation> PunctTranslator::Query(const string& input,
const Segment& segment) {
if (segment.HasTag("punct_number")) {
if (input.length() == 1 && is_digit_separator(input[0])) {
string punct = input;
formatter_.Format(&punct);
return New<UniqueTranslation>(CreatePunctCandidate(punct, segment));
}
return nullptr;
}
if (!segment.HasTag("punct"))
return nullptr;
// sync with full_shape option
Expand Down
4 changes: 3 additions & 1 deletion src/rime/gear/punctuator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <rime/processor.h>
#include <rime/segmentor.h>
#include <rime/translator.h>
#include <rime/gear/shape.h>

namespace rime {

Expand All @@ -35,7 +36,7 @@ class Punctuator : public Processor {
virtual ProcessResult ProcessKeyEvent(const KeyEvent& key_event);

protected:
bool ToggleNumberMode(const string& key);
bool ToggleNumberMode(char ch);
bool AlternatePunct(const string& key, const an<ConfigItem>& definition);
bool ConfirmUniquePunct(const an<ConfigItem>& definition);
bool AutoCommitPunct(const an<ConfigItem>& definition);
Expand Down Expand Up @@ -74,6 +75,7 @@ class PunctTranslator : public Translator {
const Segment& segment,
const an<ConfigMap>& definition);

ShapeFormatter formatter_;
PunctConfig config_;
};

Expand Down

0 comments on commit 14ccab0

Please sign in to comment.