6 #include "pinyincorrectionprofile.h" 10 #include <unordered_map> 13 #include <fcitx-utils/macros.h> 14 #include "pinyindata.h" 15 #include "pinyinencoder.h" 25 std::unordered_map<char, std::vector<char>>
26 mappingFromRows(
const std::vector<std::string> &rows) {
27 std::unordered_map<char, std::vector<char>> result;
28 for (
const auto &row : rows) {
29 for (
size_t i = 0; i < row.size(); i++) {
30 std::vector<char> items;
32 items.push_back(row[i - 1]);
34 if (i + 1 < row.size()) {
35 items.push_back(row[i + 1]);
37 result[row[i]] = std::move(items);
43 std::unordered_map<char, std::vector<char>>
44 getProfileMapping(BuiltinPinyinCorrectionProfile profile) {
46 case BuiltinPinyinCorrectionProfile::Qwerty:
47 return mappingFromRows({
"qwertyuiop",
"asdfghjkl",
"zxcvbnm"});
57 std::unordered_map<char, std::vector<char>> correctionMap_;
61 BuiltinPinyinCorrectionProfile profile)
65 const std::unordered_map<
char, std::vector<char>> &mapping)
68 d->correctionMap_ = mapping;
70 d->pinyinMap_ = getPinyinMapV2();
71 if (mapping.empty()) {
75 std::vector<PinyinEntry> newEntries;
76 for (
const auto &item : d->pinyinMap_) {
77 for (
size_t i = 0; i < item.pinyin().size(); i++) {
78 auto chr = item.pinyin()[i];
79 auto swap = mapping.find(chr);
80 if (swap == mapping.end() || swap->second.empty()) {
83 auto newEntry = item.pinyin();
84 for (
auto sub : swap->second) {
87 PinyinEntry(newEntry.data(), item.initial(), item.final(),
88 item.flags() | PinyinFuzzyFlag::Correction));
93 for (
const auto &newEntry : newEntries) {
94 d->pinyinMap_.insert(newEntry);
98 PinyinCorrectionProfile::~PinyinCorrectionProfile() =
default;
102 return d->pinyinMap_;
105 const std::unordered_map<char, std::vector<char>> &
108 return d->correctionMap_;
PinyinCorrectionProfile(BuiltinPinyinCorrectionProfile profile)
Construct the profile based on builtin layout.
const PinyinMap & pinyinMap() const
Return the updated pinyin map.
Class that holds updated Pinyin correction mapping based on correction mapping.
const std::unordered_map< char, std::vector< char > > & correctionMap() const
Return the correction mapping.