簡體   English   中英

在 python 中使用谷歌音譯 api

[英]Use the google transliterate api in python

我正在嘗試使用 google transliterate [1]將用英語編寫的印地語單詞轉換為印地語。 例如-
輸入文本 - Main sahi hun。
必填文本 -मैं सही हूँ

我想將輸入字符串傳遞給 api 並想要印地語所需的文本。 我正在使用谷歌音譯,但由於它很久以前就被棄用了,所以在 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')

這將使輸出為:

मेंसहीहूँ

這是在腳本中使用的好代碼。 對於文本音譯的反面需要改變什么,比如通過印地語文本並獲得羅馬英語。 其次,使用谷歌輸入工具進行音譯的可靠方法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM