TA的每日心情 | 开心 2019-8-21 08:44 |
---|
签到天数: 163 天 [LV.7]常住居民III
|
本帖最后由 mikeee 于 2019-2-6 22:50 编辑
& O4 R1 m2 I5 q7 m; [* j" V# X, g9 F+ M/ x$ p/ T' Z2 S
再来用 PDF 做个 mdx,Dictionary of Cliches的pdf文件论坛里可以搜到。大致看了一下,估计两小时的工作量——最后用了不下10小时。步骤:- 上传pdf到 https://pdftohtml.net, 几分钟后下载转成了 html 的文件。
- 用 Chrome 打开解压后得到的 html 文件, F12调出 devtools。稍微研究一下后即可得知,div.cls_025>span.cls_025,div.cls_025>span.cls_023,div.cls_023 的 css selector 可以完美定位所需的内容。div.cls_025>span.cls_025 定位的是词头, div.cls_025>span.cls_023,div.cls_023定位词义。几行python搞定。(用 bs4 或lxml应该也是可以的, 其实pq是基于 lxml的etree元素。用node的cheerio的话基本思想差不多,码可能更简洁。用正则的话当然就用不了css selectors,可能会繁琐一点。)
- from pyquery import PyQuery as pq) j5 ` d% k1 @) V1 ~: s) w2 N/ Y7 b8 F
+ G& `/ [: d' P' ]4 x. \' }- file = r'C:\Users\xyz\Downloads\Dictionary of Cliches (Facts on File)\b67f0a6e-1e61-11e9-8f58-0cc47a792c0a_id_b67f0a6e-1e61-11e9-8f58-0cc47a792c0a.html'
( d4 ~8 f8 D: f9 \) Q0 q; n8 h - doc = pq(open(file, encoding='utf8').read())
) B$ ^4 ^( V, i/ F
+ r; ]3 K* p+ t* R- hw_css = 'div.cls_025>span.cls_025'
0 p/ ~& |8 L5 z5 b, t# {; z- \ - ctx0_css = 'div.cls_025>span.cls_023'" _6 `; j7 e" ?$ a8 l
- ctx0a_css = 'div.cls_025>span.cls_028'% P2 Q" K) k) J p6 o/ [) K
- ctx0b_css = 'div.cls_028' # capital, references p* [0 n n$ G) v. Q
- ctx0c_css = 'div.cls_027' # italics, book names% v) y: J( ^3 \0 D# D" J5 B: l
- ctx1_css = 'div.cls_023' w, L/ i5 Y* }$ W3 @
- hw_ctx_css = f'{hw_css},{ctx0_css},{ctx0a_css},{ctx0b_css},{ctx0c_css},{ctx1_css}'
4 w/ J) r8 f: p3 A. _& d
5 C3 k4 }( G5 V- # css selector 到最后比我预期的复杂些, 所以用 abbyy finereader 可能比用 pdftohtml.net 简单些。
9 I% V' b6 n$ [$ o `$ Q+ C) Z- ] - items = doc(hw_ctx_css)
复制代码 我们要的东西在 items 里。稍微处理一下 items 得到 由(词头,词义)组成的 entries- entries = []
4 R! i( I. T$ l* n! { - hw = ''. i8 s% N9 r, T1 W: j/ ]3 R5 S
- ctx = ''
) T7 G0 ~! c5 C3 l7 Z. ^2 r) {/ V - ) ~7 s" T, c6 c9 _0 @0 Y1 @
- upper_b = 50
+ T! O% c7 o$ P- A0 V" \9 n - for elm in items[: upper_b]:
* s1 T5 S# x% j# \# X - tmp = pq(elm)
# A T9 }, x5 n M) v+ y. x1 U - if tmp.attr('class') == 'cls_025':: z8 J% Z: X# m' H+ F% R* x0 b
- 4 Y1 r: d; Q* y8 t1 Q$ |
- entries += [(hw, ctx)]
) w P& X) z, a4 C6 Y9 A - 8 T: N |) B1 s
- hw = tmp.text()
7 ]: M% {) O2 k5 j - ctx = ''
* g2 L- z7 M$ g, d. _ e% Y - else:1 t% V B3 j5 k# ]
- ctx += f' {tmp.text()}'1 D) U5 ~! y _2 e
, w/ S% J b5 E* H/ ^- # collect the last batch
! m5 Y7 r! g$ M3 O! ~8 P - entries += [(hw, ctx)]
$ }, w- z) X& x! w! P; s3 D) \( V
: }' c: H+ t% y- def proc_func(ctx):
, I! ~8 B" d: C E( p - ctx = ctx.strip()
4 R& \/ M# s3 L+ x1 o+ ?# A: H
) r5 T2 a. @( q2 w- # insert a space after .: [a-z]\.)([^\s])
. n6 i4 o% q( B( C3 f1 s. p - ctx = re.sub(r'([a-z]\.)([^\s])', r'\1 \2', ctx)
) R$ j. m5 ?3 _- T- {
5 t p1 a% U# D% h. F: g# n- # remove spaces
& c; c" ]7 l' ^' M! B' S - ctx = re.sub(r'\s\s+', r' ', ctx)- a7 j. D1 I! k! Q/ E3 U k- U
- return ctx
$ e9 D# ^( z3 g) x: F- v - ' T* A* t! K* w8 q* M( e- |
- entries = [(elm[0], proc_func(elm[1])) for elm in entries]
+ N- B/ W0 Z& Z/ Z - - w% E# [8 W8 f$ j: d
- print(entries[:3])
复制代码 输出为- [('', '—Christine Ammer —Christine Ammer'), ('about face, to do an', 'To reverse a decision or change one’s opinion. The term comes from the American military command to turn 180 degrees at attention, dating from the mid-nineteenth century, and by 1900 was being used figuratively. A more recent colloquial usage is to do a 180, but it has not yet reached cliché status.'), ('about the size of it', 'An approximately accurate version of a situation, event, or circumstance. It generally is used as a summing up: “That’s about the size of it. ”')]
复制代码 基本大功告成了。其实后面还是有很多事要做。 - 转成(mdxbuilder可用的) mdxhtml 格式(其实也可以直接用writemdict直接做成 mdx)。pdftohtml.net 转的html含位置信息,要抽取词头和释义以及参考链接还是要费点周折。折腾了几个晚上。结果是上传的三个py文件(mapping_dict.py用于处理一些特殊的链接)。运行 gen_mdicthtml.py 即可生成 dict_of_cliches_mdict.html。用 mdxbuilder 处理 dict_of_cliches_mdict 即可得到 mdx 和 mdd。所有的 py、html及 css、封面 png 打成包(见附件),python码比较乱,没有整理,但Py3.6下是可以运行的,有兴趣的网友可以折腾一下。
- Mdxbuilder 处理 dict_of_cliches_mdict.html 生成 mdx、mdd。(见附件)词头做了分拆,有交叉索引链接。见下图。
- 改进:找时间再做个可以查独立单词或词组的详细索引,进一步提高字典的可用性。
- 结语:如果 pdf 文件的内容是英文的并且可以拷出来,用 pdftohtml.net 转 html 再用 css selector 可以非常方便的自动处理词头和释义。但也有不少坑——漏掉一类 css selector就会导致内容的遗失。
0 f6 d) }+ \& u7 b# n9 F( H9 J # R6 l3 k9 P9 E* I) J
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
评分
-
1
查看全部评分
-
|