谷歌云用Python分析JupyterLab中的情绪



我正在使用谷歌云/JupyterLab/Python

我正在尝试运行一个样本情绪分析,遵循这里的指南

然而,在运行该示例时,我得到了以下错误:

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)

我使用语音到文本生成成绩单没有问题,但没有成功地进行文档情感分析!?

按照文档示例执行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

因此,我尝试了以前的代码,并在谷歌云存储的存储桶中创建了一个文本文件的路径。

response = sample_analyze_sentiment("<my-path>")
sentiment = response.document_sentiment
print(sentiment.score)
print(sentiment.magnitude)

我跑得很成功,情绪得分为-0.5,震级为1.5。我在JupyterLab中用python3进行了跑步,我想这就是你的设置。

最新更新