一般青年的选择

  1. 去威锋, 简单的说, 威锋上面很多, 在他的软件分享区.
  2. http://bbs.feng.com/read-htm-tid-7678325.html

文艺青年的选择

  1. 去知乎, 去豆瓣 上面有很多解说
  2. 转换工具:
    1. 知乎上面推这个, https://github.com/ilius/pyglossary
    2. 豆瓣上面推这个, https://github.com/jjgod/mac-dictionary-kit/
  3. 下载各种字典工具的字典, 比如star……暴多.
  4. 然后开始体会转换的酸爽, 剧很多人都说, 感觉就是死机的.
  5. 参考链接
    1. https://www.zhihu.com/question/20428599
    2. https://www.douban.com/group/topic/9591106/?author=1#sep

其他青年

  1. 小词典, 竟然是安装包, 而且作者还没有授权: http://xiaocidian.com/download/
  2. 还有很多类似的安装包.

安装位置

位置: ~/Library/Dictionaries

2b的选择

参考位置: https://www.zhihu.com/question/20428599
这个链接里面有几本字典下载, 亲测是质量最好, 如果是一般青年, 直接下载吧.
https://www.pdawiki.com/forum/thread-13014-1-1.html
看明白了, 这个就是pyglossary大神的作品: https://github.com/ilius/pyglossary

  • wav插件 for mac
    • 其实大神已经转化为mp3了, 所以完全是好的, 这个只是某个字典的问题, 完全不必理会.
    • 但是, 如果我们搞定wav4mac的插件, 那么我们就不用转化音频文件了.
  • olcc和4合一里面的音标是啥字体. Kingsoft Phonetic Plain 已经下载了.
  • 查看下字典尺寸. longman5和macmillan最大, longman带例句发音.

具体做法: —–我没看, 也没弄, 但是这个具体做法就是大神的具体做法, 知乎和pda的原始链接在前面

不过使用了一段时间后发现词典内链接跳转不正常,不能发音、图片不能显示等种种小问题。研究了一会儿之后终于把各个问题都解决了。这样词典既有原生dictionary的良好体验,又实现了GoldenDict、欧路词典下拓展词库的完善功能。

下面简要简要记录由Mdict词典文件转换成完美dictionary文件的过程如下。

开源项目

PYGLOSSARY:最新Releases见ilius/pyglossary/releases

python-lzo:

sudo pip3 install python-lzo
准备好以下环境:

Writing to AppleDict
sudo pip3 install lxml beautifulsoup4 html5lib
GNU make as part of Command Line Tools for Xcode.
Dictionary Development Kit as part of Auxillary Tools for Xcode. Extract to/Developer/Extras/Dictionary Development Kit
转换过程

用PYGLOSSARY进行转换
假设词典文件为~/Downloads/oald8/oald8.mdx, 图片、语音文件oald8.mdd也在同一文件夹下。

先将词典文件转换成xml文件,并将其他图片、音频等文件提取出来放在OtherResources文件夹下。

cd ~/Downloads/oald8/
python3 ~/Software/pyglossary/pyglossary.pyw --read-options=resPath=OtherResources --write-format=AppleDict oald8.mdx oald8-apple
cd oald8-apple
在PYGLOSSARY的说明中接下来是将xml文件中的相对链接中的"/"去掉以及替换spx为wav。

sed -i "" 's:src="/:src=":g' oald8.xml
sed -i "" 's|sound://\([/_a-zA-Z0-9]*\).spx|\1.wav|g' oald8.xml
其实这两步可以在之后用正则统一替换。

转换语音文件
如果是spx文件,先转换成wav(需安装speex)。若是wav文件则可跳过这一步。

find OtherResources -name "*.spx" -execdir sh -c 'spx={};speexdec $spx  ${spx%.*}.wav' \;
然后用FFmpeg把wav文件转换成mp3文件(Mac默认的iTunes与QuickTime均不支持wav)。

新建sh脚本文件如下,并执行脚本。

