Search code examples
pythonhindi

Invalid source language error while using googletranslate in python


I have a simple problem at hand.I'm loading from a text file containing(newline separated) Hindi sentences into a list with encoding specified as 'utf-8'.Next I try using the Translator from pygoogletranslation to translate from Hindi to English.I get an error of invalid source language.It works fine for source language of English though.Please point out what I have missed? (most probably in rendering the Hindi script in order)

from numpy import loadtxt 
content = loadtxt("path_to_input_file",delimiter="\n",dtype='str',unpack=False,encoding='utf-8')
list_=content.tolist()

pip install pygoogletranslation
from pygoogletranslation import Translator
translator = Translator()

eng_list=[]
for sent in list_:
  eng_sent=translator.translate(sent,src="hi",dest="en")
  eng_list.append(eng_sent.text)

ValueError: invalid source language

Solution

  • I used the following snippet to load the input text file,keeping everything else same and it worked !

    my_file = open("path to my file", "r")
    content_list = my_file.readlines()