0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonで支離滅裂な文章を作成する

Posted at

Pythonで支離滅裂な文章を作成する

支離滅裂な文章が突然必要になった時に大活躍

環境
Bug sur 11.2.3
Python 3.8.3
googletrans 4.0.0rc1

コードのダウンロードはこちら

##やり方
googletransをimportして
おまじないを唱えて

translator = Translator()

これで準備完了

あとは下記のコードで翻訳できる

translator.translate("翻訳する文章", src="翻訳元の文字コード", dest="翻訳後の文字コード").text

例 日本語 > 英語

from googletrans import Translator
translator = Translator()
IN = translator.translate("こんにちは", src="ja", dest="en").text
print(IN)#Hello

あとはここにマイナー言語を追加して色々機能を追加して完成

import random
from googletrans import Translator
import sys


def ja(translator, Language):
    global IN
    global SRC
    if IN[1] == 'ja':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='ja').text]
    print(f'{Language[SRC[0]]} < 日本語', IN[0])
    SRC[0] = 'ja'
    IN.append(SRC[0])
    return IN


def en(translator, Language):
    global IN
    global SRC
    if IN[1] == 'en':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='en').text]
    print(f'{Language[SRC[0]]} < 英語', IN[0])
    SRC[0] = 'en'
    IN.append(SRC[0])
    return IN


def de(translator, Language):
    global IN
    global SRC
    if IN[1] == 'de':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='de').text]
    print(f'{Language[SRC[0]]} < ドイツ語', IN[0])
    SRC[0] = 'de'
    IN.append(SRC[0])
    return IN


def es(translator, Language):
    global IN
    global SRC
    if IN[1] == 'es':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='es').text]
    print(f'{Language[SRC[0]]} < スペイン語', IN[0])
    SRC[0] = 'es'
    IN.append(SRC[0])
    return IN


def fr(translator, Language):
    global IN
    global SRC
    if IN[1] == 'fr':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='fr').text]
    print(f'{Language[SRC[0]]} < フランス語', IN[0])
    SRC[0] = 'fr'
    IN.append(SRC[0])
    return IN


def ko(translator, Language):
    global IN
    global SRC
    if IN[1] == 'ko':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='ko').text]
    print(f'{Language[SRC[0]]} < 韓国語', IN[0])
    SRC[0] = 'ko'
    IN.append(SRC[0])
    return IN


def ru(translator, Language):
    global IN
    global SRC
    if IN[1] == 'ru':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='ru').text]
    print(f'{Language[SRC[0]]} < ロシア語', IN[0])
    SRC[0] = 'ru'
    IN.append(SRC[0])
    return IN


def uz(translator, Language):
    global IN
    global SRC
    if IN[1] == 'uz':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='uz').text]
    print(f'{Language[SRC[0]]} < ウズベク語', IN[0])
    SRC[0] = 'uz'
    IN.append(SRC[0])
    return IN


def el(translator, Language):
    global IN
    global SRC
    if IN[1] == 'el':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='el').text]
    print(f'{Language[SRC[0]]} < ギリシャ語', IN[0])
    SRC[0] = 'el'
    IN.append(SRC[0])
    return IN


def mt(translator, Language):
    global IN
    global SRC
    if IN[1] == 'mt':
        return IN
    IN = [translator.translate(IN[0], src=IN[1], dest='mt').text]
    print(f'{Language[SRC[0]]} < マルト語', IN[0])
    SRC[0] = 'mt'
    IN.append(SRC[0])
    return IN


def main():
    global IN
    global SRC
    #global translator
    translator = Translator()
    Lang = [ja, en, de, es, fr, ko, ru, uz, el, mt]
    Language = {
        "en": "英語",
        "de": "ドイツ語",
        "es": "スペイン語",
        "fr": "フランス語",
        "ko": "韓国語",
        "ru": "ロシア語",
        "uz": "ウズベク語",
        "el": "ギリシャ語",
        "ja": "日本語",
        "mt": "マルト語"
    }
    try:
        f = open(sys.argv[1], 'r', encoding='UTF-8')
        IN = [f.read()]
        f.close()
    except:
        IN = [input("文章を入力>>>")]
    L = translator.detect(IN[0]).lang
    print(f"入力言語を推論>>>{Language[L]}")
    try:
        SRC = [L, sys.argv[2]]
        IN.append(SRC[0])
    except:
        SRC = [L, L]
        IN.append(SRC[0])
    if not IN[0]:
        return -1
    random.shuffle(Lang)
    for i in Lang:
        IN = i(translator, Language)
    IN = translator.translate(IN[0], src=IN[1], dest=SRC[1]).text
    try:
        f = open(sys.argv[3], 'w', encoding='UTF-8')
        f.write(IN)
        f.close()
    except:
        print(IN)


if __name__ == '__main__':
    main()

##まとめ
無駄にランダム翻訳とか
インプリッタ,file両対応とか機能つけたけど
このコードが必要な時は多分無い

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?