Google Natural Language API analyze_sentiment错误

时间:2017-08-21 20:24:59

标签: python google-cloud-platform

我正在尝试按照此处的指南运行样本情绪分析: 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))

我似乎无法找到有关错误的任何文档 - 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

您使用这样的客户端吗?

这是gcp中的字符串。

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

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