【发布时间】: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