qunwang6 发表于 2014-8-29 22:28:32

MAC OSX词典转换笔记

本帖最后由 qunwang6 于 2017-6-9 00:23 编辑

\BGL\DSL\MDX\CD\ ===> OSX.dictionary
开源项目:PYGLOSSARY
https://github.com/ilius/pyglossary

20170609更新:
新版pyglossary 要安装以下环境:sudo pip3 install lxml beautifulsoup4 html5lib
Python 改成Python3

✍准备好以下环境:
* BeautifulSoup4 required to sanitize html contents.
sudo easy_install beautifulsoup4 html5lib
* GNU make as part of Command Line Tools for Xcode.
http://developer.apple.com/downloads
* Dictionary Development Kit as part of Auxillary Tools for Xcode
http://developer.apple.com/downloads.
将Dictionary Development Kit解压到/Applications/Utilities/DictionaryDevelopmentKit/
(不含空格,中文名:应用程序/实用工具那里,也可以解压到其它地方。)


BGL ===> OSX.dictionary
转换 python pyglossary.pyw --read-options=resPath=OtherResources --write-format=AppleDict dict.bgl dict.xml编译
* 修改Makefile里2个参数,DICT_NAME是生成文件的名称,DICT_BUILD_TOOL_DIR是Dictionary Development Kit的路径,如果错了就不能生成文件。
* 修改Info.plist里3个参数,CFBundleName是在Mac字典程序工具栏下面显示的标签名,CFBundleDisplayName是词典名称,CFBundleIdentifier是词典的ID,需保持唯一性。。
* 根据模板格式编辑Dictionary.xml、Dictionary.css,如果有图片则放置在OtherResources/目录下.
* 打开终端,cd进入模板文件目录,输入make,稍等一段时间,即可生成字典文件,将object目录下生成的dictionary文件拷贝至 ~/Library/Dictionaries,打开字典程序,即可看到生成的字典。
或者直接用make && make install这个命令,生成词典后直接安装到了~/Library/Dictionaries。

**注意:如果OtherResources里的图片太多的话会出现以下错误:**
/Volumes/Macintosh HD/Applications/Utilities/DictionaryDevelopmentKit//bin/build_dict.sh: line 206: /bin/cp: Argument list too long
Error.
make: *** Error 1

可以在编译前不加入图片、语音文件,在编译完成后将图片、语音文件复制到xxx.dictionary/Contents目录下。



DSL ===> OSX.dictionary

1. Convert DSL from UTF-16 to UTF-8: iconv -f UTF-16 -t UTF-8 dict_utf16.dsl > dict_utf8.dsl
2. Using pyGlossary python project, convert UTF-8 DSL to AppleDict XML:python pyglossary.pyw --read-format=ABBYYLingvoDSL --write-format=AppleDict dict_utf8.dsl dict.xml
**lingvo格式.dsl转换成可发音的词典。(注意修改命令参数)**↓
audio conversion is optional, "no" by default. The following command works,
~/Development/pyglossary/pyglossary.pyw --read-option="audio=yes" test.dsl test.xml
make && make installpython /Applications/Utilities/DictionaryDevelopmentKit/pyglossary/pyglossary.pyw --read-options="audio=yes" --write-format=AppleDict ldoce55.dsl ldoce55.xml
3. Using Command Line Tools for Xcode and Dictionary Development Kit:编译步骤同BGL ===> OSX.dictionary
make
make install

MDX ===> OSX.dictionary

转换
cd ~/Downloads/oald8/
~/Software/pyglossary/pyglossary.pyw --read-options=resPath=OtherResources --write-format=AppleDict oald8.mdx oald8.xml
修改.xml文件里的链接(如果不是可发音的词典,以下两步可略过)
sed -i "" 's:src="/:src=":g' oald8.xml
sed -i "" 's|sound://\(*\).spx|\1.wav|g' oald8.xml第一个是去掉"/"
第二个是去掉”sound://" ,".spx"改成".wav"

将.spx语音文件转换成.wav(需要安装speex,麻烦!可直接从lingvo那里复制,或者直接从原光盘提取)find OtherResources -name "*.spx" -execdir sh -c 'spx={};speexdec $spx${spx%.*}.wav' \
编译步骤同BGL ===> OSX.dictionary
make && make install注意:如果OtherResources里的图片和语音文件太多的话会出现错误,可以在编译前不加入图片和语音文件,在编译完成后将图片复制到xxx.dictionary/Contents目录下。

