Coder Social home page Coder Social logo

john1688 / translate-pdf-node Goto Github PK

View Code? Open in Web Editor NEW

This project forked from humoonruc/translate-pdf-node

0.0 0.0 0.0 4.36 MB

以 node 为主要工具,通过百度翻译 API,批量翻译外文 pdf 文件,并提取摘要、关键词、结论等关键信息,助力科研工作。

JavaScript 23.67% Python 76.17% Makefile 0.16%

translate-pdf-node's Introduction

[TOC]

外文 PDF 批量自动翻译项目——Node.js 版

使用方法

  1. 下载或克隆(安装 Git 并使用git clone命令)项目文件夹到本地。项目地址为:

    1. 国内:https://e.coding.net/humoonruc/node.js-demo/translate-pdf-node.git
    2. 海外:https://github.com/Humoonruc/translate-pdf-node.git
    3. 可复用 API:https://humoonruc.github.io/translate-pdf-node/

    git-clone

  2. 在百度翻译开放平台(https://api.fanyi.baidu.com/product/11)申请通用翻译API(每月免费额度为2000万字符),获得 APP ID和密钥

  3. 在项目文件夹的 /scripts/ 子文件夹中,新建 config.js 文件,写入以下内容,并填入刚刚申请好的百度翻译 APP ID 和密钥,以及根据需求调整源文件语言和目标语言的参数码

    // config.js
    
    module.exports.BAIDU_TRANSLATE_ID = 'APP ID';
    module.exports.BAIDU_TRANSLATE_KEY = '密钥';
    module.exports.BAIDU_TRANSLATE_API_URL = 'http://api.fanyi.baidu.com/api/trans/vip/translate';
    module.exports.SOURCE_LANGUAGE = 'en'; // 要翻译的 pdf 源文件语言
    module.exports.TARGET_LANGUAGE = 'zh'; // 要翻译的目标语言
    
    /* 语言参数码:
    自动检测	auto
    英语	en
    简体中文	zh
    繁体中文	cht
    文言文	wyw
    日语	jp
    韩语	kor
    法语	fra
    西班牙语	spa
    阿拉伯语	ara
    俄语	ru
    葡萄牙语	pt
    德语	de
    意大利语	it
    希腊语	el
    荷兰语	nl
    波兰语	pl
    泰语	th
    越南语	vie
    */
  4. 在计算机中安装 Node.js(下载地址:Node.js (nodejs.org)) 和 Python(下载地址:Download Python | Python.org )的最新版本

  5. 安装所需的 node 第三方模块:打开终端(如 Windows 上的命令提示符),切换到项目文件夹根目录,输入

    npm install

    npm-install

  6. 安装所需的 python 第三方库:继续在终端输入

    pip install pdfminer.six

    image-20210524185248516

  7. 将想要翻译的外文 pdf 文件放入项目文件夹的 /pdf/ 子文件夹

  8. 在终端中切换到项目文件夹根目录,输入:

    node index.js

    node-index

    然后等待程序运行完毕即可。翻译完成的 md 文件位于 /md/translate-result/子文件夹中,这是一种文本文件,用记事本即可打开。

    translated

程序流程

  1. 使用 Node 模块child_process调用系统终端开启子进程,如此可以广泛地调用各种语言编写的脚本文件。

  2. 修改 pdf 文件名。学术论文的文件名中常有许多空格,会给程序运行带来极大隐患,故首先将所有的空格用连字符-替换。

  3. 使用 Python 第三方库pdfminer.six,将 pdf 文件解析为 xml 文件,尽最大可能恢复文档的结构。在解析 pdf 领域,pdfminer 似乎是目前最好的第三方库,我在 npm 上找了很久,都没有找到性能与之接近的 Node 模块。

  4. 使用 Node 模块x2js,将 xml 文件转换为易操作的 json 文件。

  5. 从 json 中提取结构化的字符信息,重新整合为行、段、页、篇,并保存为 txt 纯文本文件。

  6. 运用正则表达式解析文本,从 txt 文件中提取文章主体(不要参考文献),以及 abstract、keywords、Introduction、conclusion 等重要信息,保存为 md 文件。

  7. 调用云计算商提供的翻译 api,翻译英文文本,将翻译结果保存为 md 文件。这一步有两种实现方式:

    1. 可以用同步 API,以串行方式依次翻译每个文件。
    2. 也可以用异步 API,开启事实上的并行进程,同时翻译多个文件以提高效率。并行翻译的效率虽高,但访问服务器的频率过高,有可能被服务器拒绝响应,甚至被封杀 IP.
    3. 两种代码都写在根目录源文件 index.js 中,用户可以根据需要使用一种,而注释掉一种。
    image-20210526144207792

本项目中,原始 pdf 文档的格式经历了如下变化:pdf->xml->json->txt->md,最大限度地保留了 pdf 文本的结构信息。

如此批量翻译外文 pdf 文件,可以极大地减轻泛读文献、报告的压力。

优化方向

本项目的初版借鉴过 GitHub 上的一个自动生成综述的开源项目。那个作者的代码层级过深,再加上 Python 强制要求4格缩进,后面就出现了缩进地狱,可读性非常差:

缩进地狱2

因此本项目的新版将编程语言从 Python 切回了用大括号和分号间隔代码块的 JavaScript,在代码的组织上也充分利用了模块系统,写起来顺手了很多,代码的逻辑性和可读性都大大增强了。

image-20210525091818415

但这个小工具使用起来仍然不是太方便,安装流程也不够傻瓜化。以后假如有时间,考虑写成网页版,用户输入自己的账号密码,把 PDF 往网页里一扔,过一会儿就能返回可供下载的结果,这是最好的。

但这貌似是砸某些人的饭碗呀!

各大公司的 PDF 全文翻译服务卖到现在这种高价,简直无良。希望这个价格以后能降下来,我又何必做这种小工具呢。

translate-pdf-node's People

Contributors

humoonruc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.