Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the google transliterate api in python

I am trying to use google transliterate [1] to convert hindi words written in english to hindi. e.g-
Input text- Main sahi hun.
Required text -मैं सही हूँ

I want to pass the input string to api and wants a required text in hindi language. I am using google transliterate but as it was deprecated long time ago so can't find a suitable way to do it on python as currently the example they are providing is in javascript and not very beginner friendly. How to do it?

like image 533
Beginner Avatar asked Nov 18 '22 00:11

Beginner


1 Answers

This is a simple python code to address what you are looking for:

import http.client
import json

def request(input):
    conn = http.client.HTTPSConnection('inputtools.google.com')
    conn.request('GET', '/request?text=' + input + '&itc=' + 'hi-t-i0-und' + '&num=1&cp=0&cs=1&ie=utf-8&oe=utf-8&app=test')
    res = conn.getresponse()
    return res

def driver(input):
    output = ''
    if ' ' in input:
        input = input.split(' ')
        for i in input:
            res = request(input = i)
            #print(res.read() )
            res = res.read()
            if i==0:
                output = str(res, encoding = 'utf-8')[14+4+len(i):-31]
            else:
                output = output + ' ' + str(res, encoding = 'utf-8')[14+4+len(i):-31]
    else:
        res = request(input = input)
        res = res.read()
        output = str(res, encoding = 'utf-8')[14+4+len(input):-31]
    print(output)

driver('mein sahi hun')

This will give the output as :

में सही हूँ

like image 179
segfault404 Avatar answered Dec 31 '22 19:12

segfault404