Perform Text Sentiment analysis and Keyphrase extraction from excel and store in Azure Blob storage

Viewed 76

I want to perform Sentiment analysis and keyphrase extraction on text data stored in an excel format. The sentiments and the extracted keyphrases also need to be appended to the same excel and the final excel needs to be stored in Azure blob storage. Finally this needs be made into a flask app. Would be grateful if anyone can help me on this. Thanks in advance..

1 Answers

Your question scope is too wide, so I write a simple demo for you.

Just try the code below to read data from .csv and use Sentiment analysis and then write back to .csv and upload to blob, the only thing you need is to integrate the code with your flask app:

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
from azure.storage import blob
from azure.storage.blob import BlobClient
import pandas as pd

region = ''
key = ''

excelFilePath = "<local file path>/test.csv"

storageConnStr = '<storage conn str>'
containerName = '<container name>'
destBlob = 'test-upload.csv'

csv = pd.read_csv(excelFilePath,'rb')
data =csv['text']
documents = data.array

blob = BlobClient.from_connection_string(storageConnStr,containerName,destBlob)
credential = AzureKeyCredential(key)
text_analytics_client = TextAnalyticsClient(endpoint="https://"+ region +".api.cognitive.microsoft.com/", credential=credential)

response = text_analytics_client.analyze_sentiment(documents, language="en")

sentiments = [res.sentiment for res in response ]
csv.insert(1, "sentiment", sentiments)
csv.to_csv(excelFilePath, index=False)

blob.upload_blob(open(excelFilePath,'rb').read())

Result:

My .csv:

enter image description here

After running :

enter image description here

and it has been uploaded to storage :

enter image description here

Related