Skip to content

Commit 99b376e

Browse files
committed
Fix history bigram search when there's no path.
1 parent df855d8 commit 99b376e

3 files changed

Lines changed: 24 additions & 16 deletions

File tree

src/libime/core/historybigram.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ struct WeightedTrie {
9696
auto v = trie_.traverse(wordAndCode.first, pos);
9797
if (TrieType::isValid(v)) {
9898
result += v;
99+
} else if (TrieType::isNoPath(v)) {
100+
return 0;
99101
}
100102
const char separator[] = {wordCodeSeparator, '\0'};
101103
v = trie_.traverse(separator, pos);

src/libime/pinyin/pinyinprediction.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ PinyinPrediction::predict(const State &state,
4848

4949
if (lastEncodedPinyin.empty() || sentence.empty()) {
5050
auto result = Prediction::predictWithScore(state, sentence, maxSize);
51-
std::transform(result.begin(), result.end(),
52-
std::back_inserter(finalResult),
53-
[](std::pair<std::string, float> &value) {
54-
return std::make_pair(std::move(value.first),
55-
PinyinPredictionSource::Model);
56-
});
51+
std::ranges::transform(result, std::back_inserter(finalResult),
52+
[](std::pair<std::string, float> &value) {
53+
return std::make_pair(
54+
std::move(value.first),
55+
PinyinPredictionSource::Model);
56+
});
5757
return finalResult;
5858
}
5959

@@ -119,11 +119,9 @@ PinyinPrediction::predict(const State &state,
119119

120120
dup.insert(std::get<std::string>(newItem));
121121
intermedidateResult.push_back(std::move(newItem));
122-
std::push_heap(intermedidateResult.begin(),
123-
intermedidateResult.end(), cmp);
122+
std::ranges::push_heap(intermedidateResult, cmp);
124123
while (intermedidateResult.size() > maxSize) {
125-
std::pop_heap(intermedidateResult.begin(),
126-
intermedidateResult.end(), cmp);
124+
std::ranges::pop_heap(intermedidateResult, cmp);
127125
dup.erase(
128126
std::get<std::string>(intermedidateResult.back()));
129127
intermedidateResult.pop_back();

test/testpinyinprediction.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "libime/core/userlanguagemodel.h"
1111
#include "libime/pinyin/pinyindictionary.h"
1212
#include "libime/pinyin/pinyinencoder.h"
13+
#include "libime/pinyin/pinyinime.h"
1314
#include "libime/pinyin/pinyinprediction.h"
1415
#include "testdir.h"
1516

@@ -33,14 +34,21 @@ LogMessageBuilder &operator<<(LogMessageBuilder &log,
3334
} // namespace fcitx
3435

3536
int main() {
36-
UserLanguageModel model(LIBIME_BINARY_DIR "/data/sc.lm");
37-
PinyinDictionary dict;
38-
dict.load(PinyinDictionary::SystemDict,
39-
LIBIME_BINARY_DIR "/data/dict_sc.txt", PinyinDictFormat::Text);
37+
PinyinIME ime(
38+
std::make_unique<PinyinDictionary>(),
39+
std::make_unique<UserLanguageModel>(LIBIME_BINARY_DIR "/data/sc.lm"));
40+
ime.setNBest(2);
41+
ime.dict()->load(PinyinDictionary::SystemDict,
42+
LIBIME_BINARY_DIR "/data/sc.dict",
43+
PinyinDictFormat::Binary);
44+
ime.model()->history().addWithCode({{"", "JF"}});
45+
auto &model = *ime.model();
46+
auto &dict = *ime.dict();
4047

4148
PinyinPrediction prediction;
42-
prediction.setUserLanguageModel(&model);
43-
prediction.setPinyinDictionary(&dict);
49+
prediction.setUserLanguageModel(ime.model());
50+
prediction.setPinyinDictionary(ime.dict());
51+
4452
auto py = PinyinEncoder::encodeFullPinyin("zhong'guo");
4553
auto result = prediction.predict(model.nullState(), {"", "喜欢", "中国"},
4654
{py.data(), py.size()}, 20);

0 commit comments

Comments
 (0)