Skip to content
Merged
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.12)
project(libime VERSION 1.1.12)
project(libime VERSION 1.1.13)
set(LibIME_VERSION ${PROJECT_VERSION})

set(REQUIRED_FCITX_VERSION 5.1.13)
Expand Down
4 changes: 2 additions & 2 deletions src/libime/core/lattice.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class LIBIMECORE_EXPORT WordNode {

virtual ~WordNode() = default;
WordNode(WordNode &&other) noexcept(
std::is_nothrow_move_constructible<std::string>::value);
std::is_nothrow_move_constructible_v<std::string>);
WordNode &operator=(WordNode &&other) noexcept(
std::is_nothrow_move_assignable<std::string>::value);
std::is_nothrow_move_assignable_v<std::string>);

const std::string &word() const { return word_; }
WordIndex idx() const { return idx_; }
Expand Down
47 changes: 25 additions & 22 deletions src/libime/pinyin/pinyincontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <iterator>
#include <limits>
#include <memory>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -80,6 +81,7 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
mutable std::vector<SentenceResult> candidatesToCursor_;
mutable std::unordered_set<std::string> candidatesToCursorSet_;
std::vector<fcitx::ScopedConnection> conn_;
std::vector<WordNode> contextWords_;

size_t alignCursorToNextSegment() const {
FCITX_Q();
Expand Down Expand Up @@ -526,16 +528,19 @@ State PinyinContext::state() const {
FCITX_D();
auto *model = d->ime_->model();
State state = model->nullState();
if (!d->selected_.empty()) {
for (const auto &s : d->selected_) {
for (const auto &item : s) {
if (item.word_.word().empty()) {
continue;
}
State temp;
model->score(state, item.word_, temp);
state = std::move(temp);
for (const auto &word : d->contextWords_) {
State temp;
model->score(state, word, temp);
state = std::move(temp);
}
for (const auto &s : d->selected_) {
for (const auto &item : s) {
if (item.word_.word().empty()) {
continue;
}
State temp;
model->score(state, item.word_, temp);
state = std::move(temp);
}
}
return state;
Expand All @@ -552,21 +557,9 @@ void PinyinContext::update() {
d->clearCandidates();
} else {
size_t start = 0;
auto *model = d->ime_->model();
State state = model->nullState();
State state = this->state();
if (!d->selected_.empty()) {
start = d->selected_.back().back().offset_;

for (auto &s : d->selected_) {
for (auto &item : s) {
if (item.word_.word().empty()) {
continue;
}
State temp;
model->score(state, item.word_, temp);
state = std::move(temp);
}
}
}
SegmentGraph newGraph;
if (auto spProfile = d->matchState_.shuangpinProfile()) {
Expand Down Expand Up @@ -991,6 +984,16 @@ void PinyinContext::learn() {
}
}

void PinyinContext::setContextWords(
const std::vector<std::string> &contextWords) {
FCITX_D();
d->contextWords_.clear();
for (const auto &word : contextWords) {
d->contextWords_.push_back(
WordNode(word, d->ime_->model()->index(word)));
}
}

bool PinyinContext::learnWord() { return false; }

PinyinIME *PinyinContext::ime() const {
Expand Down
8 changes: 8 additions & 0 deletions src/libime/pinyin/pinyincontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <cstddef>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <unordered_set>
Expand Down Expand Up @@ -136,6 +137,13 @@ class LIBIMEPINYIN_EXPORT PinyinContext : public InputBuffer {
/// Opaque language model state.
State state() const;

/**
* Set context words for better prediction.
* @param contextWords The context words.
* @since 1.1.13
*/
void setContextWords(const std::vector<std::string> &contextWords);

protected:
bool typeImpl(const char *s, size_t length) override;

Expand Down
29 changes: 29 additions & 0 deletions test/testpinyincontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,34 @@ int main() {
FCITX_ASSERT(c.selectedWordsWithPinyin().size() == 1);
}

// Check that context words can change prediction.
{
{
c.clear();
c.type("ta");
size_t i = 0;
for (const auto &candidate : c.candidatesToCursor()) {
if (candidate.toString() == "她") {
break;
}
i++;
}
FCITX_ASSERT(i > 0) << i;
}
{
c.clear();
c.setContextWords({"他", "爱"});
c.type("ta");
size_t i = 0;
for (const auto &candidate : c.candidatesToCursor()) {
if (candidate.toString() == "她") {
break;
}
i++;
}
FCITX_ASSERT(i == 0) << i;
}
}

return 0;
}
Loading