Skip to content

Commit

Permalink
feat(engine): translate zero-length prediction
Browse files Browse the repository at this point in the history
allow zero-length segment to be translated.
add `placeholder` tag to the segment to prevent it from being trimmed.
  • Loading branch information
lotem authored and fxliang committed Feb 2, 2024
1 parent a3bc943 commit 62e0f25
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/rime/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ void ConcreteEngine::Compose(Context* ctx) {
}
CalculateSegmentation(&comp);
TranslateSegments(&comp);
DLOG(INFO) << "composition: " << comp.GetDebugText();
DLOG(INFO) << "composition: [" << comp.GetDebugText() << "]";
}

void ConcreteEngine::CalculateSegmentation(Segmentation* segments) {
DLOG(INFO) << "CalculateSegmentation, segments: " << segments->size()
<< ", finished? " << segments->HasFinishedSegmentation();
while (!segments->HasFinishedSegmentation()) {
size_t start_pos = segments->GetCurrentStartPosition();
size_t end_pos = segments->GetCurrentEndPosition();
Expand All @@ -189,20 +191,22 @@ void ConcreteEngine::CalculateSegmentation(Segmentation* segments) {
segments->Forward();
}
// start an empty segment only at the end of a confirmed composition.
segments->Trim();
if (!segments->empty() && !segments->back().HasTag("placeholder"))
segments->Trim();
if (!segments->empty() && segments->back().status >= Segment::kSelected)
segments->Forward();
}

void ConcreteEngine::TranslateSegments(Segmentation* segments) {
DLOG(INFO) << "TranslateSegments: " << *segments;
for (Segment& segment : *segments) {
DLOG(INFO) << "segment [" << segment.start << ", " << segment.end
<< "), status: " << segment.status;
if (segment.status >= Segment::kGuess)
continue;
size_t len = segment.end - segment.start;
if (len == 0)
continue;
string input = segments->input().substr(segment.start, len);
DLOG(INFO) << "translating segment: " << input;
DLOG(INFO) << "translating segment: [" << input << "]";
auto menu = New<Menu>();
for (auto& translator : translators_) {
auto translation = translator->Query(input, segment);
Expand Down
2 changes: 1 addition & 1 deletion tools/rime_api_console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void print_menu(RimeMenu* menu) {
}

void print_context(RimeContext* context) {
if (context->composition.length > 0) {
if (context->composition.length > 0 || context->menu.num_candidates > 0) {
print_composition(&context->composition);
} else {
printf("(not composing)\n");
Expand Down

0 comments on commit 62e0f25

Please sign in to comment.