Skip to content

Commit 967eda2

Browse files
committed
add full support for single han char
1 parent c2930d5 commit 967eda2

File tree

4 files changed

+97
-35
lines changed

4 files changed

+97
-35
lines changed

src/hook/ime_hook.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <UIAnimation.h>
1414
#include <UIAutomationClient.h>
1515
#include <atlbase.h>
16+
#include <winuser.h>
1617

1718
#include "../ui/cand_ui.h"
1819
#include "./key_handle_func_lib.h"
@@ -36,6 +37,7 @@ sqlite3 *db;
3637
std::unordered_map<std::string, std::unordered_set<std::string>> helpCode3Map; // 3 码
3738
std::unordered_map<std::string, std::unordered_set<std::string>> helpCodeMap; // 4 码
3839
std::unordered_map<std::string, std::string> helpCodeUsingHanKey; // 汉字作为键
40+
bool EntireHelpCodeFlag = 0;
3941

4042
// 整体输入法状态的一个控制
4143
// 默认是 0,也就是英文状态
@@ -279,31 +281,62 @@ LRESULT CALLBACK KBDHook(int nCode, WPARAM wParam, LPARAM lParam)
279281
}
280282

281283
/*
282-
处理 + 和 - 翻页
284+
处理 + 和 - 和 tab 翻页
283285
*/
284-
// +
285-
if (s->vkCode == VK_OEM_PLUS || s->vkCode == VK_TAB)
286+
// tab
287+
if (s->vkCode == VK_TAB)
286288
{
287289
// 快捷键不能占用
288290
if (fCtrlDown)
289291
{
290292
break;
291293
}
292-
if (s->vkCode == VK_OEM_PLUS && fShiftDown)
294+
if (candidateVec.size() > 1 && pageNo == 0)
293295
{
294-
sendStringToCursor(converter.from_bytes("+"));
296+
EntireHelpCodeFlag = 1;
297+
}
298+
if (candidateVec.size() > 1 && pageNo < candidateVec.size() - 1)
299+
{
300+
pageNo += 1;
301+
curCandidateVec = candidateVec[pageNo];
302+
printOneDVector(curCandidateVec);
295303
return 1;
296304
}
305+
if (pageNo == candidateVec.size() - 1)
306+
{
307+
return 1; // 吞掉
308+
}
309+
sendStringToCursor(converter.from_bytes("\t"));
310+
}
311+
312+
// +
313+
if (s->vkCode == VK_OEM_PLUS)
314+
{
315+
// 快捷键不能占用
316+
if (fCtrlDown)
317+
{
318+
break;
319+
}
297320
if (candidateVec.size() > 1 && pageNo < candidateVec.size() - 1)
298321
{
299-
// std::cout << "raw pageNo: " << pageNo << '\t' << "raw
300-
// candSize: " << candidateVec.size() << '\n';
301322
pageNo += 1;
302-
// std::cout << "pageNo: " << pageNo << '\n';
303323
curCandidateVec = candidateVec[pageNo];
304324
printOneDVector(curCandidateVec);
305325
return 1;
306326
}
327+
if (pageNo == candidateVec.size() - 1)
328+
{
329+
return 1; // 当前是最后一页,就吞掉
330+
}
331+
if (fShiftDown)
332+
{
333+
sendStringToCursor(converter.from_bytes("+"));
334+
}
335+
else
336+
{
337+
sendStringToCursor(converter.from_bytes("="));
338+
}
339+
return 1;
307340
}
308341

309342
// -
@@ -317,12 +350,11 @@ LRESULT CALLBACK KBDHook(int nCode, WPARAM wParam, LPARAM lParam)
317350
if (candidateVec.size() > 1 && pageNo > 0)
318351
{
319352
pageNo -= 1;
320-
// std::cout << "pageNo: " << pageNo << '\n';
321353
curCandidateVec = candidateVec[pageNo];
322354
printOneDVector(curCandidateVec);
323355
return 1;
324356
}
325-
if (candidateVec.size() >= 1 && pageNo == 0)
357+
if (candidateVec.size() >= 1 && pageNo == 0) // 当前是第一页,就吞掉
326358
{
327359
curCandidateVec = candidateVec[pageNo];
328360
printOneDVector(curCandidateVec);
@@ -573,7 +605,6 @@ void sendStringToCursor(const std::wstring &str)
573605
input.ki.time = 0;
574606
input.ki.dwFlags = KEYEVENTF_UNICODE;
575607
SendInput(1, &input, sizeof(INPUT));
576-
577608
input.ki.dwFlags |= KEYEVENTF_KEYUP;
578609
SendInput(1, &input, sizeof(INPUT));
579610
}

