谷歌自然语言API分析

2024-05-19 23:03:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着运行一个情绪分析样本,遵循以下指南: https://cloud.google.com/natural-language/docs/analyzing-sentiment#language-sentiment-string-python

但是,在运行示例时,我得到以下错误:

AttributeError: 'Client' object has no attribute '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))

我似乎找不到关于这个错误的任何文档-我遗漏了什么吗?在


Tags: thetextinclienttype错误documentlanguage