python - 使用 Python 将印地语翻译成英语

标签 python google-translate google-api-python-client microsoft-translator

我正在做一个识别语言(英语、印地语、马拉地语等)的项目,具体取决于源语言代码,并将其翻译成另一种语言取决于输入的目标语言代码。

一切都是用Python语言完成的。

Google API 可识别语言并解释为文本格式,然后使用 Microsoft API 将其翻译为另一种语言。

但是我遇到了一个错误,就是这里

Traceback(most recent call last):
  File "pitranslate.py", line 60, in < module >
  translation_result = requests.get(translation_url + urllib.urlencode(translation_args), headers = headers)
File "/usr/lib/python2.7/urllib.py", line 1332, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can 't encode characters in position 0-3: ordinal not in range(128)

我的输入:क्या कर रहे हो

完整代码如下:

import json
import requests
import urllib
import subprocess
import argparse
import speech_recognition as sr
from subprocess import call

parser = argparse.ArgumentParser(description='This is a demo script by DaveConroy.com.')
parser.add_argument('-o','--origin_language', help='Origin Language',required=True)
parser.add_argument('-d','--destination_language', help='Destination Language', required=True)
#parser.add_argument('-t','--text_to_translate', help='Text to Translate', required=True)
args = parser.parse_args()

## show values ##
print ("Origin: %s" % args.origin_language )
print ("Destination: %s" % args.destination_language )
#print ("Text: %s" % args.text_to_translate )

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")

    audio = r.listen(source)
args.text_to_translate = r.recognize_google(audio, language=args.origin_language) 
text = args.text_to_translate
#text=r.recognize_google(audio)
print text
origin_language=args.origin_language
destination_language=args.destination_language


def speakOriginText(phrase):
    googleSpeechURL = "http://translate.google.com/translate_tts?tl="+ origin_language +"&q=" + phrase
    subprocess.call(["mplayer",googleSpeechURL], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def speakDestinationText(phrase):
    googleSpeechURL = "http://translate.google.com/translate_tts?tl=" + destination_language +"&q=" + phrase
    print googleSpeechURL
    subprocess.call(["mplayer",googleSpeechURL], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

args = {
        'client_id': 'create and enter your client id',
        'client_secret': 'create id and enter here',#your azure secret here
        'scope': 'http://api.microsofttranslator.com',
        'grant_type': 'client_credentials'
    }

oauth_url = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
oauth_junk = json.loads(requests.post(oauth_url,data=urllib.urlencode(args)).content)
translation_args = {
        'text': text,
        'to': destination_language,
        'from': origin_language
        }

headers={'Authorization': 'Bearer '+oauth_junk['access_token']}
translation_url = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?'
translation_result = requests.get(translation_url+urllib.urlencode(translation_args),headers=headers)
translation=translation_result.text[2:-1]

speakOriginText('Translating ' + translation_args["text"])
speakDestinationText(translation)

如何克服这个错误?

最佳答案

对于此错误,您必须像示例一样以 Utf-8 解码文本 您有其他语言的文字 My_input=क्या कररहेहो 现在要使用此文本进行转换或翻译,您必须使用解码

My_input=क्या कर रहे हो
My_input.decode("utf-8")

像这样你可以解码和编码字符串

关于python - 使用 Python 将印地语翻译成英语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776958/

相关文章:

javascript - Google 翻译文本转语音操作已取消

python - 尝试使用 python 访问 "google drive"时出错(google quickstart.py 源代码)

python - 我可以在 GAE 上使用 django.contrib.gis 吗?

python - 如何使用 Python 访问 Firefox 的内部 indexedDB 文件?

python - 如何使 pandas/core/generic/pct_change 从负值更改为正值时返回正值

如果列表为空,Python 会阻塞线程

android - Google Translate API 有时需要很长时间来初始化

html - 如何在 Chrome 中禁用谷歌从 HTML 翻译

python - Google Drive API 权限,如何禁用互联网上任何人都可以找到的功能?

python - 访问 token 刷新错误: invalid_grant from oauth2client