【问题标题】:Google Natural Language API analyze_sentiment errorGoogle 自然语言 API 分析情绪错误
【发布时间】:2020-03-07 07:33:20
【问题描述】:

我正在尝试按照此处的指南运行示例情绪分析: https://cloud.google.com/natural-language/docs/analyzing-sentiment#language-sentiment-string-python

但是,在运行示例时,我收到此错误:

AttributeError: 'Client' 对象没有属性 'analyze_sentiment'

下面是我正在尝试的代码:

def sentiment_text(text):
"""Detects sentiment in the text."""
client = language.Client.from_service_account_json('<my_creds>')

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 sentiment in the document. You can also analyze HTML 
with:
    #   document.type == enums.Document.Type.HTML
    sentiment = client.analyze_sentiment(document).document_sentiment
    print('Score: {}'.format(sentiment.score))
    print('Magnitude: {}'.format(sentiment.magnitude))

我似乎找不到任何有关该错误的文档 - 我是否遗漏了什么?

【问题讨论】:

    标签: python google-cloud-platform


    【解决方案1】:

    你用过这样的客户端吗?

    这是用于 gcp 中的字符串。

    client = language_v1.LanguageServiceClient.from_service_account_json("key1.json")
    

    当您只调用语言时,它是用于文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 2016-11-06
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多