掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 4736|回复: 8

[讨论] 【完成】pdf 转 mdx:Dictionary of Cliches (Facts on File)

[复制链接]
  • TA的每日心情
    开心
    2019-8-21 08:44
  • 签到天数: 163 天

    [LV.7]常住居民III

    发表于 2019-1-23 01:19:47 | 显示全部楼层 |阅读模式
    本帖最后由 mikeee 于 2019-2-6 22:50 编辑
    - L8 M/ d  P2 h' V! K$ ^; K) ~  k: [/ L) C, l6 M4 {4 M
    再来用 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,可能会繁琐一点。)
      1. from pyquery import PyQuery as pq
        ( O/ X* J$ P2 O! P

      2. - J) w" y; s5 ]2 q0 O4 ~' I
      3. file = r'C:\Users\xyz\Downloads\Dictionary of Cliches (Facts on File)\b67f0a6e-1e61-11e9-8f58-0cc47a792c0a_id_b67f0a6e-1e61-11e9-8f58-0cc47a792c0a.html'8 F" u/ w& Y% g, z; k3 B+ h
      4. doc = pq(open(file, encoding='utf8').read())6 N- N( P( M" y. \$ U) H

      5. 6 |: v8 _8 q$ @! z/ S8 n% `6 w
      6. hw_css = 'div.cls_025>span.cls_025'
        0 T8 b6 V5 ~# u" J2 p& y& \
      7. ctx0_css = 'div.cls_025>span.cls_023'% d$ W: a4 C9 P* a5 @
      8. ctx0a_css = 'div.cls_025>span.cls_028'
        ; x# Y: J! D4 L. l7 |
      9. ctx0b_css = 'div.cls_028'  # capital, references. f4 C: O  n# n7 c
      10. ctx0c_css = 'div.cls_027'  # italics, book names; V# e" R' u1 ~9 `* ?2 a/ M
      11. ctx1_css = 'div.cls_023'
        / {9 e+ w4 C* H; u7 N2 w) ]7 Z
      12. hw_ctx_css = f'{hw_css},{ctx0_css},{ctx0a_css},{ctx0b_css},{ctx0c_css},{ctx1_css}'
        ; b2 a9 }7 n1 c3 z$ S
      13. 7 F; s" G. u* W+ s
      14. # css selector 到最后比我预期的复杂些, 所以用 abbyy finereader 可能比用 pdftohtml.net 简单些。+ L: }7 [) ?, c8 y3 _. w! A
      15. items = doc(hw_ctx_css)
      复制代码
      我们要的东西在 items 里。稍微处理一下 items 得到 由(词头,词义)组成的 entries
      1. entries = []" k0 S- a/ ^' t0 K( a) M
      2. hw = ''! `# t! T0 b' v' n3 I
      3. ctx = ''& f; P) U4 r  R+ _+ Z
      4. 5 `7 f2 f" V5 L3 H- i
      5. upper_b = 50
        ! g5 ?3 C0 I6 e5 A& g0 E0 }
      6. for elm in items[: upper_b]:
        / c3 C5 h/ Y4 K8 Z% q! Q
      7.     tmp = pq(elm)+ A6 \* j0 ?. P' _8 j& m7 N
      8.     if tmp.attr('class') == 'cls_025':$ X# B: \, P* X9 P

      9. ! G$ q: a1 w- g* F
      10.         entries += [(hw, ctx)]" X$ D) a% [* ^0 T: V8 X9 L
      11. 6 {' G" P0 [7 U
      12.         hw = tmp.text()
        4 F, g5 h3 h! @% J5 w" j1 E7 B
      13.         ctx = ''
        # `2 ~8 A3 D- B+ r8 B$ A; B
      14.     else:
        ' H+ H* W- M/ p( c6 s/ S5 z
      15.         ctx += f' {tmp.text()}'
        1 j4 V' o/ L' \9 E9 x& ?
      16. 7 K) T2 A, C/ }2 D
      17. # collect the last batch
        , f9 Q- i+ w% b& D- A
      18. entries += [(hw, ctx)]
        ) J$ N0 D" A1 U( g7 h+ D4 E  w2 D) b
      19. $ f7 \7 q; s$ L: T
      20. def proc_func(ctx):6 ]& B! A2 P5 G5 n/ D
      21.     ctx = ctx.strip()3 }! P6 s. a4 A5 ?/ y8 ^" @, a

      22. 1 N5 p0 v" a# g3 N8 g; ^) s  a
      23.     # insert a space after .: [a-z]\.)([^\s])% T% Q' k! ^8 C: P  ?# u
      24.     ctx = re.sub(r'([a-z]\.)([^\s])', r'\1 \2', ctx)
        + u# H8 }& G! j3 q" [5 y! W& ~
      25. 3 P. u8 a3 o% @& q& b. T
      26.     # remove spaces4 w+ ]9 m/ F# Y% f7 j/ Z) y& w
      27.     ctx = re.sub(r'\s\s+', r' ', ctx)/ L5 F4 z7 g; l0 ~3 H
      28.     return ctx
        ! [3 J0 Q0 z! y# l. C1 M
      29. ' e/ d" g3 V6 W% p% ~
      30. entries = [(elm[0], proc_func(elm[1])) for elm in entries]
        $ R( R" R0 D; g0 F/ ?2 y

      31. 4 d2 p, [% C& k" h6 H  L  g
      32. print(entries[:3])
      复制代码
      输出为
      1. [('', '—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就会导致内容的遗失。* S3 s7 W; c0 i( N/ \3 q3 o6 _

    ' E  D' t. j. @% I. [2 O3 j5 t* y

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?免费注册

    x

    评分

    1

    查看全部评分

    本帖被以下淘专辑推荐:

  • TA的每日心情
    奋斗
    2019-4-14 02:12
  • 签到天数: 93 天

    [LV.6]常住居民II

    发表于 2019-1-23 01:35:09 | 显示全部楼层
    厉害了,要跟楼主学习
  • TA的每日心情
    开心
    2023-3-10 21:17
  • 签到天数: 85 天

    [LV.6]常住居民II

    发表于 2019-1-23 03:35:30 来自手机 | 显示全部楼层
    很厉害,就是看不懂
  • TA的每日心情
    开心
    2025-4-3 21:05
  • 签到天数: 1092 天

    [LV.10]以坛为家III

    发表于 2019-1-23 08:13:47 | 显示全部楼层
    厉害厉害,支持楼主!
  • TA的每日心情
    无聊
    2019-1-29 19:23
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2019-1-29 19:43:46 | 显示全部楼层
    厉害,根本看不懂
  • TA的每日心情
    开心
    2019-8-21 08:44
  • 签到天数: 163 天

    [LV.7]常住居民III

     楼主| 发表于 2019-2-6 16:58:31 | 显示全部楼层
    做完了,没有人工校对,免费下载 mdx。欢迎交流。

    该用户从未签到

    发表于 2019-2-27 08:12:11 | 显示全部楼层
    感谢楼主分享====
  • TA的每日心情
    擦汗
    2020-6-6 16:10
  • 签到天数: 164 天

    [LV.7]常住居民III

    发表于 2019-3-1 19:07:06 | 显示全部楼层
    楼主厉害啊 膜拜以下  
    您需要登录后才可以回帖 登录 | 免费注册

    本版积分规则

    小黑屋|手机版|Archiver|PDAWIKI |网站地图

    GMT+8, 2026-8-1 10:47 , Processed in 0.024805 second(s), 29 queries .

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表