\BGL\DSL\MDX\CD\ ===> OS X dictionary

###开源项目:PYGLOSSARY

###准备好以下环境:

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

###BGL ===> OSX.dictionary
转换

python pyglossary.pyw --read-options=resPath=OtherResources --write-format=AppleDict dict.bgl dict.xml

编译

  • 在Makefile里设置好相关参数和路径,如DICT_NAME是生成文件的名称,DICT_BUILD_TOOL_DIR是Dictionary Development Kit的路径,如果错了就不能生成文件。
  • 编辑模板文件中的Info.plist文件,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: * [all] 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 install
python /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:
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://\([/_a-zA-Z0-9]*\).spx|\1.wav|g' oald8.xml
    

第一个是去掉”/“
第二个是吧”.spx”改成”.wav”

  • 将.spx语音文件转换成.wav(需要安装speex,麻烦!可直接从lingvo那里复制)
find OtherResources -name "*.spx" -execdir sh -c 'spx={};speexdec $spx  ${spx%.*}.wav' \
  • 编译
make && make install

注意:如果OtherResources里的图片和语音文件太多的话会出现错误,可以在编译前不加入图片和语音文件,在编译完成后将图片复制到xxx.dictionary/Contents目录下。


###修改单词内部的链接

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 rif 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


**3dict_bundle_id 辞書の中のInfo.plistのBundle identifierの値
(例えば大辞泉の場合はcom.apple.dictionary.Daijisen)

  • 单词跳转用x-dictionary:r:
    (只能在当前词典内跳转且x-dictionary:r:后的单词大小写须与d:entry id=”***“里的大小写一致,不能有空格否则不能跳转。。)
  • 单词查找用x-dictionary:d:
    (会先搜索当前的词典,若没有,会调用其它词典搜索,不限大小写、空格。)
    (基本上都是用r,用d很少)

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”

正则替换:(<d:entry id="[^"]+)\s替换为:\1_
(href="x-dictionary:r:[^"]+)\s替换为\1_

####不能含有'or符号
正则替换:(href="x-dictionary:r:[^"]+)’
(<d:entry id="[^"]+)’
替换为\1-

词组/词性跳转、定位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
词组/词性页面内定位
<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

###语音

1
<audio id="\1" src="Sounds\/\1" type="audio/mpeg"/><img class="sound" src="Sound.png" onmouseover="document.getElementById('\1').play(); return false;"/>

高亮搜索关键词

1
2
3
4
5
6
<d:index d:value="make it" d:parental-control="1" d:anchor="xpointer(//*[@id='make_it'])"/>
<div d:parental-control="1" d:priority="2">
<h3>PHRASES</h3>
<div id="make_it"><b>make it</b> : succeed in something; survive.</div>
<h4><a href="x-dictionary:r:make_up_ones_mind"><b>make up one's mind</b></a></h4>
</div>

===>

1
<d:index d:value="make it" d:anchor="xpointer(//*[@id='make_it'])"/>

###附录

1
2
3
4
5
6
<d:entry id="front_back_matter" d:title="Front/Back Matter">
</d:entry>
添加以下值到Info.plis或者编译前的.plist文件
<key>DCSDictionaryFrontMatterReferenceID</key>
<string>front_back_matter</string>

entry id值要与string里的值一样

1
2
3
<key>CFBundleIdentifier</key>
<string>com.apple.xxxx</string>
必须为英文字符,否则不能显示附录。

###词条自动合并

1
2
3
4
5
6
7
8
9
10
11
12
<d:entry id="1" d:title="1">
<d:index d:value="a"/>
1
</d:entry>
<d:entry id="2" d:title="2">
<d:index d:value="a"/>
2
</d:entry>
<d:entry id="3" d:title="3">
<d:index d:value="a"/>
3
</d:entry>

<d:index d:value="a"/>值相同,查a时,全部合并在同一页面显示。

1
2
3
4
5
6
7
8
9
<d:entry id="1" d:title="1">
<d:index d:value="a"/>
<d:index d:value="b"/>
a
</d:entry>
<d:entry id="2" d:title="2">
<d:index d:value="b"/>
b
</d:entry>