CD(原词典光盘)===> OSX.dictionary
以OALECD8为例。
数据提取:虚拟机xp+python2.7,用IDMSKconv提取。
制作:从OALECD8安装目录下找到oalecd_master.css.BAK、从提取出的数据里找到output文件、以及图片和语音mp3文件,复制到Mac OSX下备用。
将output文件重命名为oalecd8.xml,oalecd_master.css.BAK作为CSS文件重命名为oalecd8.css,从Dictionary Development Kit里找到模板文件分别复制出MyInfo.plist、makefile文件,MyInfo.plist重命名为oalecd8.plist,修改oalecd8.plist和makefile里的参数。
排版:里面各种奇形怪状的标签可以不必修改,有oalecd_master.css.BAK作为默认CSS,所以只需要在oalecd8.xml里添加头部和尾部,删除不要的东西,就可以编译出和原版相同的词典。
......

###参考:
* GoldenDict Mac OS X available - Early Access Builds
* PyGlossary Overview
*https://developer.apple.com/library/mac/documentation/userexperience/Conceptual/DictionaryServicesProgGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40006152

细节部分

v======* 注意修改单词内部的链接========v
单词跳转用x-dictionary:r:
(只能在当前词典内跳转且x-dictionary:r:后的单词大小写须与d:entry id="***"里的大小写一致,不能有空格否则不能跳转。)

单词查找用x-dictionary:d:
(会先搜索当前的词典,若没有,会调用其它词典搜索,不限大小写、空格。)
(基本上都是用r,用d很少)
^==============================^
The x-dictionary: URI contains three elements separated by colons as the general form—target selector, target text, and dictionary bundle ID. The target selector must be either d (for definition) or r (for reference). Use d if you want to search definitions of the following key text. Use r if you want to refer to the entry specified by the reference ID which must be unique to each dictionary.

辞書.appの辞書を引くURLスキームに
次のようなものもあります。
x-dictionary:d:key_text:dict_bundle_id
x-dictionary:r:reference_id:dict_bundle_id
**1key_text    調べたい単語
**2reference_id 辞書を作るために作成するdictionary.xmlの
各タイトルごとに割り当てる固有のエントリーID
<d:entry id="0ABCDE" d:title="案の定">
* ex.


   <d:entry id="make a face" d:title="make a face">
   <d:index d:value="make a face"/>
   <html><body><h1>make a face</h1>
   <a href="entry://grimace">grimace</a></body></html>
   </d:entry>

   <d:entry id="make faces" d:title="make faces">
   <d:index d:value="make faces"/>
   <html><body><h1>make faces</h1>
   <a href="entry://make a face">make a face</a></body></html
   </d:entry>

"entry://grimace"
将entry://替换为x-dictionary:r:
===>"x-dictionary:r:grimace"