src/hook/ime_hook.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ extern std::unordered_map<std::string, std::string> helpCodeUsingHanKey;
3939
extern bool IMEState;
4040
extern std::string IMEStateToast;
4141

42+
// 是否开启完整的辅助码的支持,默认是 0,也就是不支持
43+
extern bool EntireHelpCodeFlag;
44+
4245
// 造词的标志:0 -> 否,1 -> 是
4346
extern int CREATE_WORD_FLAG;
4447
// 造词需要用到的字符串

src/hook/key_handle_func_lib.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -329,24 +329,11 @@ void handleEnterByChars()
329329

330330
void commitCandidate(char c, int canSize, int cInt)
331331
{
332-
// 在控制台打印测试
333-
// std::cout << curCandidateVec[cInt - 1].first << '\n';
334332
// 输送到光标所在的地方
335333
std::wstring wstr = converter.from_bytes(curCandidateVec[cInt - 1].first);
336334
sendStringToCursor(wstr);
337-
// fany: test
338335
std::string curPinyin(charVec.begin(), charVec.end());
339-
// std::cout << curPinyin << '\t' << curCandidateVec[cInt - 1].first << '\t'
340-
// << curCandidateVec[cInt - 1].second <<
341-
// '\n'; 更新权重
342336
int index = 0;
343-
// if (candidateVec[0].size() > 3) {
344-
// index = 3;
345-
// } else if (candidateVec[0].size() > 2) {
346-
// index = 2;
347-
// } else if (candidateVec[0].size() > 1) {
348-
// index = 1;
349-
// }
350337
// 此处是更新一个更新权重的问题
351338
int cIntOfAllCand = 8 * pageNo + cInt;
352339
if (cIntOfAllCand - 1 > 2)
@@ -364,15 +351,7 @@ void commitCandidate(char c, int canSize, int cInt)
364351
// 我取的应该是所有的查询结果里面的最前面的呀,嗯,暂时又没发现问题了。
365352
long weight = candidateVec[0][index].second + 1;
366353
updateItemWeightInDb(db, curPinyin, curCandidateVec[cInt - 1].first, weight);
367-
368-
// 上屏了之后要把 candidateVec 给清除掉
369-
// candidateVec.clear();
370-
// curCandidateVec.clear();
371-
// charVec.clear();
372-
// pageNo = 0;
373-
// fanyHideWindow(gHwnd);
374354
std::string curStr = curCandidateVec[cInt - 1].first;
375-
// std::string hanKey(charVec.begin(), charVec.end());
376355
clearCandRelative(curStr, curPinyin);
377356
}
378357

