我正在尝试寻找与Google翻译服务互动的方式。 我希望能够通过源语言中的字符串向服务传递一个列表,并返回包含目标语言中的值字符串的列表。
使用Python工作,我在这里找到了一些相关的代码: https : //github.com/google/google-api-python-client/blob/master/samples/translate/main.py
我尝试了代码,它的工作原理。 见下文:
from __future__ import print_function __author__ = 'jcgregorio@google.com (Joe Gregorio)' from googleapiclient.discovery import build def main(): # Build a service object for interacting with the API. Visit # the Google APIs Console <http://code.google.com/apis/console> # to get an API key for your own application. service = build('translate', 'v2', developerKey='AIzaSyCT4Ds5GuytOZnxGav492RAUvN508daobAVo') print(service.translations().list( source='el', target='en', q=['Είμαι πολύ ευτυχής που σας βλέπω σήμερα εδώ', 'Η κόρη μου θα χαρεί πολύ να σας δει'] ).execute()) if __name__ == '__main__': main()当运行这段代码时,输出如下内容:
{'translations': [{'translatedText': 'I am very happy to see you here today'}, {'translatedText': 'My daughter will be very pleased to see you'}]}但正如我所说,上面的代码将返回的结果打印到屏幕上。 我想要的是将翻译后的文本存储到一个对象中,最终我将它变成一个熊猫对象。
所以我试图用这种方式来修正它 - 见下文:
from __future__ import print_function __author__ = 'jcgregorio@google.com (Joe Gregorio)' from googleapiclient.discovery import build def main(): # Build a service object for interacting with the API. Visit # the Google APIs Console <http://code.google.com/apis/console> # to get an API key for your own application. service = build('translate', 'v2', developerKey='AIzaSyCT4Ds5GuytOZnxGav492RAUvN508daobAVo') result = service.translations().list( source='el', target='en', q=['Είμαι πολύ ευτυχής που σας βλέπω σήμερα εδώ', 'Η κόρη μου θα χαρεί πολύ να σας δει'] ).execute() return result if __name__ == '__main__': main()但是当我试图显示对象“结果”时,我得到了以下异常:
NameError: name 'result' is not defined你能帮我修改代码,以便它符合我的目的吗? 您的建议将不胜感激。
如果您有兴趣,可以免费试用Google云服务12个月,但据我所知,文档非常差。
I am trying to find a way to interact with the Google translation service. I would like to be able to pass to the service a list with strings in a source language and get back a list with values strings in the target language.
Working from Python, I have found some relevant code here: https://github.com/google/google-api-python-client/blob/master/samples/translate/main.py
I tried the code and it works. See below:
from __future__ import print_function __author__ = 'jcgregorio@google.com (Joe Gregorio)' from googleapiclient.discovery import build def main(): # Build a service object for interacting with the API. Visit # the Google APIs Console <http://code.google.com/apis/console> # to get an API key for your own application. service = build('translate', 'v2', developerKey='AIzaSyCT4Ds5GuytOZnxGav492RAUvN508daobAVo') print(service.translations().list( source='el', target='en', q=['Είμαι πολύ ευτυχής που σας βλέπω σήμερα εδώ', 'Η κόρη μου θα χαρεί πολύ να σας δει'] ).execute()) if __name__ == '__main__': main()When run this code printed the following output:
{'translations': [{'translatedText': 'I am very happy to see you here today'}, {'translatedText': 'My daughter will be very pleased to see you'}]}However as I said, the code above prints the returned results to the screen. What I would like instead would be to store the translated text into an object that eventually I would make it a pandas object.
So I tried to ammend it in this way - see below:
from __future__ import print_function __author__ = 'jcgregorio@google.com (Joe Gregorio)' from googleapiclient.discovery import build def main(): # Build a service object for interacting with the API. Visit # the Google APIs Console <http://code.google.com/apis/console> # to get an API key for your own application. service = build('translate', 'v2', developerKey='AIzaSyCT4Ds5GuytOZnxGav492RAUvN508daobAVo') result = service.translations().list( source='el', target='en', q=['Είμαι πολύ ευτυχής που σας βλέπω σήμερα εδώ', 'Η κόρη μου θα χαρεί πολύ να σας δει'] ).execute() return result if __name__ == '__main__': main()However when I tried to display of the object "result" I got the following exception:
NameError: name 'result' is not definedCould you help me ammend the code so that it meets my ends? Your advice will be appreciated.
In case you are interested you can try the Google cloud service for free for a period of 12 months, but the documentation as far as I can tell is very poor.
最满意答案
你如何试图“显示结果”? 它看起来像你的代码成功创建的结果,但然后main()返回它...到哪里? 你如何调用main并使用它返回的内容?
您不能在定义它们的函数之外访问本地函数变量(例如结果)。
可能的是,你想要的是这个代码是一个函数,它将结果返回给另一个函数/程序,然后使用返回的结果。 例如
def your_main_program(): result = the_function_you_currently_have() #currently called main() # do something with the resultHow are you trying to "display result"? It looks like your code successfully creates result, but then main() returns it... to where? How are you calling main and using what it returns?
You can't access local function variables (e.g. result) outside the function that defines them.
Likely, what you want is for this code to be a function that returns the result to another function/program, that then uses the result returned. E.g.
def your_main_program(): result = the_function_you_currently_have() #currently called main() # do something with the result更多推荐
发布评论