我正在使用Google Cloud / JupyterLab / Python
我正在尝试进行样本情感分析,遵循指南here
但是,在运行示例时,出现此错误:
AttributeError:“ SpeechClient”对象没有属性'analyze_sentiment'
下面是我正在尝试的代码:
def sample_analyze_sentiment (gcs_content_uri):
gcs_content_uri = 'gs://converted_audiofiles/Converted_Audio/200315_1633 1.txt'
client = language_v1.LanguageServiceClient()
type_ = enums.Document.Type.PLAIN_TEXT
language = "en" document = {
"gcs_content_uri":'gs://converted_audiofiles/Converted_Audio/200315_1633 1.txt',
"type": 'enums.Document.Type.PLAIN_TEXT', "language": 'en'
}
response = client.analyze_sentiment(document,
encoding_type=encoding_type)
我使用“语音转文本”生成成绩单没有问题,但是没有成功进行文档情感分析!?
在documentation示例之后,我可以很好地执行analyze_sentiment。我有一些关于您的代码的问题。对我来说应该是
from google.cloud import language_v1
from google.cloud.language import enums
from google.cloud.language import types
def sample_analyze_sentiment(path):
#path = 'gs://converted_audiofiles/Converted_Audio/200315_1633 1.txt'
# if path is sent through the function it does not need to be specified inside it
# you can always set path = "default-path" when defining the function
client = language_v1.LanguageServiceClient()
document = types.Document(
gcs_content_uri = path,
type = enums.Document.Type.PLAIN_TEXT,
language = 'en',
)
response = client.analyze_sentiment(document)
return response
因此,我尝试了之前的代码,并使用自己的路径访问了Google Cloud Storage存储桶中的文本文件。
response = sample_analyze_sentiment("<my-path>")
sentiment = response.document_sentiment
print(sentiment.score)
print(sentiment.magnitude)
我的情绪得分为-0.5,强度为1.5。我使用python3在JupyterLab中进行了运行,我认为这是您的设置。