Skip to content

Commit bb8a61a

Browse files
authored
Add API to allow set context words for pinyin context. (#113)
1 parent 1bae691 commit bb8a61a

File tree

5 files changed

+65
-25
lines changed

5 files changed

+65
-25
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.12)
2-
project(libime VERSION 1.1.12)
2+
project(libime VERSION 1.1.13)
33
set(LibIME_VERSION ${PROJECT_VERSION})
44

55
set(REQUIRED_FCITX_VERSION 5.1.13)

src/libime/core/lattice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class LIBIMECORE_EXPORT WordNode {
6868

6969
virtual ~WordNode() = default;
7070
WordNode(WordNode &&other) noexcept(
71-
std::is_nothrow_move_constructible<std::string>::value);
71+
std::is_nothrow_move_constructible_v<std::string>);
7272
WordNode &operator=(WordNode &&other) noexcept(
73-
std::is_nothrow_move_assignable<std::string>::value);
73+
std::is_nothrow_move_assignable_v<std::string>);
7474

7575
const std::string &word() const { return word_; }
7676
WordIndex idx() const { return idx_; }

src/libime/pinyin/pinyincontext.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <iterator>
1212
#include <limits>
1313
#include <memory>
14+
#include <span>
1415
#include <stdexcept>
1516
#include <string>
1617
#include <string_view>
@@ -80,6 +81,7 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
8081
mutable std::vector<SentenceResult> candidatesToCursor_;
8182
mutable std::unordered_set<std::string> candidatesToCursorSet_;
8283
std::vector<fcitx::ScopedConnection> conn_;
84+
std::vector<WordNode> contextWords_;
8385

8486
size_t alignCursorToNextSegment() const {
8587
FCITX_Q();
@@ -526,16 +528,19 @@ State PinyinContext::state() const {
526528
FCITX_D();
527529
auto *model = d->ime_->model();
528530
State state = model->nullState();
529-
if (!d->selected_.empty()) {
530-
for (const auto &s : d->selected_) {
531-
for (const auto &item : s) {
532-
if (item.word_.word().empty()) {
533-
continue;
534-
}
535-
State temp;
536-
model->score(state, item.word_, temp);
537-
state = std::move(temp);
531+
for (const auto &word : d->contextWords_) {
532+
State temp;
533+
model->score(state, word, temp);
534+
state = std::move(temp);
535+
}
536+
for (const auto &s : d->selected_) {
537+
for (const auto &item : s) {
538+
if (item.word_.word().empty()) {
539+
continue;
538540
}
541+
State temp;
542+
model->score(state, item.word_, temp);
543+
state = std::move(temp);
539544
}
540545
}
541546
return state;
@@ -552,21 +557,9 @@ void PinyinContext::update() {
552557
d->clearCandidates();
553558
} else {
554559
size_t start = 0;
555-
auto *model = d->ime_->model();
556-
State state = model->nullState();
560+
State state = this->state();
557561
if (!d->selected_.empty()) {
558562
start = d->selected_.back().back().offset_;
559-
560-
for (auto &s : d->selected_) {
561-
for (auto &item : s) {
562-
if (item.word_.word().empty()) {
563-
continue;
564-
}
565-
State temp;
566-
model->score(state, item.word_, temp);
567-
state = std::move(temp);
568-
}
569-
}
570563
}
571564
SegmentGraph newGraph;
572565
if (auto spProfile = d->matchState_.shuangpinProfile()) {
@@ -991,6 +984,16 @@ void PinyinContext::learn() {
991984
}
992985
}
993986

987+
void PinyinContext::setContextWords(
988+
const std::vector<std::string> &contextWords) {
989+
FCITX_D();
990+
d->contextWords_.clear();
991+
for (const auto &word : contextWords) {
992+
d->contextWords_.push_back(
993+
WordNode(word, d->ime_->model()->index(word)));
994+
}
995+
}
996+
994997
bool PinyinContext::learnWord() { return false; }
995998

996999
PinyinIME *PinyinContext::ime() const {

src/libime/pinyin/pinyincontext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <cstddef>
1010
#include <memory>
11+
#include <span>
1112
#include <string>
1213
#include <string_view>
1314
#include <unordered_set>
@@ -136,6 +137,13 @@ class LIBIMEPINYIN_EXPORT PinyinContext : public InputBuffer {
136137
/// Opaque language model state.
137138
State state() const;
138139

140+
/**
141+
* Set context words for better prediction.
142+
* @param contextWords The context words.
143+
* @since 1.1.13
144+
*/
145+
void setContextWords(const std::vector<std::string> &contextWords);
146+
139147
protected:
140148
bool typeImpl(const char *s, size_t length) override;
141149

test/testpinyincontext.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,34 @@ int main() {
246246
FCITX_ASSERT(c.selectedWordsWithPinyin().size() == 1);
247247
}
248248

249+
// Check that context words can change prediction.
250+
{
251+
{
252+
c.clear();
253+
c.type("ta");
254+
size_t i = 0;
255+
for (const auto &candidate : c.candidatesToCursor()) {
256+
if (candidate.toString() == "") {
257+
break;
258+
}
259+
i++;
260+
}
261+
FCITX_ASSERT(i > 0) << i;
262+
}
263+
{
264+
c.clear();
265+
c.setContextWords({"", ""});
266+
c.type("ta");
267+
size_t i = 0;
268+
for (const auto &candidate : c.candidatesToCursor()) {
269+
if (candidate.toString() == "") {
270+
break;
271+
}
272+
i++;
273+
}
274+
FCITX_ASSERT(i == 0) << i;
275+
}
276+
}
277+
249278
return 0;
250279
}

0 commit comments

Comments
 (0)