diff --git a/README.md b/README.md index 6c83b1a..6f22d07 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,15 @@ ## 简介 +借助 Rime 输入法, 让 Gboard 的输入体验媲美旧时的 Google Pinyin. + 本脚本可将 Rime `xxx.userdb.txt` (测试于 [Weasel](https://github.com/rime/weasel)) 转换为 Gboard `PersonalDictionary.zip` 格式, 便于将词库导入 Gboard. +## 适用输入方案 + +- 全拼 (luna_pinyin) +- 五笔 (wubi86) + ## 脚本依赖 - Python 3.8-3.10 @@ -22,7 +29,7 @@ 2. 运行脚本, 按照提示将你的待转换的 `userdb.txt` 拖拽入命令行. -3. 根据指引输入文件文本繁简类型. +3. 根据指引, 确认输入方案, 输入文件文本繁简类型. 4. 回车, 等待数秒, `.zip` 压缩文件将自动生成. @@ -37,10 +44,12 @@ ## License +```License Copyright 2023 GC Chen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +``` diff --git a/main.py b/main.py index 7928ca1..c8f2f0f 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,14 @@ def read_file(rime_userdb_path: str): userdb_data.append(line) return userdb_data +# 判断输入方案 +def get_scheme(userdb_data: list): + scheme_type = "" + if "luna_pinyin" in userdb_data[1]: + scheme_type = "luna_pinyin" + elif "wubi86" in userdb_data[1]: + scheme_type = "wubi86" + return scheme_type # 通过 opencc 繁体转简体 def trans_to_simp(input_data: list): @@ -36,12 +44,27 @@ def simp_to_trans(input_data: list): # 通过正则匹配自定义短语 -def find_words(input_data: list): +def find_words(scheme_type: str, input_data: list): result = [] - pattern = re.compile(r'(.*?)\t(.*?)\t') - for line in tqdm(input_data, desc="正则匹配短语"): - res = pattern.findall(line) - result.append(res) + + if scheme_type == "luna_pinyin": + pattern = re.compile(r'(.*?)\t(.*?)\t') + for line in tqdm(input_data, desc="正则匹配短语"): + res = pattern.findall(line) + result.append(res) + elif scheme_type == "wubi86": + pattern = re.compile(r'(.*?)\t(.*?)\t') + for line in tqdm(input_data, desc="正则匹配短语"): + res = pattern.findall(line) + new_res = () + if len(res) == 1: + res_list = list(res) + new_key = res_list[0][0].replace('\x7fenc\x1f', '') + new_res = [(new_key, res_list[0][1])] + else: + new_res = res + result.append(new_res) + return result @@ -75,6 +98,17 @@ def main(): # 获取 rime userdb.txt 用户词典 userdb_data = read_file(rime_userdb_path) + # 判断输入方案 + scheme_type = get_scheme(userdb_data=userdb_data) + if scheme_type != "": + scheme_result = input("识别为 {} 词库, 输入 0 -- 正确, 1 -- 错误 以继续: ".format(scheme_type)) + if scheme_type == "" or scheme_result == 1: + input_scheme_type = input("输入 Rime userdb.txt 方案类型 (0 -- luna_pinyin, 1 -- wubi86): ") + if input_scheme_type == 0: + scheme_type == "luna_pinyin" + elif input_scheme_type == 1: + scheme_type = "wubi86" + while True: input_format = ["0", "1"] userdb_type = input("输入 Rime userdb.txt 简繁类型 (0 -- 简体, 1 -- 繁体): ") @@ -97,7 +131,7 @@ def main(): userdb_data = trans_to_simp(userdb_data) # 匹配自定义短语 - words_list = find_words(userdb_data) + words_list = find_words(scheme_type, userdb_data) # 新建列表存储短语 new_words_list = generate_gboard_format_data(words_list) @@ -105,6 +139,7 @@ def main(): # 生成 gboard 个人词典 generate_gboardDic(words_list=new_words_list) + input("转换完成, 回车退出.") if __name__ == '__main__': main()