【问题标题】:Translating a part of JSON file to hindi将部分 JSON 文件翻译成印地语
【发布时间】:2019-06-06 19:33:08
【问题描述】:

我在this link 中有一个名为 region_descriptions.json 的 JSON 文件。 此文件在我的 Windows 中的记事本 ++ 中未正确加载(因为它是一个巨大的文件)。该文件部分加载到谷歌浏览器中。这个文件是我的密集字幕任务的数据集,我需要编写一个 python 脚本来将其中的每个“短语”翻译成印地语。

我在 power shell 中导航到我的 json 文件所在的目录,然后使用以下命令设置环境变量: >>$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\Preeti\Downloads\Compressed\region_descriptions.json"

之后我尝试在同一目录中打开 jupyter notebook 并运行代码:

import ijson
from google.cloud import translate

translate_client = translate.Client()

parser = ijson.items(open("region_descriptions.json"), "item.regions.item")

maxTranslations = 100;
for region in parser:
    translation = translate_client.translate(region["phrase"], target_language="hi")

    print(region["phrase"])
    print(translation['translatedText'])

    maxTranslations-=1
    if maxTranslations==0:
        break

但是 jupyter notebook 给了我一个错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-1-5fa13c6f3710> in <module>
      2 from google.cloud import translate
      3 
----> 4 translate_client = translate.Client()
      5 
      6 parser = ijson.items(open("region_descriptions.json"), "item.regions.item")

c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\cloud\translate_v2\client.py in __init__(self, target_language, credentials, _http, client_info)
     75     ):
     76         self.target_language = target_language
---> 77         super(Client, self).__init__(credentials=credentials, _http=_http)
     78         self._connection = Connection(self, client_info=client_info)
     79 

c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\cloud\client.py in __init__(self, credentials, _http)
    128             raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
    129         if credentials is None and _http is None:
--> 130             credentials, _ = google.auth.default()
    131         self._credentials = google.auth.credentials.with_scopes_if_required(
    132             credentials, self.SCOPE

c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in default(scopes, request)
    303 
    304     for checker in checkers:
--> 305         credentials, project_id = checker()
    306         if credentials is not None:
    307             credentials = with_scopes_if_required(credentials, scopes)

c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in _get_explicit_environ_credentials()
    163     if explicit_file is not None:
    164         credentials, project_id = _load_credentials_from_file(
--> 165             os.environ[environment_vars.CREDENTIALS])
    166 
    167         return credentials, project_id

c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in _load_credentials_from_file(filename)
    100     # The type key should indicate that the file is either a service account
    101     # credentials file or an authorized user credentials file.
--> 102     credential_type = info.get('type')
    103 
    104     if credential_type == _AUTHORIZED_USER_TYPE:

AttributeError: 'list' object has no attribute 'get'

有人可以帮我编写一个 python 脚本来将 json 文件中的所有短语翻译成印地语或帮助我克服我的错误吗?我强烈建议从给出的链接下载 json 文件,以便更好地理解我所指的“短语”。

【问题讨论】:

    标签: python json google-translate


    【解决方案1】:

    由于你的文件很大,你应该使用ijson

    以下代码对我有用:

    import ijson
    from google.cloud import translate
    
    translate_client = translate.Client()
    
    parser = ijson.items(open("region_descriptions.json"), "item.regions.item")
    
    maxTranslations = 100;
    for region in parser:
        translation = translate_client.translate(region["phrase"], target_language="hi")
    
        print(region["phrase"])
        print(translation['translatedText'])
    
        maxTranslations-=1
        if maxTranslations==0:
            break
    

    您应该考虑以下几点,如果以上对您不起作用:

    1. 别忘了setup GOOGLE_APPLICATION_CREDENTIALS环境变量。
    2. 一旦一切正常,从for 循环中删除break
    3. 如果您无法理解 ijson 的工作原理,您会发现 this tutorial 很有帮助。

    【讨论】:

    • 非常感谢您写得很好的回答。我尝试运行此代码,但我不断收到内存错误(这可能表明代码是正确的)。我试着用谷歌搜索这个错误,发现可能我的笔记本电脑不够好,无法运行此代码并根据其硬件和软件规格执行任务。有一台高级笔记本电脑的人可以通过给我一个翻译文件的链接来帮助我,以便我可以下载该文件并将其用于我的目的。我的工作确实急需
    • 我坚信您的笔记本电脑应该足够好。文件大小仅为 600MB。如果您的笔记本电脑 RAM >=4GB,我认为它应该可以正常工作。尝试使用 python shell 而不是 Jupyter 运行代码。也可以用 Jupyter 试试这个:jupyter notebook -- NotebookApp.iopub_data_rate_limit=1.0e10 你能发布错误吗?
    • 另外,不要指望 StackOverflow 上的任何人来做你的工作。这个平台不应该以这种方式工作。向我们展示您的错误,我们一定会提供帮助:)。
    • 是的,我的笔记本电脑有超过 4GB 的 RAM,我希望是最好的。如果我问错了问题,我很抱歉。我尝试在 python shell 上运行代码以获得相同的内存错误。当我尝试使用上面提到的参数运行 jupyter notebook 的代码时,我收到了这个错误: MemoryError Traceback (most recent call last) MemoryError:
    • 我已经更新了我的答案。这绝对适合你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多