Skip to content

Commit

Permalink
Merge commit 'cb5a99e82a43c08dd7c13af6ec550f228ebb00a0'
Browse files Browse the repository at this point in the history
  • Loading branch information
cgcel committed Aug 20, 2023
2 parents bb37911 + cb5a99e commit 83d58fe
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +29,7 @@

2. 运行脚本, 按照提示将你的待转换的 `userdb.txt` 拖拽入命令行.

3. 根据指引输入文件文本繁简类型.
3. 根据指引, 确认输入方案, 输入文件文本繁简类型.

4. 回车, 等待数秒, `.zip` 压缩文件将自动生成.

Expand All @@ -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.
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.
```
47 changes: 41 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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


Expand Down Expand Up @@ -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 -- 繁体): ")
Expand All @@ -97,14 +131,15 @@ 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)

# 生成 gboard 个人词典
generate_gboardDic(words_list=new_words_list)

input("转换完成, 回车退出.")

if __name__ == '__main__':
main()

0 comments on commit 83d58fe

Please sign in to comment.