@@ -466,18 +445,25 @@ void clearCandRelative(std::string curStr, std::string hanKey)
466445
candidateVec.clear();
467446
curCandidateVec.clear();
468447
pageNo = 0;
448+
EntireHelpCodeFlag = 0; // 完整辅助码支持状态清零
469449
// 要注意,这里一个汉字的 size 是 3 或者 4!
470450
// 候选框的 (汉字的数量 * 2) 小于拼音字符的数量
471451
// 以及,三码不造字、五码也不造字
472452
// TODO: 目前这里只能插入三个字节的汉字,对于 unicode 表中靠后的一些字暂时没去处理
473-
if (calc_han_count(curStr) * 2 < hanKey.size() && hanKey.size() != 3 && hanKey.size() != 5)
453+
if (calc_han_count(curStr) * 2 < hanKey.size() && hanKey.size() != 3 && hanKey.size() != 5 && hanKey.size() != 4)
474454
{
475455
committedChars.push_back(curStr);
476456
committedPinyin.push_back(hanKey);
477457
// 这个擦除以后可以自动把 size 变成缩小后的程度
478458
charVec.erase(charVec.begin(), charVec.begin() + curStr.size() / 3 * 2);
479459
handleAlphaByChars();
480460
}
461+
else if (hanKey.size() == 4 && EntireHelpCodeFlag)
462+
{
463+
// 存储拼音的 vector 也要清掉
464+
charVec.clear();
465+
fanyHideWindow(gHwnd);
466+
}
481467
else
482468
{
483469
if (committedChars.size() > 0)

src/sqlite/sqlite_wrapper.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ykgdviiidoucgezifuneqiuiwoyebuvidcdenividcmawovfdebvidcdekeuiv(62)
88
#include "./sqlite_wrapper.h"
99
#include "../hook/ime_hook.h"
1010
#include "../utils/han_char_util.h"
11+
#include "../hook/ime_hook.h"
1112

1213
#include <algorithm>
1314
#include <unordered_set>
@@ -509,6 +510,7 @@ std::vector<std::pair<std::string, long>> queryThreeChars(sqlite3 *db, std::stri
509510

510511
/*
511512
查询 4 个字符
513+
支持辅助码
512514
参数:
513515
pinyin: string
514516
返回值:vector<vector<pair<string, long>>>
@@ -517,8 +519,46 @@ std::vector<std::pair<std::string, long>> queryThreeChars(sqlite3 *db, std::stri
517519
*/
518520
std::vector<std::pair<std::string, long>> queryFourChars(sqlite3 *db, std::string pinyin)
519521
{
520-
// std::string pinyin02 = pinyin.substr(0, 1); // 切第一个字符
521522
std::string pinyin02 = pinyin.substr(0, 2); // 切前两个字符
523+
std::string pinyin03 = pinyin.substr(2, 2); // 切后两个字符
524+
if (EntireHelpCodeFlag)
525+
{
526+
/* pageNo = 0; // 确定使用辅助码之后,这里要将 pageNo 状态归零 */
527+
std::vector<std::pair<std::string, long>> resVec;
528+
std::string tblName = "fullpinyinsimple";
529+
std::string querySQL = "select * from " + tblName + " where key = '" + pinyin02 + "' order by weight desc";
530+
int result;
531+
char *errMsg = nullptr;
532+
int itemCount = 0;
533+
UserData userData{itemCount, resVec};
534+
// 查询
535+
result = sqlite3_exec(db, querySQL.c_str(), queryPinyinCallback, &userData, &errMsg);
536+
// std::cout << "itemCnt = " << itemCount << '\n';
537+
if (result)
538+
{
539+
// Todo: 日志
540+
std::cout << "query error!" << '\n';
541+
}
542+
// 根据辅助码进行过滤
543+
// 过滤出不在键集合中的数据
544+
auto cur_it = helpCodeMap.find(pinyin03);
545+
if (cur_it != helpCodeMap.end())
546+
{
547+
std::unordered_set<std::string> cur_set = helpCodeMap.find(pinyin03)->second;
548+
// 过滤出长度为一个汉字且在集合中的数据
549+
auto isFiltered = [&](const std::pair<std::string, long> &element) {
550+
// 检查长度是否为一个汉字
551+
if (element.first.size() == 3 || element.first.size() == 4)
552+
{ // 汉字通常占3个字符
553+
// 检查值是否在集合中
554+
return cur_set.find(element.first) == cur_set.end();
555+
}
556+
return false; // 如果长度不是一个汉字,保留元素
557+
};
558+
resVec.erase(std::remove_if(resVec.begin(), resVec.end(), isFiltered), resVec.end());
559+
}
560+
return resVec;
561+
}
522562
std::vector<std::pair<std::string, long>> resVec;
523563
std::string tblName = "fullpinyinsimple";
524564
std::string querySQL = "select * from " + tblName + " where key = '" + pinyin + "' order by weight desc limit 400";
@@ -545,8 +585,9 @@ std::vector<std::pair<std::string, long>> queryFourChars(sqlite3 *db, std::strin
545585
// Todo: 日志
546586
std::cout << "query error!" << '\n';
547587
}
588+
548589
return resVec;
549-
} // 你好烦
590+
} // 你好烦
550591

551592
/*
552593
查询 5 个字符
@@ -1547,6 +1588,7 @@ std::vector<std::pair<std::string, long>> querySixteenChars(sqlite3 *db, std::st
15471588
*/
15481589
std::vector<std::vector<std::pair<std::string, long>>> queryCharsInPage(sqlite3 *db, std::string hankey)
15491590
{
1591+
pageNo = 0; // 每一次作新的查询的时候,应该将 pageNo 清零
15501592
std::string pinyin = hankey;
15511593
for (char &c : pinyin)
15521594
{

0 commit comments

Comments
 (0)