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
13 changes: 13 additions & 0 deletions src/libime/core/utils_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ inline int millisecondsTill(T t0) {
.count();
}

struct StringHash {
using hash_type = std::hash<std::string_view>;
using is_transparent = void;

std::size_t operator()(const char *str) const { return hash_type{}(str); }
std::size_t operator()(std::string_view str) const {
return hash_type{}(str);
}
std::size_t operator()(const std::string &str) const {
return hash_type{}(str);
}
};

} // namespace libime

#endif // _LIBIME_LIBIME_CORE_UTILS_P_H_
34 changes: 34 additions & 0 deletions src/libime/pinyin/pinyindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <vector>
#include <fcitx-utils/log.h>
#include <fcitx-utils/stringutils.h>
#include "pinyindata_p.h"
#include "pinyinencoder.h"

namespace libime {
Expand Down Expand Up @@ -152,6 +153,39 @@ getInnerSegment() {
return innerSegment;
}

const InnerSegmentMap &getInnerSegmentV2() {
static const InnerSegmentMap innerSegment = []() {
InnerSegmentMap innerSegmentV2;
for (const auto &[key, value] : getInnerSegment()) {
innerSegmentV2[key].push_back(value);
}

for (const auto &newItem : std::vector<
std::pair<std::string, std::pair<std::string, std::string>>>{
{"qiao", {"qia", "o"}},
{"niao", {"nia", "o"}},
{"liao", {"lia", "o"}},
{"zhuo", {"zhu", "o"}},
{"diao", {"dia", "o"}},
{"shao", {"sha", "o"}},
{"xiao", {"xia", "o"}},
{"zhua", {"zhu", "a"}},
{"shuo", {"shu", "o"}},
{"shua", {"shu", "a"}},
{"zhao", {"zha", "o"}},
{"jiao", {"jia", "o"}},
{"chuo", {"chu", "o"}},
{"chua", {"chu", "a"}},
{"chao", {"cha", "o"}},
}) {
innerSegmentV2[newItem.first].push_back(newItem.second);
}
return innerSegmentV2;
}();

return innerSegment;
}

inline bool operator==(const PinyinEntry &a, const PinyinEntry &b) {
return a.pinyin() == b.pinyin() && a.initial() == b.initial() &&
a.final() == b.final() && a.flags() == b.flags();
Expand Down
29 changes: 29 additions & 0 deletions src/libime/pinyin/pinyindata_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifndef _FCITX_LIBIME_PINYIN_PINYINDATA_P_H_
#define _FCITX_LIBIME_PINYIN_PINYINDATA_P_H_

#include <functional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "libime/core/utils_p.h"

namespace libime {

using InnerSegmentMap =
std::unordered_map<std::string,
std::vector<std::pair<std::string, std::string>>,
StringHash, std::equal_to<>>;

const std::unordered_map<std::string,
std::vector<std::pair<std::string, std::string>>,
StringHash, std::equal_to<>> &
getInnerSegmentV2();
} // namespace libime

#endif // _FCITX_LIBIME_PINYIN_PINYINDATA_P_H_
15 changes: 9 additions & 6 deletions src/libime/pinyin/pinyinencoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "libime/core/segmentgraph.h"
#include "pinyincorrectionprofile.h"
#include "pinyindata.h"
#include "pinyindata_p.h"
#include "shuangpinprofile.h"

namespace libime {
Expand Down Expand Up @@ -295,13 +296,15 @@ PinyinEncoder::parseUserPinyin(std::string userPinyin,
fuzzyFlags.test(PinyinFuzzyFlag::Inner)) ||
(nextPinyin.size() == 3 &&
flags.test(PinyinFuzzyFlag::InnerShort))) {
const auto &innerSegments = getInnerSegment();
auto iter = innerSegments.find(std::string(nextPinyin));
const auto &innerSegments = getInnerSegmentV2();
auto iter = innerSegments.find(nextPinyin);
if (iter != innerSegments.end()) {
result.addNext(top,
top + iter->second.first.size());
result.addNext(top + iter->second.first.size(),
top + nextSize[i]);
for (const auto &innerSeg : iter->second) {
result.addNext(top,
top + innerSeg.first.size());
result.addNext(top + innerSeg.first.size(),
top + nextSize[i]);
}
}
} else if (nextPinyin.size() == 2 &&
flags.test(PinyinFuzzyFlag::InnerShort) &&
Expand Down
4 changes: 4 additions & 0 deletions test/testpinyinencoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ int main() {

check("sangeren", PinyinFuzzyFlag::Inner, {"san", "ge", "ren"});

check("jiao", PinyinFuzzyFlag::Inner, {"jiao"});
check("jiao", PinyinFuzzyFlag::Inner, {"ji", "ao"});
check("jiao", PinyinFuzzyFlag::Inner, {"jia", "o"});

{
PinyinCorrectionProfile profile(BuiltinPinyinCorrectionProfile::Qwerty);
auto graph = PinyinEncoder::parseUserPinyin(
Expand Down
Loading