TA的每日心情 | 开心 2019-8-21 08:44 |
---|
签到天数: 163 天 [LV.7]常住居民III
|
本帖最后由 mikeee 于 2019-2-6 22:50 编辑
1 l( x/ c: X( r- u4 U; v4 o6 l% @9 q" V7 |' v v
再来用 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! O1 I% o5 N3 F# x
! f9 ^; c1 F$ O+ ^; q- file = r'C:\Users\xyz\Downloads\Dictionary of Cliches (Facts on File)\b67f0a6e-1e61-11e9-8f58-0cc47a792c0a_id_b67f0a6e-1e61-11e9-8f58-0cc47a792c0a.html'
4 \1 o& |, \% e0 f - doc = pq(open(file, encoding='utf8').read())7 Y( b3 a3 |" l% ?& X; p" ~: l8 D
4 F$ h; F; `* T) ^) s- hw_css = 'div.cls_025>span.cls_025'
: X$ v& \& z% i! I* @; ] - ctx0_css = 'div.cls_025>span.cls_023'
0 _: k+ o( U) f) ]0 u/ Z+ p - ctx0a_css = 'div.cls_025>span.cls_028'3 i2 m6 ^* k& o- w0 {' F. e! \
- ctx0b_css = 'div.cls_028' # capital, references+ M0 j) n3 {) H/ \& u1 g! D7 ?7 c
- ctx0c_css = 'div.cls_027' # italics, book names8 I6 k C2 F# w' |9 x
- ctx1_css = 'div.cls_023'7 k9 w3 V7 P+ s9 C2 B1 M
- hw_ctx_css = f'{hw_css},{ctx0_css},{ctx0a_css},{ctx0b_css},{ctx0c_css},{ctx1_css}'
6 Y7 f. n* R; t* k" x - 1 e6 O8 I/ M& y, F" i$ t! l
- # css selector 到最后比我预期的复杂些, 所以用 abbyy finereader 可能比用 pdftohtml.net 简单些。
, D8 i) V) J$ U. s1 ^- W4 g' | - items = doc(hw_ctx_css)
复制代码 我们要的东西在 items 里。稍微处理一下 items 得到 由(词头,词义)组成的 entries- entries = []3 N4 S* [: X2 b
- hw = ''7 g" T8 h9 P) v
- ctx = ''
6 t& d( \, L! j( C: V4 q* K
+ B$ r# _% \+ Q8 S" H- upper_b = 50
" T$ T# J) F% S" s. B K - for elm in items[: upper_b]:4 q$ [$ C% K0 B0 r& Z
- tmp = pq(elm)
) I: a1 Z+ |/ B7 q& t( ^/ o& L - if tmp.attr('class') == 'cls_025':
& I9 J& u) D1 h* F
$ ]& r2 A, z3 S( [ r- entries += [(hw, ctx)]
* {, @. Y# _. L G5 I3 e
8 s6 \0 e$ S3 U+ i% J: p- hw = tmp.text()3 M! E4 |6 M8 s6 P6 H
- ctx = ''
$ ?6 @) M% g. e% D - else:
) U- X+ \4 k1 Z0 M. D - ctx += f' {tmp.text()}') w! I! n9 ^) R6 O, m0 c( W
- 0 b9 s1 S/ X, T, T5 V
- # collect the last batch6 W2 K7 B1 x7 C. q: h2 q
- entries += [(hw, ctx)]
+ n, u- n. G. I% U% E - 7 K9 o7 V9 B% X- ^4 s5 J# f
- def proc_func(ctx):; F7 l q3 E6 ^7 }. T
- ctx = ctx.strip() W% z7 U x1 Z$ c
6 C3 A( i7 |- V: f9 J: p: O( m- # insert a space after .: [a-z]\.)([^\s])
4 h- d( @, v$ t. \ - ctx = re.sub(r'([a-z]\.)([^\s])', r'\1 \2', ctx). Z) r, n: _# b
8 b, m8 d3 `5 @3 @( U D# x- # remove spaces/ L/ p! o( q3 E; A6 j' e$ s
- ctx = re.sub(r'\s\s+', r' ', ctx)
1 \' d: k$ A1 n+ n# X - return ctx& d& c2 p1 l9 {* N
- ' B( C- N; r3 g. R) C7 x
- entries = [(elm[0], proc_func(elm[1])) for elm in entries]
) G/ y, x$ m. }/ |# B' E
+ `" ]1 J; H7 N5 i$ i3 v- 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就会导致内容的遗失。 M5 J3 N( M3 W, ?% f8 V
8 u" U# P/ C% a6 u8 Y, X9 g) u |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
评分
-
1
查看全部评分
-
|