d:entry id="make a face"
href="entry://make a face"
类似上面的单词,中间不能有空格,否则单词间不能跳转。
===>
d:entry id="make_a_face"
href="x-dictionary:r:make_a_face"
替换空格需要用到的正则表达式:
(在此感谢ryuya,https://pdawiki.com/forum/thread-13044-1-1.html)(<d:entry id="[^"]+)\s替换为\1_(href="x-dictionary:r:[^"]+)\s替换为\1_
v=========词组/词性跳转、定位===========v
词组/词性页面内定位
<a class="LP_OS" href="entry://#L_hkx1">suffix</a>
<a name="L_hkx1"></a>
==> <a class="LP_OS" href="#L_hkx1">suffix</a>
------------------
词组/词性跳转并定位
<a href="entry://clip#ld3097">clip</a>
<a name="ld3097"></a><span class="L_IDOM">$100 / 50 cents etc a clip</span>
==> <a href="x-dictionary:r:clip:#ld3097">clip</a>
用到的正则表达式:
x-dictionary:r:(.+?)#
替换为:
x-dictionary:r:\1:#
相关的词典:朗文当代英语词典英汉双解第4版
Shorter Oxford English Dictionary Sixth Edition
^=========词组/词性跳转、定位===========^


另外一个开源项目
DSL to OS X/iOS dictionary converter(相关教程可到我的空间相册里查看)
https://github.com/svintuss/DSLConverter
StarDict转换工具
https://code.google.com/p/mac-dictionary-kit/
关于金山字体问题
http://www.linuxsir.org/bbs/thread363915.html
许多Linux词典也使用了Kingsoft Phonetic Plain字体,但是能下载到的金山字体一般都不是unicode编码的,导致Linux下音标显示为方框。实际上用FontForge修改一下金山字体即可使用。
安装FontForge:
#urpmi fontforge
然后用FontForge打开金山字体文件,你可以看到FontForge对字体里的每一个字形都分两行显示。上面小的是字符的名称,下面大的就是那个字符的字形了。金山字体本来是对普通拉丁字符赋予音标字形的一个字体,但是因为它不是unicode编码,在Unicode的拉丁字符区一个字形都没有。
所以首先要找到金山字符的位置,比较靠后。注意从一个空格字形开始的。这个空格很重要,因为空格和之后几个符号的字形跟普通拉丁字符的字形没有区别,所以我们用来定位所有的金山字符。
找到之后,从空格开始选定所有的字符,大约有三四十个。剪切之。
然后找到拉丁字符区。可以看到所有的字形都是“X”,也就是空。找到表示空格的那个字符位(U+0020),选中,复制。所有的金山字形就按照顺序复制到这个字符位以及之后的字符位去了。
最后保存成.ttf文件就OK了。保存的时候会有一些提示信息,照着做一下就行。
顺便发一个做好的.ttf文件。仅为技术探讨,版权归原作者所有。


一些小技巧
Mac osx 下可以用三指取词,但在一些第三方软件下是不起作用的。
不过结合Alfred这个神器+下面这个脚本可实现热键取词。open dict://{query}同理可将网上流传的“142000个单词语音库”解压到同一个文件夹内,借助内置的afplay命令+Alfred+下面的脚本,可做到热键取词发音。afplay /Users/qunwang6/Sound/Webster/{query}.wav
生词本
BetterDictionary
https://github.com/pooriaazimi/BetterDictionary/raw/master/Images/BetterDictionary-Yosemite.png
...

自己动手,丰衣足食。先发颗种子。
Longman Dictionary of Contemporary English 5th Edition 无发音
(宇宙盘0x0001词库分类 /MAC OSX dictionary下 or http://pan.baidu.com/s/1kTolbkb)
转自https://www.pdawiki.com/forum/thread-10697-1-1.html
https://www.pdawiki.com/forum/data/attachment/album/201409/01/011712w3y8dty3ifdvd9ci.gif
链接: http://pan.baidu.com/s/1i3rgl7n 密码: vcpf

colin00 发表于 2017-9-7 13:15:08

本帖最后由 colin00 于 2017-9-7 13:19 编辑

大神,我在转这部Collins的时候出现了问题:

"""/Developer/Extras/Dictionary Development Kit"/bin"/build_dict.sh""CollinsDictionary" "CollinsDictionary.xml" "CollinsDictionary.css" "CollinsDictionary.plist"
- Building CollinsDictionary.dictionary.
- Checking source.
CollinsDictionary.xml:70: parser error : xmlParseEntityRef: no name
OL.css$/;for(var i=_l.length-1;i>=0;i--)with(_l.href){var _m=match(_r);if(_m&
                                                                            ^
CollinsDictionary.xml : failed to parse
Error.
make: *** Error 1

请问下,这个该如何解决呢?

谢谢!

这一行代码是这样的:
<d:entry id="_n" d:title="404">
<d:index d:value="404" d:title="404"/><h1>404</h1><div class="dxr"><div id="404_1"><div class="j84"><a id="404_1"></a><div class="f9d">404 <span class="kf5">/ˌfɔːəʊˈfɔː<img class="ik4" onclick="aes(this,'e/en_/en_gb/en_gb_404')" src="sp.png"/>/</span></div><div class="mh1"><div class="x5z"><h3 class="jgs"><span class="sg0">noun</span></h3><div class="o8h"><div class="lij"><span class="rgm">slang.</span> <span class="sd9">a stupid or ineffectual person</span></div></div></div></div><div class="roj"><h2 class="fhq">Origin</h2>C21: from the Internet error message '404 Not Found'</div></div></div><script src="cl.js" type="text/javascript"></script><script>if(typeof(uc1)=="undefined"){var _l=document.getElementsByTagName("link");var _r=/COL.css$/;for(var i=_l.length-1;i>=0;i--)with(_l.href){var _m=match(_r);if(_m&&_l.id=="kc1"){document.write('<script src="'+replace(_r,"cl.js")+'"type="text/javascript"><\/script>');break;}}}</script></div>
</d:entry>

qunwang6 发表于 2014-9-6 09:58:50

cidianaihao 发表于 2014-9-6 08:39 static/image/common/back.gif
不过结合Alfred这个神器+下面这个脚本可实现热键取词。

    open dict://{query}


https://www.pdawiki.com/forum/data/attachment/album/201408/29/223719exdxzxi8i181iex1.pnghttps://www.pdawiki.com/forum/data/attachment/album/201408/29/223725n1cq0q7ldeec7s7s.png

zhiqiujinsheng 发表于 2017-2-12 23:35:57

感谢楼主,成功转换了[英-汉] 牛氵聿高阝介第⑧中英又又解 https://www.pdawiki.com/forum/thread-14881-1-1.html。但是有几个问题:
1.
2.
图片的xml代码如下:
<img alt="/pic/fruit_comp.jpg" height="540" onclick="this.style.display='none';this.nextSibling.nextSibling.style.display='block';" src="/pic/fruit_comp.jpg" style="display:none;" width="720"/>
<img alt="/thumb/apple.jpg" backup-height="540" backup-width="720" class="Media" onclick="this.style.display='none';this.previousSibling.previousSibling.style.display='block';" resource="pic" src="/thumb/apple.jpg" thumb="apple.jpg" thumb_resource="thumb" topic="fruit_comp.htm" type="image"/>

文件O8C.dictionary/Contents/pic/fruit_comp.jpg也存在。这个词典里面的所有图片都没有。你发的朗文5里面图片是可以的

词性的xml代码如下:<span class="block-g" display="block" level="3">
<span class="pos-g" display="inline" level="4">
<span class="pos" level="5" pos="n">noun</span>
</span>
<img alt="/pic/fruit_comp.jpg" height="540" onclick="this.style.display='none';this.nextSibling.nextSibling.style.display='block';" src="/pic/fruit_comp.jpg" style="display:none;" width="720"/>
<img alt="/thumb/apple.jpg" backup-height="540" backup-width="720" class="Media" onclick="this.style.display='none';this.previousSibling.previousSibling.style.display='block';" resource="pic" src="/thumb/apple.jpg" thumb="apple.jpg" thumb_resource="thumb" topic="fruit_comp.htm" type="image"/>
<span class="clear"></span>
</span>
就只有第一行在音标后面的词性才不换行,之后的词性没有问题,所有的单词都是这样。当窗口宽最小时,词性才换行

第一行的xml代码如下:
<span class="top-g" level="2">
<span class="h" core="y" level="3">apple</span>
<span class="z">
<span class="symbols-coresym">★</span>
</span>
<span class="ei-g" level="3" type="h_full_">
<span class="z_ei-g">/</span>
<span class="phon-gb" file="{gb}/a/ap/app/apple#_gb_1.spx" level="4" recdate="070503" wd="apple">ˈæpl</span>
<a backup-class="Media" class="fayin" href="x-dictionary:d:sound://uk/apple__gb_1.spx" resource="uk_pron" topic="a/app/apple/apple__gb_1.spx" type="sound"><img class="fayin" src="uk_pron.png"/></a>
<span class="z">;
<span class="z_phon-us">NAmE</span>
</span>
<span class="phon-us" file="{gb}/a/ap/app/apple#_us_1.spx" level="4" recdate="070503" sup="y" wd="apple">ˈæpl</span>
<a backup-class="Media" class="fayin" href="x-dictionary:d:sound://us/apple__us_1.spx" resource="us_pron" topic="a/app/apple/apple__us_1.spx" type="sound"><img class="fayin" src="us_pron.png"/></a>
<span class="z_ei-g">/</span>
</span>
<span class="block-g" display="block" level="3">
<span class="pos-g" display="inline" level="4">
<span class="pos" level="5" pos="n">noun</span>
</span>
<img alt="/pic/fruit_comp.jpg" height="540" onclick="this.style.display='none';this.nextSibling.nextSibling.style.display='block';" src="/pic/fruit_comp.jpg" style="display:none;" width="720"/>
<img alt="/thumb/apple.jpg" backup-height="540" backup-width="720" class="Media" onclick="this.style.display='none';this.previousSibling.previousSibling.style.display='block';" resource="pic" src="/thumb/apple.jpg" thumb="apple.jpg" thumb_resource="thumb" topic="fruit_comp.htm" type="image"/>
<span class="clear"></span>
</span>
</span>

dliaas 发表于 2021-9-15 23:15:03

多谢分享!

geinixingxing 发表于 2021-8-25 03:52:19

能出个视频就好了,我连用什么软件转换都不知道

greatABC005 发表于 2020-2-3 15:02:57

Thanks for your great work.

njpig 发表于 2019-8-17 13:05:34

兄弟,LDOCE6好像用不了哦。

nsw 发表于 2017-12-25 15:53:29

请问各位有没有 牛津高阶英汉双解第八版 给mac用的呢

colin00 发表于 2017-9-10 10:40:58

qunwang6 发表于 2017-9-9 20:01
用ultraedit编辑器打开,正则替换

多谢大神,已经成功制作了!!!

qunwang6 发表于 2017-9-9 20:01:31

本帖最后由 qunwang6 于 2017-9-9 20:03 编辑

colin00 发表于 2017-9-8 15:31
谢谢大神,新版本貌似不能删除空格,删除了就没用了;xml文件很大,不知道为什么用编辑器打开存储后老出 ...

用ultraedit编辑器打开,正则替换
https://i.loli.net/2017/09/09/59b3d80f0f136.png

colin00 发表于 2017-9-8 15:31:58

qunwang6 发表于 2017-9-7 23:11
不支持javascript,把和里面的内容删掉,单独用一个单词测试编译试试。另外"/Developer/Extras/Diction ...

谢谢大神,新版本貌似不能删除空格,删除了就没用了;xml文件很大,不知道为什么用编辑器打开存储后老出现 invalid string length 的错误,所以想借助终端的 sed 命令批量修改,但是又不知道该怎么删除这种 html 标签和中间内容,大神指教下吧~~

qunwang6 发表于 2017-9-7 23:11:17

本帖最后由 qunwang6 于 2017-9-7 23:12 编辑

colin00 发表于 2017-9-7 13:15
大神,我在转这部Collins的时候出现了问题:




不支持javascript,把<script></script>和里面的内容删掉,单独用一个单词测试编译试试。另外"/Developer/Extras/Dictionary Development Kit"/bin"/build_dict.sh"路径里有空格,建议去掉。

changfan666 发表于 2017-2-26 22:07:37

太好了支持一个一定要顶非常实用

papago 发表于 2017-2-24 13:04:15

感谢大师的作品!

孤影 发表于 2017-2-22 07:17:52

有没有OSX to MDX的方法

霜叶莺 发表于 2016-12-16 18:11:38

楼楼xcode8是不是不可以用啊?

4l613 发表于 2016-11-28 09:54:56

支持楼主!!!

4l613 发表于 2016-11-28 09:54:42

micangel 发表于 2016-11-27 17:53
才看到大神的回复.已经转头的欧路Mac版的怀抱. 用Anki的Word Query插件可以实现制卡直接同时导入多本Mdx...

支持楼主!!!

micangel 发表于 2016-11-27 17:53:17

qunwang6 发表于 2016-10-30 22:15
/Applications/Utilities/DictionaryDevelopmentKit//bin/build_dict.sh: No such file or directory
检 ...

才看到大神的回复.已经转头的欧路Mac版的怀抱. 用Anki的Word Query插件可以实现制卡直接同时导入多本Mdx dict的释义,已经玩High了.这个错误还真没注意到,谢谢大神, 方法mark. 回头再试一遍,当是学习.

qunwang6 发表于 2016-10-30 22:15:24

micangel 发表于 2016-10-29 17:02
"""/Applications/Utilities/DictionaryDevelopmentKit/"/bin"/build_dict.sh""abc" "abc.xml" "abc.css" ...

/Applications/Utilities/DictionaryDevelopmentKit//bin/build_dict.sh: No such file or directory
检测一下文件的路径

micangel 发表于 2016-10-29 17:02:36

"""/Applications/Utilities/DictionaryDevelopmentKit/"/bin"/build_dict.sh""abc" "abc.xml" "abc.css" "abc.plist"
/bin/sh: /Applications/Utilities/DictionaryDevelopmentKit//bin/build_dict.sh: No such file or directory
make: *** Error 127
到make这一步时出现这个问题,求大神现身.

Niuzigeng 发表于 2016-10-29 14:22:58

谢谢楼主努力,提供好资源。

dinglove55 发表于 2016-8-21 22:26:55

谢谢了,大神。

patrickouc 发表于 2016-7-15 20:12:13

支持一下开源项目

lorlno11 发表于 2016-4-13 17:21:49

好棒啊!!!!!!!!!!

EarthWorm 发表于 2016-3-28 17:18:45

qunwang6大神, 按照您的方法使用PYGLOSSARY时碰到了问题, 能否请您抽空赐教?
具体请移步: https://www.pdawiki.com/forum/forum.php?mod=viewthread&tid=16119&extra=page%3D1
页: [1] 2 3
查看完整版本: MAC OSX词典转换笔记