apple.dictionary字典制作及EntityRef, Specification mandates value错误处理
如题, 此文将介绍MacOS下, 将mdx等格式的第三方词典转换并扩展成Mac系统内置词典, 并解决make时出现的EntityRef: expecting ‘;’ 错误.
需要配置的工具有:
一. Mac 字典制作工具
二. pyglossary 字典格式转换工具
一. Mac字典制作工具
1.1背景:
Mac字典制作工具Dictionary Development Kit, 是制作Mac内置字典所必须的, 用于将pyglossary转换得到的AppleDict Source文件制作成系统能识别的 apple.dictionary字典.
1.2下载:
要下载它也很简单, 因为它是 Additional Tools for Xcode的一部分, 我们从官网上下载它就可以了. 点击进入 Additional Tools for Xcode官网页面, 可能要输入Apple ID, 按提示输入即可. 然后在页面中找到 Additional_Tools_for_Xcode_16.3 并下载它.
1.3移动:
将得到一个 .dmg 文件, 双击打开它, 进入Utilities文件夹, 将其中的 Dictionary Development Kit文件夹复制到 “/应用程序/实用工具/” 路径下( 英文路径是/Applications/Utilities/). 这个路径是 pyglossary 运行后得到的Makefile中的默认路径. 当然, 也可以把Dictionary Development Kit放置到任何路径, 然后修改Makefile中的 ‘DICT_BUILD_TOOL_DIR’为对应路径. 只不过每次转换你都要修改makefile, 不如直接使用默认路径.
二. pyglossary 字典格式转换工具
2.1安装:
pyglossary 已经集成到 python 模块库中, 可以直接使用pip安装, 但需要注意的是: 现版本PyGlossary依赖 3.10 或更高的Python版本.
pip3installpyglossary pip3installlxml beautifulsoup4 html5lib# 依赖库安装pip3installprompt_toolkit# 安装命令行版本的依赖库可选交互方式: 如果需要更直观的图形交互界面, 还需安装Gtk3. Mac 的终端中运行下列命令以安装Gtk3:
brewinstallpygobject3 gtk+32.2运行
2.2.1 命令行模式:
# 1. 先cd到.mdx文件所在路径cd/Downloads/fmidioms# 2. 运行工具命令pyglossary fmidioms.mdx fmidioms.apple# fmidioms.mdx为输入文件, fmidioms.apple为输出文件# 3. 进入运行结果文件夹cdfmidioms.apple# 4. 一个make命令, 即可使用Dictionary Development Kit制作词典make# 5. 又一个make命令, 一键复制新字典到系统内置字典路径, 即 ~/Library/Dictionariesmakeinstall# 6. 打开字典app, 进入偏好设置, 检查新导入字典.2.2.2 图形界面模式:
直接在终端中不加任何参数运行pyglossary命令, 即可激活图像界面窗口:
pyglossary在图形窗口中, ‘Browse’ 浏览并选择输入文件, 软件会自动识别输入格式; 然后选择Output Format为AppleDic source; 最后点击 Convert, 转换程序就会开始执行, 底部会提示相应的百分比进度:
Covert结束之后, 目标文件夹会和命令行运行结束时一样, 多出一个以.apple结束的新文件夹. 接着就是在终端中cd进这个新文件然后make 和 make install了.
三. 一些问题
3.1 make 命令不能用
那你可能要安装Command Line Tools for Xcode:
在Apple官网中找到Command Line Tools for Xcode下载并安装.
3.2 没有图形界面
图形界面不是必须的, 命令行能实现同样的功能且更加简洁. 如若非要图形界面, 可以参考pyglossary项目的官方文档, 其中Requirements部分会介绍图形界面的相关依赖及其安装, 以及图形界面调用时的顺序. 相关依赖安装会有些繁琐, 不在此展开.
3.3 make时出现 EntityRef: expecting ‘;’ 错误
这是一个技术问题, xml文件格式有所损坏, 从而引发的错误. 虽然在其他数据处理时此问题较为常见, 但是在apple.dictionary字典制作过程中出现此错误, 全网也没有相关记录, 因此重点介绍.
- 如图, make后提示xml文件解析错误:
检查xml文件, 发现存在非转义 & 字符:
使用Python脚本将其中的非法字符&, 统一为合法的
&:
importredeffix_illegal_ampersands(xml_content):# 排除如 &、{、< 等合法情况returnre.sub(r'&(?!(?:amp|lt|gt|quot|apos|#\d+|#x[\da-fA-F]+);)','&',xml_content)# 读取损坏的 XML 文件withopen("fmidioms_apple.xml","r",encoding="utf-8")asf:broken_content=f.read()# 修复内容fixed_content=fix_illegal_ampersands(broken_content)# 保存修复后的内容withopen("fmidioms_apple.xml","w",encoding="utf-8")asf:f.write(fixed_content)note: Some errors may occur in “make” process after this script finishes running. they looks like:
Specification mandates valueforattribute knifetime">before time</a></li><li><a href="x-dictionary:d:before you can say " knifeif this is the case, try this script:
importredeffix_broken_href_quotes_fast(xml_content):# 1. Match everything from href="x-dictionary:d: up until the tag closing '>'# 2. Use a lambda function to clean up quotes inside that specific block nativelypattern=r'href="(x-dictionary:d:[^>]+)"'returnre.sub(pattern,lambdamatch:'href="'+match.group(1).replace('"','"')+'"',xml_content)# --- Streamlined File Execution ---input_file="fmidioms_apple.xml"output_file="fmidioms_apple_fixed.xml"print("Reading file...")withopen(input_file,"r",encoding="utf-8")asf:xml_data=f.read()print("Fixing broken attributes instantly...")fixed_xml_data=fix_broken_href_quotes_fast(xml_data)print(f"Saving fixed data to{output_file}...")withopen(output_file,"w",encoding="utf-8")asf:f.write(fixed_xml_data)print("Done! You can now parse your dictionary file.")Then, change the output file name to be ‘fmidioms_apple.xml’ and just do “make” command.
4. 修复后, 再次make :
- make成功, make install :
make install 成功, 检查字典:
app内使用:
内嵌使用:
均能成功检索. 问题解决, 格式成功转换!
最后, command + shift + G, 弹出窗口中输入路径: ~/Library/Dictionaries, 路径下可以管理已安装的所有字典: