'Get word definition/s from google translator using Python

enter image description hereplease, I am making my own dictionary and cant figure out how to pull translation definitions from google translate. My idea is that python will open my excel file and in every cell in column 1 is a new word. python will take every single one simultaneously. translate it from English to Slovak by using google translator and don't take just the translated word, but rather its definition/s (if there's more than one definition, take them all) and the group of the definition (noun, adverb, verb, ...) and then add these data in the excel table either in a new cell next to the original translated word or if more definitions, add rows for every definition.

I'm new to this so please excuse me.



Solution 1:[1]

To be able to satisfy your requirements. A way to do this is to do the following in your script:

  1. You can use pandas.read_excel to read your excel file and do some data manipulation to get all values in your column 1.
  2. When you got your values to translate you can use something like googletrans which uses Google Translate on the back end or use the paid Google Translation API to handle your translations. But based from your requirements, I suggest using the Google Translation API since it is capable of returning all possible definitions.
  3. When you get your translations, it is up to you to transform your data so you can add them as a new column on your original excel file. You can use pandas.ExcelWriter for this.

I made this simple script that reads a CSV file (I don't have excel installed in my machine), translates everything under text column and puts them to the translated column. It's up to you if you process the data differently.

NOTE for the script below:

  • I used the Google Translation API which is the paid service
  • Use pd.read_excel() to read excel files
  • Adjust the column number based from your input file

sample_data.csv:

text dummy_field
run dummy1
how are you dummy2
jump dummy3

Sample script:

import pandas as pd
from google.cloud import translate_v2 as translate

def translate_text(text):

    translate_client = translate.Client()
    target = 'tl'
    result = translate_client.translate(text, target_language = target)

    return result["translatedText"]

def process_data(input_file):

    #df = pd.read_excel('test.xlsx', engine='openpyxl')
    df = pd.read_csv(input_file)
    df['translated'] = df['text'].apply(translate_text)

    # move column 'translated' to second column
    # this position will depend on your actual data
    second_col = df.pop('translated')
    df.insert(1, 'translated', second_col)
    print(df)

    df.to_csv('./updated_data.csv',index=False)
    df.to_excel('./updated_data.xlsx',index=False)


process_data('sample_data.csv')

Output:

Dataframe

enter image description here

Generated csv file:

enter image description here

Generated excel file:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1