AttributeError: ‘str’ 对象没有属性 ‘before_request’

我刚开始使用 Google 自然语言处理库…尝试从本地文本文件中提取实体,但一直遇到错误。我甚至尝试了 Google 提供的示例代码,但错误依然存在。

这是我的代码:

import sixfrom google.cloud import languagefrom google.cloud.language import enumsfrom google.cloud.language import typesdef 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'

请问如何才能返回实体信息呢?


回答:

credentials 参数不接受字符串,而是需要一个 Credentials 对象。从您的 JSON 文件中创建一个这样的对象并传入即可。

from google.oauth2 import service_accountcredentials = service_account.Credentials.from_service_account_file('cred.json')

从这个文档页面

这里的提示是这一行: self._credentials.before_request。您传入 credentialsLanguageServiceClient 对象将其存储在私有变量 _credentials 中,并尝试调用其上的方法。由于您的字符串没有这个方法,所以会出错。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注