在python中使用谷歌音译api

Beg*_*ner 5 nlp google-api python-3.x machine-translation

我正在尝试使用 google 音译[1] 将用英语书写的印地语单词转换为印地语。例如-
输入文本- Main sahi hun。
必填文本 - ??? ??????

我想将输入字符串传递给 api 并需要印地语的必需文本。我正在使用 google 音译,但因为它很久以前就被弃用了,所以找不到合适的方法在 python 上执行它,因为目前他们提供的示例是在 javascript 中,对初学者不太友好。怎么做?

seg*_*404 2

这是一个简单的 python 代码,可以满足您的需求:

\n
import http.client\nimport json\n\ndef request(input):\n    conn = http.client.HTTPSConnection('inputtools.google.com')\n    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')\n    res = conn.getresponse()\n    return res\n\ndef driver(input):\n    output = ''\n    if ' ' in input:\n        input = input.split(' ')\n        for i in input:\n            res = request(input = i)\n            #print(res.read() )\n            res = res.read()\n            if i==0:\n                output = str(res, encoding = 'utf-8')[14+4+len(i):-31]\n            else:\n                output = output + ' ' + str(res, encoding = 'utf-8')[14+4+len(i):-31]\n    else:\n        res = request(input = input)\n        res = res.read()\n        output = str(res, encoding = 'utf-8')[14+4+len(input):-31]\n    print(output)\n\ndriver('mein sahi hun')\n
Run Code Online (Sandbox Code Playgroud)\n

这将给出输出:

\n
\n

\xe0\xa4\xae\xe0\xa5\x87\xe0\xa4\x82 \xe0\xa4\xb8\xe0\xa4\xb9\xe0\xa5\x80 \xe0\xa4\xb9\xe0\xa5\x82\xe0 \xa4\x81

\n
\n