TA的每日心情 | 开心 2019-8-21 08:44 |
|---|
签到天数: 163 天 [LV.7]常住居民III
|
本帖最后由 mikeee 于 2019-2-6 22:50 编辑 ) i) Y& J5 m, O+ \: V
6 D3 m, k1 |& {" o5 B# E+ X; w
再来用 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
" i8 Z$ O! C; o* S) W8 a' k& s6 P - ' S! V5 c! q/ w* g0 }2 X, H v( I
- file = r'C:\Users\xyz\Downloads\Dictionary of Cliches (Facts on File)\b67f0a6e-1e61-11e9-8f58-0cc47a792c0a_id_b67f0a6e-1e61-11e9-8f58-0cc47a792c0a.html'3 \) g" Z. H: O* g+ r7 ~
- doc = pq(open(file, encoding='utf8').read())5 S( J& T: v& s+ U3 J/ I7 K1 [
- # c- ^/ R4 C5 H: A- ^* ]6 _, D" `9 D/ W
- hw_css = 'div.cls_025>span.cls_025'
9 p8 V6 Y1 ^: i - ctx0_css = 'div.cls_025>span.cls_023'7 z- F; f- A( G! a- v: @1 J, _
- ctx0a_css = 'div.cls_025>span.cls_028'
/ r1 U) _$ W4 d3 E [0 A5 \0 @ - ctx0b_css = 'div.cls_028' # capital, references
, ?# `& {' ]' H: \$ Y) w. c& h - ctx0c_css = 'div.cls_027' # italics, book names2 o, C6 g3 D0 F0 @* V
- ctx1_css = 'div.cls_023'
# Y e0 ]" @) B' n" @% o# k' H - hw_ctx_css = f'{hw_css},{ctx0_css},{ctx0a_css},{ctx0b_css},{ctx0c_css},{ctx1_css}'* P: Z- F$ S3 r4 M8 `" z \" j1 y
- 2 w- j, v% H; d5 q6 z* i
- # css selector 到最后比我预期的复杂些, 所以用 abbyy finereader 可能比用 pdftohtml.net 简单些。7 A4 w" T4 m% _' [/ w! L
- items = doc(hw_ctx_css)
复制代码 我们要的东西在 items 里。稍微处理一下 items 得到 由(词头,词义)组成的 entries- entries = []" e# ^; [1 X- }2 z7 M; j
- hw = ''
: G8 x0 v, r' U* h1 _ - ctx = ''
2 }( i' K) k, }* I5 V, H1 D# ~, N - * a7 Q, s1 ]0 |# X& c3 y
- upper_b = 50
: i+ H: w: @+ P4 y2 I4 B7 s( y- K - for elm in items[: upper_b]:; h, d+ i1 e, }/ U% k
- tmp = pq(elm)
& N& b& l* g; X0 Z# U5 r - if tmp.attr('class') == 'cls_025':
" u0 R: n+ A; b1 t1 X
/ z* N( Z! {+ m) a8 U- entries += [(hw, ctx)]
: x( W1 A$ x, z1 x
! V/ F3 F; k# S. p- C- hw = tmp.text()
; H) w6 A b$ i8 n - ctx = ''
M2 H; L6 J$ J! `) w5 I - else:- \4 I" Z2 U7 m: R" j- b
- ctx += f' {tmp.text()}'
6 ^1 }$ l8 t& d/ N - ! ?( x m/ O/ ]$ Z0 K
- # collect the last batch+ W {* j+ S4 v: g
- entries += [(hw, ctx)]3 a) }. k6 C: H2 j* H' V* p
6 y' W; f x |: R6 g9 S- def proc_func(ctx):
/ s2 g( I; q+ r" s) u9 x4 g8 T D) ~ - ctx = ctx.strip()
0 u$ ?" F) ? K& r: ~
( V' Y8 @6 S, X7 }* `- # insert a space after .: [a-z]\.)([^\s]); S' Y6 b4 C E6 F
- ctx = re.sub(r'([a-z]\.)([^\s])', r'\1 \2', ctx)
8 F( F+ m4 t2 `0 V. O2 @, f5 e - - ~* ~6 Q8 g* Y4 z
- # remove spaces
7 t- R% ]9 D7 Q9 R3 d0 V - ctx = re.sub(r'\s\s+', r' ', ctx)' ^8 w. v, n1 k
- return ctx! e9 b4 m) Y( w% {# q
% X4 y% Z: Z- D9 M P. \- entries = [(elm[0], proc_func(elm[1])) for elm in entries]+ w# h3 z$ `5 I
" x* i; e2 X* x# u6 }# R/ q- 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就会导致内容的遗失。4 Z, O( c% l$ o6 Q% E
" ]9 s/ C; E9 N* @% O: G |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
评分
-
1
查看全部评分
-
|