goldendict ex
日期:2024-01-06
归属:CC-BY-NC 4.0
添加在线词典库#
参考GoldenDict++的默认配置,可以添加DICT.org作为在线词典库。
GoldenDict → 编辑 → 词典 → 词典来源/词典服务器 → 添加:
添加单词脚本#
ety-python是一个Python工具,能够打印词源的各历史时期的树状信息图,数据来自Etymological Wordnet项目,大部分信息是从Wiktionary挖掘来。
首先安装Python,我装的是python3.9
。安装过程中勾选「添加Python到PATH」,其他全部默认。
在终端中运行:
将输出:
当在Windows 10的GoldenDict里使用ety-python
时,我遇到了一些问题(见issue #1678, blog)。最后虽然并未解决问题,但算是勉强能用了。使用了一个在隔离环境中的、有修改的ety-python
:
git clone https://github.com/jmsv/ety-python
cd ety-python
python -m venv venv
venv/Scripts/activate.bat
pip install -e .
修改ety/cli.py
,在开头的import ety
下方,加上:
注释掉末尾的print(output.strip())
,加上:
encoded_output = output.strip().encode("utf-16", errors="replace")
sys.stdout.buffer.write(encoded_output)
sys.stdout.buffer.flush()
GoldenDict → 编辑 → 词典 → 词典来源/程序 → 添加:
已启用 On
类型 纯文本
名称 `ety-python`
命令行 `...\ety-python\venv\Scripts\python.exe ...\ety-python\ety\__main__.py -r -t %GDWORD%`
也可以添加其他的脚本,例如:
- 拼写提示DoYouMean
- 同义词词典thes
- 缩写查询abbr-cli。不如原网页All Acronyms靠谱
添加多引擎翻译脚本#
deep-translator是一个支持了多引擎的翻译脚本工具。目前支持DeepL、谷歌翻译、微软翻译、腾讯翻译君、百度翻译等等翻译器。如何申请或创建API请看各翻译器官网介绍。
按照说明,需要添加API的两个变量值到系统环境中。以腾讯翻译为例,使用环境变量编辑器Rapid Environment Editor来添加:
- RapidEE → 用户变量 → 右键 → 添加环境变量 → 变量名称
TENCENT_SECRET_ID
→ 填写你的SecretId
- … 添加环境变量 → 变量名称
TENCENT_SECRET_KEY
→ 填写你的SecretKey
不怕Bug可以从源码安装:
运行示例:
有些翻译器支持检查源语言
,然后都需要指明目标语言
。例如我需要「中英文互译」的情况,有一个不太优雅但可行的方法是:在GoldenDict中添加两个脚本,其中一个的输入语言为en
,目标语言为zh
,另一个则将两者互换。
添加专用翻译脚本#
我目前的需求是:多语言(主要是英语)翻译到中文,中文翻译到英文,运行快而稳。所以我使用了特定翻译器的脚本,用于Goldendict的腾讯云翻译。支持多门外语,文本翻译一项上,每月有500万字符免费额度,重度使用也完全够用。
- 首先参考申请翻译API说明,或者看官方介绍去获得API
- 下载修改版的TencentTrans_22.py,脚本中填写API的
SecretId
和SecretKey
pip install tencentcloud-sdk-python langid
- … 词典 → 词典来源/程序 → 添加:
不足之处有:
- 不明Bug,例如有些语言如韩语,当在GoldenDict中输出时,在翻译文字前会显示一串报错
- 输出的文本没有段落,因为GoldenDict会删除换行符
添加分词脚本(仅gd-ng)#
GoldenDict tools是一套GoldenDict-ng的增强脚本集,主要用于日语学习。它的gd-marisa、gd-mecab脚本,可以置顶句子、分词、断句。似乎也能用于中文,但并没有实际的「中文分词」的功能,可以用「划词再右键」来代替。
参考issue #18下载gd-tools_windows.zip
,解压后运行安装包。当安装包没有提供Hash值时,我习惯拖到VirusTotal进行检查。虽然这个安装包有几个红色警告,但我在HiBit Uninstaller的「安装监视程序」下,选择了任意一处位置进行安装。
GoldenDict-ng → 编辑 → 词典 → 词典来源/程序 → 添加:
已启用 On
类型 Html
名称 `gd-marisa`
命令行 `...\Ajatt-tools\gd-tools\gd-tools.exe marisa --word %GDWORD% --sentence %GDSEARCH% --path-to-dic ...\Ajatt-tools\gd-tools\marisa_words.dic`
使用LanguageTool进行语法检查#
LanguageTool是一个开源的多语言的拼写、语法、风格检查工具。在运行了它的本地服务应用后,可以去到它的浏览器插件中,切换服务源,从「云服务」切换到「本地服务」:
- 安装浏览器插件
- 根据说明,下载
LanguageTool Desktop version
,解压缩 - 安装OpenJDK,我使用的是openjdk17,
scoop install openjdk17
- 终端中运行
java -cp ...\LanguageTool\languagetool-server.jar org.languagetool.server.HTTPServer --port 8081 --allow-origin
- 浏览器插件 → Settings → Advanced settings → Localserver (localhost)
- … General settings → Show in right-click menu (On)
在一些场景,例如提交和回复Issues时,我需要使用简单的书面英语。虽然不进行检查,也有可能被理解意思,但我考虑在书写时用上语法检查。
本地版的LanguageTool插件,它似乎只方便于:在浏览器的「输入框」输入完后,进行检查。我不太想无论长短句都去到新窗口中处理。这里就用到pyLanguagetool,将它嵌入到GoldenDict来进行交互。
git clone https://github.com/Findus23/pyLanguagetool
cd pyLanguagetool
python39 -m venv venv
venv\Scripts\activate.bat
GoldenDict在处理一些字符时会出错。编辑 pylanguagetool/cli.py
,修改:
tick = colored(u"\u2713", Fore.LIGHTGREEN_EX) + " "
cross = colored(u"\u2717", Fore.LIGHTRED_EX) + " "
到:
修改:
到:
try:
print(error["message"])
except UnicodeEncodeError:
# Replace problematic character with an empty string
print(error["message"].replace('\xa0', ''))
可以注释掉两个段落来隐藏一些提示信息:
print(colored(
"{} detected ({:.0f}% confidence)".format(language["detectedLanguage"]["name"],
language["detectedLanguage"]["confidence"] * 100)
, Fore.LIGHTBLACK_EX))
if language["detectedLanguage"]["code"] != language["code"]:
print(colored(
"checking as {} text because of setting".format(language["name"])
, Fore.LIGHTBLACK_EX))
print()
if explain_rule:
col_len = max(len(d) for d, u in rule_explanations) + 1
for descr, url in rule_explanations:
print(descr + ":" + " " * (col_len - len(descr)) + url)
print()
print(colored("Text checked by {url} ({version})".format(url=api_url, version=version), Fore.LIGHTBLACK_EX))
安装pyLanguagetool
:
我使用了echo-cli来代替echo
命令,pnpm add -g echo-cli
。
终端中测试,我指定了要检查的语言:
echo-cli "this is a exampl" | pylanguagetool --api-url http://localhost:8081/v2/ --input-type html --no-color --lang en-US
添加到GoldenDict里时需要写长点:
已启用 On
类型 纯文本
名称 `pyLanguagetool`
命令行 `C:\Users\YourName\AppData\Roaming\pnpm\echo-cli.CMD %GDWORD% | ...\gd_pyLanguagetool\venv\Scripts\pylanguagetool.exe --api-url http://localhost:8081/v2/ --input-type html --no-color --lang en-US`
需要等宽字体才能对齐「字母」和「波浪线」。速度上不如插件。
作为本地服务,LanguageTool大概占用500~600mb
内存,耗电量属非常低。在不使用「Windows计划任务」的情况下,开机后在后台运行LanguageTool服务,可以新建srv_languagetool.cmd
:
...\java.exe -cp ...\LanguageTool\languagetool-server.jar org.languagetool.server.HTTPServer --port 8081 --allow-origin
和srv_languagetool.vbs
,来隐藏运行窗口:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "...\srv_languagetool.cmd" & Chr(34), 0
Set WshShell = Nothing
右键srv_languagetool.vbs
,新建快捷方式。将快捷方式,移动到C:\Users\YourName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
。
目前,我一般集中地去处理(遗留的)Issues,所以我并未在后台常驻这个服务。
另一个语法检查脚本#
Gramformer是一个Python库,可以用于检查句子的拼写、标点符号、语法或用词错误。曾经计划支持64个字符(包括空格和标点符号)以上长度的句子。虽然可以使用GPU模式,但是运行很慢。
git clone https://github.com/PrithivirajDamodaran/Gramformer
python39 -m venv venv
venv\Scripts\activate.bat
pip install -e .
先安装CUDA Toolkit。根据PyTorch,我装了CUDA 11.8.0
,下载特定版本的torch,再
参考issue #31:
创建cli.py
,详情参见gramformer_cli.py:
import sys
import warnings
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from gramformer import Gramformer
def set_seed(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def main():
if len(sys.argv) != 2:
print("Usage: python cli.py INPUT")
return
set_seed(1212)
warnings.filterwarnings("ignore", category=FutureWarning)
use_gpu = torch.cuda.is_available()
device = torch.device("cuda" if use_gpu else "cpu")
gf = Gramformer(models=1, use_gpu=use_gpu) # 1=corrector, 2=detector
influent_sentence = sys.argv[1]
corrected_sentences = gf.correct(influent_sentence, max_candidates=1)
print("x ", influent_sentence)
for corrected_sentence in corrected_sentences:
print("v ", corrected_sentence)
print("Device:", device)
if __name__ == "__main__":
main()
可以修改gramformer/gramformer.py
的:
来隐藏这段文字。将这行改为:
测试:
… 词典 → 词典来源/程序 → 添加:
已启用 On
类型 纯文本
名称 `Gramformer`
命令行 `...\Gramformer\venv\Scripts\python.exe ...\Gramformer\cli.py %GDWORD%`
另一种联动OCR取词#
也是使用GoldenDictOCR,在它「OCR取词」模式下,按Ctrl+右键单击
可识别鼠标附近字符,按Ctrl+反引号
可进行框选。
先安装OCR工具Capture2Text。默认OCR语言为英语,可通过Installing Additional OCR Languages的说明添加额外的语言。
之后,进入GoldenDictOCR
文件夹。编辑IncludeAHK/GdOcrTool.ahk
。填写Capture2Text.exe
所在的位置:
按Ctrl+Alt+O
开关一次「OCR取词」。在C:\Users\YourName\AppData\Roaming\Capture2Text
目录下,会生成配置文件Capture2Text.ini
。
打开配置文件示例IncludeAHK/Capture2Text.ini
,复制从[BubbleCapture]
到[Hotkey]
的内容,粘贴到前面的配置文件的末尾。再添加或编辑以下两处,用于设置3个快捷键,来切换OCR语言,如:
用来识别标准字形的单个英文小字时,识别率还可以,稳定性较差。