【问题标题】:AttributeError: 'str' object has no attribute 'before_request'AttributeError:“str”对象没有属性“before_request”
【发布时间】:2018-10-12 10:05:48
【问题描述】:

我是 Google 自然语言处理库的新手...并尝试从本地文本文件中获取实体,但不断出错。我什至试过谷歌的示例代码,但错误是一样的。

这是我的代码:

import six
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types



def entities_text(text):
    """Detects entities in the text."""
    client = language.LanguageServiceClient(credentials='cred.json')

    if isinstance(text, six.binary_type):
        text = text.decode('utf-8')

    # Instantiates a plain text document.
    document = types.Document(
        content=text,
        type=enums.Document.Type.PLAIN_TEXT)

    # Detects entities in the document. You can also analyze HTML 
    with:
    #   document.type == enums.Document.Type.HTML
    entities = client.analyze_entities(document).entities

    # entity types from enums.Entity.Type
    entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION',
               'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER')

    for entity in entities:
        print('=' * 20)
        print(u'{:<16}: {}'.format('name', entity.name))
        print(u'{:<16}: {}'.format('type', entity_type[entity.type]))
        print(u'{:<16}: {}'.format('metadata', entity.metadata))
        print(u'{:<16}: {}'.format('salience', entity.salience))
        print(u'{:<16}: {}'.format('wikipedia_url',
            entity.metadata.get('wikipedia_url', '-')))



if __name__ == "__main__":
    with open('test.txt', 'r') as text:
        text = text.read()
    ent = entities_text(text)
    print(ent)

这是堆栈跟踪:

AuthMetadataPluginCallback " 
<google.auth.transport.grpc.AuthMetadataPlugin object at 
0x7f6973b4a668>" raised exception!
Traceback (most recent call last):
File "/home/user/Documents/CODE/venv/lib/python3.6/site- 
packages/grpc/_plugin_wrapping.py", line 79, in __call__
   callback_state, callback))
File "/home/user/Documents/CODE/venv/lib/python3.6/site- 
   packages/google/auth/transport/grpc.py", line 77, in __call__
   callback(self._get_authorization_headers(context), None)
File "/home/user/Documents/CODE/venv/lib/python3.6/site- 
   packages/google/auth/transport/grpc.py", line 61, in 
   _get_authorization_headers
   self._credentials.before_request(
AttributeError: 'str' object has no attribute 'before_request'

请问如何让它返回实体?

【问题讨论】:

  • 请更正代码缩进,因为我们不知道这是您的代码错误还是stackoverflow中的格式错误。我看到至少 2 个奇怪的情况:with open('...') as text 行以及函数 def entities_text(text): 中的单独 with 语句
  • 另外,请添加完整的错误堆栈跟踪。与流行的看法相反,我们通常无法猜测错误是什么以及为什么会发生。
  • 更新了堆栈跟踪@Ralf

标签: python machine-learning google-cloud-platform nlp


【解决方案1】:

credentials 参数不接受 str,但接受 Credentials 对象。从您的 JSON 文件创建一个并将其传入。

from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
'cred.json')

From this doc page

这里的提示是这一行:self._credentials.before_request。你传入credentialsLanguageServiceClient 对象将它放入私有变量_credentials 并尝试调用它的方法。由于您的字符串没有该方法,因此它会爆炸。

【讨论】:

  • 非常感谢@dwagnerkc。非常感谢您的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 2021-10-04
  • 2019-12-02
  • 2021-09-25
  • 2014-03-04
  • 2013-09-22
相关资源
最近更新 更多