Test

Last updated: July 7th, 20202020-07-07Project preview
In [6]:
import pandas
import os
from yandex_translate import YandexTranslate, YandexTranslateException

def get_translated_column(
    yandex_token: str,
    table: pandas.DataFrame,
    column_name: str,
    lang_to_from: str = 'ru-en',
):
    translator = YandexTranslate(yandex_token)
    return table[column_name].apply(lambda x: translator.translate(x, lang=lang_to_from)['text'][0])


def get_supported_langs(yandex_token: str):
    import requests
    import json

    url = f'https://translate.yandex.net/api/v1.5/tr.json/getLangs?ui=ru&key={yandex_token}'
    return json.loads(requests.get(url).text)


def translate_column(
    yandex_token_list: list,
    file_path: str,
    original_column_name: str,
    translated_column_name: str,
    save_to_file_path: str = None,
    lang_to_from: str = 'ru-en',
    start_row: int = 0,
    finish_row: int = None,
    batch: int = 10,
):
    # получить таблицу из файла
    ext = os.path.splitext(file_path)[1]
    if ext == ".csv":
        table = pandas.read_csv(file_path, header=None)
    elif ext in [".xlsx", ".xls"]:
        table = pandas.read_excel(file_path)
    else:
        raise ValueError(f"Неподдерживаемый формат: {ext}")
    
    if not translated_column_name in table.columns:
        table[translated_column_name] = pandas.Series([], dtype=str)
    if not finish_row:
        finish_row = len(table)
    
    yandex_token_pos = 0

    for i in range(((finish_row - start_row) // batch) + 1):
        l = start_row + i * batch
        r = start_row + (i + 1) * batch
        print(f"Перевод строк {l}-{r}.")
        try:
            translated_column = get_translated_column(
                yandex_token=yandex_token_list[yandex_token_pos],
                table=table[l:r],
                column_name=original_column_name,
                lang_to_from=lang_to_from,
            )
        except YandexTranslateException:
            yandex_token_pos += 1
            if yandex_token_pos >= len(yandex_token_list):
                raise YandexTranslateException(
                    "Лимиты на перевод достигнуты для всех переданных токенов."
                )
            translated_column = get_translated_column(
                yandex_token=yandex_token_list[yandex_token_pos],
                table=table[l:r],
                column_name=original_column_name,
                lang_to_from=lang_to_from,
            )
                
        table[translated_column_name][l:r] = translated_column
        print(f"Перевод строк {l}-{r} сохранен.")
        
        # сохранить таблицу с переведенным столбцом
        if not save_to_file_path:
            save_to_file_path = file_path
        table.to_csv(save_to_file_path, index=False)
    
    return table
In [14]:
# токен можно получить здесь: https://translate.yandex.ru/developers/keys
yandex_token_list = [
    'trnsl.1.1.20200331T110242Z.9b513b0ed4f09237.33aec5c62db6c59d166b2d087085a48984c5e773',  # ivan
    'trnsl.1.1.20190906T170533Z.2cdef571e4680794.3161998ff0a03702bffae1c9b1c6f5cfbd84a682',  # kamil@zonesmart.ru
    'trnsl.1.1.20200110T122307Z.0eab94a550695b16.851da33dae66e111a419c298ea04bbc64e9889cd',  # dev@zonesmart.ru
    'trnsl.1.1.20200331T111658Z.debed69e977278e3.dc2212dc94324502e515153f8f706e70bfe9f323',  # info@zonesmart.ru
]


# перевод названий товаров c русского на английский
translate_column(
    yandex_token_list=yandex_token_list,
    file_path='translate.csv',
    save_to_file_path='translate.csv',
    original_column_name=0,
    translated_column_name='1',
    lang_to_from='en-ja',
    start_row=0,
    finish_row=None,
)
Перевод строк 0-10.
Перевод строк 0-10 сохранен.
Перевод строк 10-20.
Перевод строк 10-20 сохранен.
Перевод строк 20-30.
Перевод строк 20-30 сохранен.
Перевод строк 30-40.
Перевод строк 30-40 сохранен.
Перевод строк 40-50.
Перевод строк 40-50 сохранен.
Out[14]:
0 1
0 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
1 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
2 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
3 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
4 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
5 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
6 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
7 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
8 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
9 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
10 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
11 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
12 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
13 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
14 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
15 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
16 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
17 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
18 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
19 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
20 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
21 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
22 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
23 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
24 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
25 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
26 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
27 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
28 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
29 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
30 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
31 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
32 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
33 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
34 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
35 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
36 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
37 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
38 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
39 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
40 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
41 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
42 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
43 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
44 <p><strong>Dear Customers!</strong><br>It’s ho... <p><strong>お客様!</strong><br>も光栄でお迎えいてアマゾンでもokで...
In [ ]:
 
In [10]:
resp = get_supported_langs(yandex_token='trnsl.1.1.20200331T110242Z.9b513b0ed4f09237.33aec5c62db6c59d166b2d087085a48984c5e773')
In [13]:
resp.text
Out[13]:
'{"dirs":["az-ru","be-bg","be-cs","be-de","be-en","be-es","be-fr","be-it","be-pl","be-ro","be-ru","be-sr","be-tr","bg-be","bg-ru","bg-uk","ca-en","ca-ru","cs-be","cs-en","cs-ru","cs-uk","da-en","da-ru","de-be","de-en","de-es","de-fr","de-it","de-ru","de-tr","de-uk","el-en","el-ru","en-be","en-ca","en-cs","en-da","en-de","en-el","en-es","en-et","en-fi","en-fr","en-hu","en-it","en-lt","en-lv","en-mk","en-nl","en-no","en-pt","en-ru","en-sk","en-sl","en-sq","en-sv","en-tr","en-uk","es-be","es-de","es-en","es-ru","es-uk","et-en","et-ru","fi-en","fi-ru","fr-be","fr-de","fr-en","fr-ru","fr-uk","hr-ru","hu-en","hu-ru","hy-ru","it-be","it-de","it-en","it-ru","it-uk","lt-en","lt-ru","lv-en","lv-ru","mk-en","mk-ru","nl-en","nl-ru","no-en","no-ru","pl-be","pl-ru","pl-uk","pt-en","pt-ru","ro-be","ro-ru","ro-uk","ru-az","ru-be","ru-bg","ru-ca","ru-cs","ru-da","ru-de","ru-el","ru-en","ru-es","ru-et","ru-fi","ru-fr","ru-hr","ru-hu","ru-hy","ru-it","ru-lt","ru-lv","ru-mk","ru-nl","ru-no","ru-pl","ru-pt","ru-ro","ru-sk","ru-sl","ru-sq","ru-sr","ru-sv","ru-tr","ru-uk","sk-en","sk-ru","sl-en","sl-ru","sq-en","sq-ru","sr-be","sr-ru","sr-uk","sv-en","sv-ru","tr-be","tr-de","tr-en","tr-ru","tr-uk","uk-bg","uk-cs","uk-de","uk-en","uk-es","uk-fr","uk-it","uk-pl","uk-ro","uk-ru","uk-sr","uk-tr"],"langs":{"af":"Африкаанс","am":"Амхарский","ar":"Арабский","az":"Азербайджанский","ba":"Башкирский","be":"Белорусский","bg":"Болгарский","bn":"Бенгальский","bs":"Боснийский","ca":"Каталанский","ceb":"Себуанский","cs":"Чешский","cv":"Чувашский","cy":"Валлийский","da":"Датский","de":"Немецкий","el":"Греческий","en":"Английский","eo":"Эсперанто","es":"Испанский","et":"Эстонский","eu":"Баскский","fa":"Персидский","fi":"Финский","fr":"Французский","ga":"Ирландский","gd":"Шотландский (гэльский)","gl":"Галисийский","gu":"Гуджарати","he":"Иврит","hi":"Хинди","hr":"Хорватский","ht":"Гаитянский","hu":"Венгерский","hy":"Армянский","id":"Индонезийский","is":"Исландский","it":"Итальянский","ja":"Японский","jv":"Яванский","ka":"Грузинский","kk":"Казахский","km":"Кхмерский","kn":"Каннада","ko":"Корейский","ky":"Киргизский","la":"Латынь","lb":"Люксембургский","lo":"Лаосский","lt":"Литовский","lv":"Латышский","mg":"Малагасийский","mhr":"Марийский","mi":"Маори","mk":"Македонский","ml":"Малаялам","mn":"Монгольский","mr":"Маратхи","mrj":"Горномарийский","ms":"Малайский","mt":"Мальтийский","my":"Бирманский","ne":"Непальский","nl":"Нидерландский","no":"Норвежский","pa":"Панджаби","pap":"Папьяменто","pl":"Польский","pt":"Португальский","ro":"Румынский","ru":"Русский","sah":"Якутский","si":"Сингальский","sk":"Словацкий","sl":"Словенский","sq":"Албанский","sr":"Сербский","su":"Сунданский","sv":"Шведский","sw":"Суахили","ta":"Тамильский","te":"Телугу","tg":"Таджикский","th":"Тайский","tl":"Тагальский","tr":"Турецкий","tt":"Татарский","udm":"Удмуртский","uk":"Украинский","ur":"Урду","uz":"Узбекский","vi":"Вьетнамский","xh":"Коса","yi":"Идиш","zh":"Китайский"}}'
In [ ]:
 
Notebooks AI
Notebooks AI Profile20060