Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

はじめに

英語論文の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.

クイックブラウンキツネは怠惰な犬の上を飛びます。
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away