for x in ./OtherResources/*.wav; do ffmpeg -i "$x" "Sounds/`basename "$x" .wav`.mp3"; done
如果语音文件较多的话需等待较长时间。朗文当代第五版中近二十万个音频文件我顶配的15寸 Pro也转换了半天...

编译
make
有时生成的词典包文件中并没有图片、音频文件。这时将OtherResources文件夹中除音频外的其他文件以及放置mp3文件的Sounds文件夹复制到xxx.dictionary/Contents目录下(右键dictionary文件,选择Show Package Contents)。

安装
将生成的dictinary文件复制到当前用户文件下下的/Library/Dictionaries中,之后在词典->偏好设置中启用一下就行。

修正词典文件的各种问题

以上方法生成的词典会有开头说到的词典内链接跳转不正常,不能发音、图片不能显示等种种小问题。下面修复这些问题。

Dictionary的URI Scheme介绍
以下引自Dictionary Services Programming Guide。

x-dictionary: is an URI scheme that describes cross references between entries in dictionaries.  It is used in tag such as <a href="x-dictionary:r:another_id">.

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.

x-dictionary:d:key_text:dict_bundle_id
x-dictionary:r:reference_id:dict_bundle_id
The dictionary bundle ID can be omitted in both forms, as shown in the following lines.  If it is omitted, Dictionary Services searches the target text in all active dictionaries.

x-dictionary:d:key_text
x-dictionary:r:reference_id
修复词典中的链接跳转问题。
举例来说,假设dict_bundle_id为Longman5(dict_bundle_id可在生成的词典文件中的Info.plist中修改。)。

假设xml文件中为:

<a href="x-dictionary:d:entry://hour">hour</a>
可替换为:

<a href="x-dictionary:d:hour:Longman5">hour</a>
可用正则表达式匹配然后处理。

修正词典中页面内的定位问题
例如点击apple1会跳转到同一个页面中的apple2处。

<a href="x-dictionary:d:entry://#_hke1">apple1</a>
<a name="_hke1">apple2</a>
将其替换为

<a href="#_hke1">apple1</a>
<a name="_hke1">apple2</a>
可用正则表达式匹配然后处理。

修复词典的发音问题
假设xml文件中为:

<a href="x-dictionary:d:sound://1.spx"><img border="0" src="Br.gif"/></a> 
可替换为

<audio id="1" src="Sounds/1.mp3"/><img border="0" src="Br.gif" onmousedown="document.getElementById('1').play(); return false;"/> 
onmousedown可替换成onmouseover,这样鼠标悬浮上发音图标就开始发音,而不用单击。

可用正则表达式匹配然后处理。

代码示例
以Longman5_Activator为示例,假设只需要修复链接跳转问题和页面内的定位问题。写得比较随便,随意看一下就好。

当然有些编辑器直接支持正则表达式查找和替换,这样就不用写程序了。

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Replace {
    static final String REGEX_1 = "x-dictionary:d:entry:\\/\\/#";
    static final String REGEX_2 = "entry:\\/\\/([^#\"][^\"]*)\"";
    static final String Bundle_Id = "Longman5_Activator";
    static final String inputName = "Longman5_Activator.xml";
    static final String outputName = "Longman5_Activator_Output.xml";
    static final String ENCODE = "UTF-8";
    BufferedReader br = null;
    BufferedWriter bw = null;

    public static void main(String[] args) throws IOException {
        new Replace().replaceFile();
    }

    public String replacefun1(String line) {
        Pattern pattern = Pattern.compile(REGEX_1);
        StringBuffer sbr = new StringBuffer();
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            matcher.appendReplacement(sbr, "#");
        }
        matcher.appendTail(sbr);
        return sbr.toString();
    }

    public String replacefun2(String line) {
        Pattern pattern = Pattern.compile(REGEX_2);
        StringBuffer sbr = new StringBuffer();
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            matcher.appendReplacement(sbr, matcher.group(1) + ":" + Bundle_Id + "\"");
        }
        matcher.appendTail(sbr);
        return sbr.toString();
    }

    public void replaceFile() throws IOException {
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    inputName), ENCODE));
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(outputName)));
            String line = null;
            while ((line = br.readLine()) != null) {
                bw.write(replacefun2(replacefun1(line)));
                bw.newLine();
            }
        } catch (IOException e) {
        } finally {
            bw.close();
            br.close();
        }

    }

}
其他注意事项

文本匹配的正则表达式需一一调试
以上每个词典文件中的具体模式均不太一样,而且有时会有与一般模式不同的个例(如多加了几个空格、标签间加了个<br/>、中间换了一行、图片大多是png个别是gif等等),需要一一调试。推荐调试网站Regexr。

空格相关问题
连续两个粗体单词之间的空格会不显示。例如

<b>apple<\/b> <b>pie</b>
会显示为applepie。此时将其替换为

<b>apple<\/b>&#160;<b>pie</b>
注意xml中不能用&nbsp;。

另外有时xml中会有&amp;nbsp;,原意应显示为为空格,却显示为&nbsp;。此时将其替换为&#160;就好。

取词小窗口修改大小
若取词小窗口中字体太小,可在词典包内容中的DefaultStyle.css文件中加上以下内容:

.apple_client-panel body{margin: 0 0 15px;font-size: 16px;}
CSS样式文件
若OtherResources文件夹中有其它CSS文件,或者原来CSS文件就独立于MDict文件之外,将其内容复制到词典文件包中的DefaultStyle.css文件中

其它问题
将@@@LINK=abc替换成<a href="x-dictionary:d:abc:dict_bundle_id">abc</a>.

将相对路径名最前面的/去掉,例如将<img src="/dir/abc.png"/>替换成<img src="dir/abc.png"/>.

其他功能
Dictinary还支持高亮搜索关键词、家长控制功能等等,参考之前提到过的Dictionary Services Programming Guide。