python-3.x - 在python中使用谷歌音译api

标签 python-3.x google-api nlp machine-translation

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

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

最佳答案

这是一个简单的 python 代码来解决您正在寻找的问题:

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')
这将使输出为:

में सही हूँ

关于python-3.x - 在python中使用谷歌音译api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52834152/

相关文章:

python - 有没有办法在 GPU 上使用 tensorflow map_fn?

c# - 寻找一种方法来优化此算法以解析非常大的字符串

python - 如何从 sklearn 中的 TF*IDF 值获取字数

Python - 如何将 Panda 中的元素从列表转换为字符串

python-3.x - matplotlib 错误 : No module named matplotlib even though it is installed

python - 使用 google.appengine.api.mail.send_mail 发送电子邮件时如何显示发件人姓名而不是电子邮件地址

api - 谷歌分析通知API

ios - 添加、更新有效,但如何使用 iOS Google Sheets API v4 进行追加?

nlp - 使用 "natural"语言编写代码是否更好?

Python Authlog 日志文件分析