LoginSignup
2
4

Too Long; Don'wanna Read

これ使ってください:
https://github.com/konbraphat51/MassiveTranslator

はじめに

NLPの者なら100度は夢見る大量ドキュメント翻訳、したいですよね。無料Google translation APIから拒絶されるぐらいの。

しかし、払う金もないし、ローカル環境も汚したくないし、そもそもスペックがない。

活路はGoogle Colaboratoryにあります。

作戦

単純に、Google様から無料で授かりしGPU環境を、argos-translateをベースに翻訳機に改造させ、翻訳させます。

Google Colaboratoryとは?

Google社が提供している、Pythonプログラミングがネットで簡単にできるサイトです、簡単に言うと。

Colaboratory(略称: Colab)は、Google Research が提供するサービスです。Colab では、誰でもブラウザ上で Python を記述、実行できるため、機械学習、データ分析、教育に特に適しています。具体的には、GPU などのコンピューティング リソースに料金なしでアクセスしながら、特別な設定なしでご利用いただけるホスト型の Jupyter Notebook サービスです。

現状、どこの大学も、初心者学生にプログラミングを教えるときに利用しているかと思います。
最近の高校の情報1も。

Argos Translateとは?

Argos Open Tech社が提供している、ローカルで機械翻訳ができるPythonライブラリです。

2023/12/9現在の対応言語は

アラビア語、アゼルバイジャン語、カタロニア語、中国語、チェコ語、デンマーク語、オランダ語、英語、エスペラント語、フィンランド語、フランス語、ドイツ語、ギリシャ語、ヘブライ語、ヒンディー語、ハンガリー語、インドネシア語、アイルランド語、イタリア語、日本語、韓国語、ペルシア語、ポーランド語、ポルトガル語、ロシア語、 スロバキア語、スペイン語、スウェーデン語、トルコ語、ウクライナ語

だそうです。(https://pypi.org/project/argostranslate/ より翻訳引用)

実装

実装しました
https://github.com/konbraphat51/MassiveTranslator

特に特殊な技術はなかったので特筆するまでもないですが、

GPUを使わせるように命令し

import os
os.environ["ARGOS_DEVICE_TYPE"]="cuda"

翻訳先の言語を指定できるようにし

lang_to = "en" #@param {type:"string"}

#@paramでなんか良い感じのUIをつけることができます
image.png
[ColabにUIをつける小技集](https://qiita.com/john-rocky/items/e5802cdd15dc2e34cb84

翻訳のargostranslateと言語検出のlangdetectライブラリをインストール

!pip install argostranslate
!pip install langdetect

何も考えず全言語分のモデルをダウンロード

import argostranslate.package
from tqdm import tqdm

argostranslate.package.update_package_index()
available_packages = argostranslate.package.get_available_packages()

for package in tqdm(list(available_packages)):
	argostranslate.package.install_from_path(package.download())

content フォルダにある全ての.txtファイルを取得

files = glob("/content/*.txt")

contents = []

for file in files:
  with open(file, "r", encoding="utf-8") as f:
    content = f.read()

  contents.append((file, content))

翻訳

translated = []

for content in tqdm(contents):
    t = translate(content[1], from_code = detect(content[1]), to_code = lang_to)

    translated.append((content[0], t))

翻訳済みファイルをcontent/translatedフォルダに入れ、zip化してダウンロード。

for t in translated:
  file_name = t[0].split("/")[-1]
  with open("/content/translated/"+file_name, "w", encoding="utf-8") as f:
    f.write(t[1])

!zip -r /content/download.zip /content/translated

from google.colab import files
files.download("/content/download.zip")

使い方

  • このnotebookをダウンロードし、Google Colabで開きます。

  • デフォルトで開かれているcontentフォルダーに翻訳したい.txtファイルを全て入れます

  • lang_toに翻訳先の言語コードを記入
    image.png

  • 全てのセルを実行
    image.png

  • download.zipがダウンロードされるまで待つ

だけです。

2
4
0

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
  3. You can use dark theme
What you can do with signing up
2
4