LoginSignup
8

More than 5 years have passed since last update.

posted at

PDFからコピーした英文をGoogle翻訳する

はじめに

英語論文のPDFから英文をコピーしてGoogle翻訳に投げるときは、あちこちに入った改行を消さないとうまく翻訳してもらえない。
手作業でやるのに飽きたのでPythonの関数を用意しました。
自分用です。

準備

googletransを使うのでインストールしておきます。

$ pip install googletrans

プログラム

import re
from googletrans import Translator


def translate(text):
    comment_pattern = re.compile(r'//([^\n]*)')
    out = re.sub(comment_pattern, r'\1', text)

    out = re.sub(r'^\s*|\s*(?=\s)|\s*$', '', out)
    out = re.sub(r'[\r\n]', ' ', out)
    out = re.sub(r':\s+', r':\n', out)
    out = re.sub(r'\.\s+([A-Z])', r'.\n\1', out)

    out = ' '.join(
        '\n' + line if ' ' in line else line
        for line in out.splitlines())
    out = '\n'.join(
        re.sub(r' ([A-Z]\w+)', r' _\1_', line)
        for line in out.splitlines())

    for t in Translator().translate(out.strip().splitlines(), src='en', dest='ja'):
        print(t.origin)
        print()
        print(t.text)
        print()

実行例

translate("""
The quick brown fox jumps over the 
lazy dog. The quick brown fox jumps 
over the lazy dog. The quick brown 
fox jumps over the lazy dog.
""")
The quick brown fox jumps over the lazy dog. 

クイックブラウンキツネは怠惰な犬の上を飛びます。

The quick brown fox jumps over the lazy dog. 

クイックブラウンキツネは怠惰な犬の上を飛びます。

The quick brown fox jumps over the lazy dog.

クイックブラウンキツネは怠惰な犬の上を飛びます。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
8