Google Cloud使用Python在JupyterLab中分析情绪

2024-05-23 18:37:07 发布

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

我正在使用googlecloud/JupyterLab/Python

我正试图按照指南here运行一个情绪分析样本

但是,在运行该示例时,出现以下错误:

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

我没有问题生成的文字记录使用语音文字,但没有成功地得到一个文件情感分析


Tags: txtclientgstypeuricontentlanguageaudio
1条回答
网友
1楼 · 发布于 2024-05-23 18:37:07

按照documentation示例执行分析情绪没有问题。 我对你的代码有一些问题。对我来说应该是

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云存储中一个存储桶中的文本文件的路径

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

我的情绪得分为-0.5,震级为1.5,这是一次成功的跑步。我使用python3在JupyterLab中执行了运行,我假设这就是您的设置

相关问题 更多 >