# 某汉英词典解析代码

**URL:** <https://forum.freemdict.com/t/topic/4883>\
**Category:** 技术交流与词典编修\
**Created:** [2021 年2 月 5 日 02:52 UTC](https://forum.freemdict.com/t/topic/4883 "2021-02-05T02:52:36Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

作者： ![Quantus](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/q/e495f1/32.png) [@Quantus](https://forum.freemdict.com/u/Quantus)\
发布日期： [2021 年2 月 19 日 07:23 UTC](https://forum.freemdict.com/t/topic/4883/26 "2021-02-19T07:23:02Z")

</div>

我在这儿提供一个修改拼音的Python程序。  
[py.py](https://forum.freemdict.com/uploads/short-url/xNlr08aNSzp0tg6kitsouuDJWTw.py) (559 字节)  
[py.txt](https://forum.freemdict.com/uploads/short-url/rFM94BspHwKfU02cimL66zBTqCB.txt) (954 字节)

用法：  
把上面这两个文件，以及你已经用楼主转换好的txt文件，改名为CC-CEDICT.txt（或者你进py.py文件把里面的相应的词典名改成你的那个txt的名字），然后运行就行了。

注：因为列举所有完整的拼音太麻烦了，我懒得弄，py.txt里面就只有韵母部分的替换，难免有正常文字部分恰好含有这些字符而被替换掉了，谁发现在知会我一声儿，或者做一个完整的拼音替换列表，也烦请知会我一声儿。另外，如果你是在Linux下运行的这个程序，得到了一个替换后的CC-CEDICT.txt文件，但是当你把这个txt拷到Windows电脑上的时候，在mdxbuilder做成词典时会有问题，这时你只要在Windows上把txt内容复制并粘贴到一个新的txt里就好了，我觉得主要原因就是Linux和Windows换行符不一样导致的。

---

<div class="post-metadata">

作者： ![lurker](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/lurker/32/16016_2.png) [@lurker](https://forum.freemdict.com/u/lurker)\
发布日期： [2021 年4 月 19 日 15:07 UTC](https://forum.freemdict.com/t/topic/4883/28 "2021-04-19T15:07:17Z")

</div>

重新做了一版 CC-CEDICT。修了一下拼音（儿化音、匹配、缺省等方面）。

[extract11.py](https://forum.freemdict.com/uploads/short-url/7MwFlQnYQKZLQVmUxQXjd1knDhj.py) (2.5 KB)

---

<div class="post-metadata">

作者： ![王绍军](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/%E7%8E%8B/e95f7d/32.png) [@王绍军](https://forum.freemdict.com/u/%E7%8E%8B%E7%BB%8D%E5%86%9B)\
发布日期： [2021 年4 月 19 日 15:17 UTC](https://forum.freemdict.com/t/topic/4883/29 "2021-04-19T15:17:13Z")

</div>

lurker哥威武

---

<div class="post-metadata">

作者： ![sculiuchang](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@sculiuchang](https://forum.freemdict.com/u/sculiuchang)\
发布日期： [2021 年4 月 20 日 04:58 UTC](https://forum.freemdict.com/t/topic/4883/30 "2021-04-20T04:58:32Z")

</div>

制作辛苦，多谢哈。

---

<div class="post-metadata">

作者： ![lurker](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/lurker/32/16016_2.png) [@lurker](https://forum.freemdict.com/u/lurker)\
发布日期： [2021 年6 月 25 日 02:59 UTC](https://forum.freemdict.com/t/topic/4883/31 "2021-06-25T02:59:08Z")

</div>

我通常不看好自己写的代码，但本帖这份，实有可圈可点之处，有心人多年后应该还能用它来解析 CC-CEDICT。用到它的人，可以的话，请分享产出。

---

<div class="post-metadata">

作者： ![青稚Quentin](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/%E9%9D%92%E7%A8%9Aquentin/32/14413_2.png) [@青稚Quentin](https://forum.freemdict.com/u/%E9%9D%92%E7%A8%9AQuentin)\
发布日期： [2021 年10 月 4 日 01:12 UTC](https://forum.freemdict.com/t/topic/4883/32 "2021-10-04T01:12:31Z")

</div>

请问我需要怎么调整 才能生成下面的样式？

我暂时无法将 `<span class="cc_trad">21三體綜合症 21三体综合症</span>`

改成

```auto
<span class="cc_trad">21三體綜合症</span>
<span class="cc_simp">21三体综合症</span>

```

  

这是我目前的 extract.py 文件

```auto
        headwords = list(dict.fromkeys(line.split(" [")[0].split()))
        pinyin = line.split(" [")[1].split("] ")[0]
        definitions = line.split(" /")[1].strip("/").split("/")

        css = '<link href="cedict.css" rel="stylesheet">'
        header = '<div class="cc_wrapper"><div class="cc_header"><span class="cc_trad">' + ' '.join(headwords) + '</span></div><div class="cc_header_pinyin">' + pinyin + '</div>'
        defs = '<div class="cc_defs"><p class="cc_def">' + ' '.join(definitions) + '</p></div></div>'

```

我希望能生成这样的文件

```auto
21三体综合症
<link href="cedict.css" rel="stylesheet">
<div class="cc_wrapper">
    <div class="cc_header">
        <span class="cc_trad">21三體綜合症</span>
        <span class="cc_simp">21三体综合症</span>
    </div>
    <div class="cc_header_pinyin">èr shí yī sān tǐ zōng hé zhèng</div>
    <div class="cc_defs">
        <p class="cc_def">trisomy</p>
        <p class="cc_def">Down's syndrome</p>
    </div>
</div>

```

  

我目前只能生成下面的样式

```auto
21三体综合症
<link href="cedict.css" rel="stylesheet">
<div class="cc_wrapper">
    <div class="cc_header">
        <span class="cc_trad">21三體綜合症 21三体综合症</span>
    </div>
    <div class="cc_header_pinyin">èr shí yī sān tǐ zōng hé zhèng</div>
    <div class="cc_defs">
        <p class="cc_def">trisomy Down's syndrome</p>
    </div>
</div>

```

  
[extract.py|attachment](upload://6FLBUez5RJT7ldwyE7IwPBMSKqJ.py) (1.4 KB)

---

<div class="post-metadata">

作者： ![okayer](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/okayer/32/16083_2.png) [@okayer](https://forum.freemdict.com/u/okayer)\
发布日期： [2021 年10 月 4 日 04:54 UTC](https://forum.freemdict.com/t/topic/4883/35 "2021-10-04T04:54:35Z")

</div>

分享下我的流程，假设cedict\_ts.u8原始词条：

龠 龠 [yue4] /ancient unit of volume (half a 合[ge3], equivalent to 50ml)/ancient flute/

1. 仅替换第一个 [和] 为Tab \t
2. 以\t为分隔符，拆分出字头、拼音cc\_header\_pinyin、释义
3. 以空格为分隔符，拆分出繁体字头cc\_trad、简体字头cc\_simp。
4. 以 / 分隔符，拆分出英文释义cc\_def

这样后续处理排版就很方便了

 ![Screen Shot 2021-10-04 at 12.39.15 PM](https://forumcdn.freemdict.com/uploads/default/original/3X/c/a/cadf75acd57fbc339103821d8b70e9e83171c067.png)

---

<div class="post-metadata">

作者： ![青稚Quentin](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/%E9%9D%92%E7%A8%9Aquentin/32/14413_2.png) [@青稚Quentin](https://forum.freemdict.com/u/%E9%9D%92%E7%A8%9AQuentin)\
发布日期： [2021 年10 月 4 日 07:43 UTC](https://forum.freemdict.com/t/topic/4883/38 "2021-10-04T07:43:57Z")

</div>

跟你说的去重没关系， 我只是想知道 `header` 怎么写 才能将显示我想要的结果 我看了你的`extract11.py` 文件 然后修改成下面这样 就可以实现效果了

```auto
header = '<div class="cc_wrapper"><div class="cc_header"><span class="cc_trad">' + '</span><span class="cc_simp">'.join(headwords) + '</span></div><div class="cc_header_pinyin">' + pinyin + '</div>'

```

目前效果

```auto
502胶
<link href="cedict.css" rel="stylesheet">
<div class="cc_wrapper">
    <div class="cc_header">
        <span class="cc_trad">502膠</span>
        <span class="cc_simp">502胶</span>
    </div>
    <div class="cc_header_pinyin">wǔ líng èr jiāo</div>
    <div class="cc_defs">
        <p class="cc_def">cyanoacrylate glue</p>
    </div>
</div>
</>

```

---

<div class="post-metadata">

作者： ![青稚Quentin](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/%E9%9D%92%E7%A8%9Aquentin/32/14413_2.png) [@青稚Quentin](https://forum.freemdict.com/u/%E9%9D%92%E7%A8%9AQuentin)\
发布日期： [2021 年10 月 4 日 07:47 UTC](https://forum.freemdict.com/t/topic/4883/39 "2021-10-04T07:47:36Z")

</div>

![CleanShot 2021-10-04 at 15.47.03@2x](https://forumcdn.freemdict.com/uploads/default/original/3X/9/2/92c2bdd000aed71374e5b476a8c7e4263fa5c54a.jpeg)

---

<div class="post-metadata">

作者： ![lurker](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/lurker/32/16016_2.png) [@lurker](https://forum.freemdict.com/u/lurker)\
发布日期： [2021 年10 月 4 日 08:14 UTC](https://forum.freemdict.com/t/topic/4883/40 "2021-10-04T08:14:44Z")

</div>

你这样改，繁简同形的词头、繁体词头用的都是 span.cc\_trad 。不能实现 okayer 那种排版。

---

<div class="post-metadata">

作者： ![青稚Quentin](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/%E9%9D%92%E7%A8%9Aquentin/32/14413_2.png) [@青稚Quentin](https://forum.freemdict.com/u/%E9%9D%92%E7%A8%9AQuentin)\
发布日期： [2021 年10 月 4 日 08:36 UTC](https://forum.freemdict.com/t/topic/4883/41 "2021-10-04T08:36:41Z")

</div>

> [@lurker](#):
>
> okayer 那种排版

没事 我的这样的排版也行，不行可以把繁体词头隐藏起来

---

<div class="post-metadata">

作者： ![lurker](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/lurker/32/16016_2.png) [@lurker](https://forum.freemdict.com/u/lurker)\
发布日期： [2021 年10 月 4 日 08:44 UTC](https://forum.freemdict.com/t/topic/4883/42 "2021-10-04T08:44:39Z")

</div>

这种写法，很难隐藏繁体词头。也不建议隐藏，或制作简体版，因为存在一种情况：

> 㥦 惬 [qie4] /variant of 愜|惬[qie4]/

很好奇你是怎么处理拼音音调的。

---

<div class="post-metadata">

作者： ![青稚Quentin](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/%E9%9D%92%E7%A8%9Aquentin/32/14413_2.png) [@青稚Quentin](https://forum.freemdict.com/u/%E9%9D%92%E7%A8%9AQuentin)\
发布日期： [2021 年10 月 4 日 09:14 UTC](https://forum.freemdict.com/t/topic/4883/43 "2021-10-04T09:14:17Z")

</div>

我用的这个 [py.txt](https://forum.freemdict.com/uploads/short-url/rFM94BspHwKfU02cimL66zBTqCB.txt) (954 字节)

---

<div class="post-metadata">

作者： ![lurker](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/lurker/32/16016_2.png) [@lurker](https://forum.freemdict.com/u/lurker)\
发布日期： [2021 年10 月 4 日 09:25 UTC](https://forum.freemdict.com/t/topic/4883/44 "2021-10-04T09:25:56Z")

</div>

py.txt 没有考虑到所有情形，还可能误替换。不妨看看我在 extract11.py 里是怎么处理拼音的。

---

<div class="post-metadata">

作者： ![青稚Quentin](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/%E9%9D%92%E7%A8%9Aquentin/32/14413_2.png) [@青稚Quentin](https://forum.freemdict.com/u/%E9%9D%92%E7%A8%9AQuentin)\
发布日期： [2021 年10 月 4 日 09:36 UTC](https://forum.freemdict.com/t/topic/4883/45 "2021-10-04T09:36:17Z")

</div>

我也不是很懂， 这是在另一个帖子看到的 [CC-CEDICT制作mdx自助](https://forum.freemdict.com/t/topic/5555)

---

<div class="post-metadata">

作者： ![hjtoh](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/h/e99b99/32.png) [@hjtoh](https://forum.freemdict.com/u/hjtoh)\
发布日期： [2021 年10 月 5 日 05:21 UTC](https://forum.freemdict.com/t/topic/4883/46 "2021-10-05T05:21:21Z")

</div>

隔壁ff和bt4baidu技术最好，ff被迫离开，bt4baidu潜水了，其他人的水准都差一个档次，ff离开真的是重大损失。

---

<div class="post-metadata">

作者： ![lurker](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/lurker/32/16016_2.png) [@lurker](https://forum.freemdict.com/u/lurker)\
发布日期： [2021 年10 月 5 日 06:02 UTC](https://forum.freemdict.com/t/topic/4883/47 "2021-10-05T06:02:51Z")

</div>

fearfare090807 即使没跟站务闹矛盾，也不可能十年如一日地制作 MDX。人能自由支配的时间本就不多，其中一小部分用于学习，学习时间的一小部分用于查字典。FF 抽时间制作了他要用、众人也用得着的 MDX，做完就撤了，这结局很好，非要他留下来继续做公益，那就成他的“重大损失”了。

---

<div class="post-metadata">

作者： ![hjtoh](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/h/e99b99/32.png) [@hjtoh](https://forum.freemdict.com/u/hjtoh)\
发布日期： [2021 年10 月 5 日 08:17 UTC](https://forum.freemdict.com/t/topic/4883/48 "2021-10-05T08:17:16Z")

</div>

我想ff就是单纯地把自己的杰作分享给大家，当然他自愿什么时候停止都是ok的，甚至ff的作品收点费用我都觉得是ok的，因为质量过硬，可能ff自己都没楼主想的那么远，那么功利。

---

<div class="post-metadata">

作者： ![lurker](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/lurker/32/16016_2.png) [@lurker](https://forum.freemdict.com/u/lurker)\
发布日期： [2021 年10 月 5 日 09:07 UTC](https://forum.freemdict.com/t/topic/4883/49 "2021-10-05T09:07:59Z")

</div>

你不功利，你来替我免费干活吧。FF ，你，我，所有人的时间都是宝贵的。别慷他人之慨。

---

<div class="post-metadata">

作者： ![hjtoh](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/h/e99b99/32.png) [@hjtoh](https://forum.freemdict.com/u/hjtoh)\
发布日期： [2021 年10 月 5 日 09:27 UTC](https://forum.freemdict.com/t/topic/4883/50 "2021-10-05T09:27:43Z")

</div>

我没有慷他人之慨，本站一切建立在自愿的基础上，也是来去自由，我也对你的工作和作品不感任何兴趣，如果功利二字伤到你的自尊心了，说声抱歉，就此打住。

[上一页](https://forum.freemdict.com/t/topic/4883.md?page=1)

[下一页](https://forum.freemdict.com/t/topic/4883.md?page=3)