id=”1”里只有a;id=”2”含有a和b。
==>查找a,只有一个;查找b,有两个,合并。

1
2
3
4
5
6
7
8
9
<d:entry id="1" d:title="1">
<d:index d:value="a b"/>
a
</d:entry>
<d:entry id="2" d:title="2">
<d:index d:value="a-b"/>
b
</d:entry>

a ba-b相似,合并。
id=”1”里有ab;id=”2”有ba。

###隐藏 词条/搜索结果

  • 需要电脑打开家长控制
  • 在词条中加入d:parental-control="1"标签,含有这个标签的词条或句子就不会显示。

###其他

  • <d:index d:value="made" d:title="made (make)"/>查找made,索引栏显示made (make)。

  • @@@LINK=(?<sub>.*?)</body>
    ===>
    <a href="x-dictionary:r:\1">\1</a></body>

在 XHTML 中不鼓励使用 name 属性,应该使用 id 取而代之。

help

(run “pyglossary.pyw –help” for more info), or use Tkinter interface:
Install python-tk using “pip install python-tk” or somehow else (i don’t know much about Mac)
Then run the program with: python pyglossary.pyw –ui=tk

RuntimeError

RuntimeError: maximum recursion depth exceeded while calling a Python object
The conversion stops when cleaning item “non-“. The html text maybe buggy to cause many time recursion. Here is a workaround, insert the following lines at the top of file pyglossary/plugins/appledict.py

1
2
import sys
sys.setrecursionlimit(10000)

But be aware that this item cannot be displayed at all. The above line is only to avoid runtime exception.

No reference index record

  • Note: No reference index record.

可忽略的错误,文本中若没有a href="x-dictionary:r:和对应的d:entry id便不会生成reference index

###常用正则表达式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(\w+)
(\w+.mp3)
(?<sub>.*?)
(.*?)
(.+?)
(.*+)
小写转大写\U\1
===
去下划线_
(<d:index d:value="[^"]+)_
\1
===
<h1>advance_2</h1>
(<h1>[^\<]+)_
\1 (后加一空格)//<h1> air_plane</h1> ==> <h1> air plane</h1>
===
匹配/ // //.jpg
<sk_img (?<sub>.*?)/(\w+).jpg" (?<sub>.*?)”/>
(?<sub>.*?)([^\/]+)"
===
<sk_example variant="BRE" file="uk/p/p07/p070_/p070-000210864.mp3”/>
<sk_example variant="BRE" file="(?<sub>.*?)/(\w+\-\w+).mp3”/>
===
^p ^n
===
去空行
[^p]+
===>^p

###大小写替换

1
2
3
4
href="x-dictionary:r:(.+?)"
href="x-dictionary:r:\L\1"
<d:entry id="(.+?)"
<d:entry id="\L\1"

若大写转写会重复的词,编译会出错,不建议替换。
词条跳转entry id部分与x-dictionary:r:部分大小写要一致。


###CSS部分

  • .apple_client-panel body{margin: 0 0 15px;font-size: 16px;}//取词小窗口修改大小
  • mdx===>osx.dic 部分字体间距
    font {padding: 2px;}

###显示数量限制
0001-01100
1000个词


###特殊符号

  • 德文字符ä ü ö ß替换成相应的英文字母,链接跳转用。

    XML文档<htlm>里的 &amp;不要替换。
    在xml当中“&”必须采用转义字符&amp;

&amp;<—&

&copy;<—©

&reg;<—®

&quot;<—"

&apos; ='

&nbsp;<—  空格 ===>&#160;

&lt;—><

&gt;—>>
单个< >要替换掉

###'or符号的问题

&acute;

audio id元素里不能有' getelementbyid里也不能有
正则 (<audio id="[^"]+)’
下载链接里不能识别’
wget -i 下载
替换为%27
http://www.rhymer.com/RhymingDictionary/hadn%27t.html
终端命令里也不能识别文件名含有
_代替


###参考: