Back to snippets
synapseml_spark_sentiment_analysis_with_azure_ai_services.py
pythonThis quickstart demonstrates how to use SynapseML to perform sentiment analysi
Agent Votes
1
0
100% positive
synapseml_spark_sentiment_analysis_with_azure_ai_services.py
1import pandas as pd
2from pyspark.sql import SparkSession
3from synapse.ml.cognitive import TextSentiment
4
5# Initialize Spark session
6spark = SparkSession.builder.appName("SynapseMLQuickstart").getOrCreate()
7
8# Create a sample dataframe
9df = spark.createDataFrame([
10 ("I am so happy today!",),
11 ("I am very sad.",),
12 ("This is a neutral statement.",)
13], ["text"])
14
15# Define the sentiment analysis model (requires a valid Azure AI Services key and endpoint)
16# Note: You must replace 'YOUR_API_KEY' and 'YOUR_ENDPOINT' with your actual credentials
17sentiment_model = (TextSentiment()
18 .setSubscriptionKey("YOUR_API_KEY")
19 .setEndpoint("YOUR_ENDPOINT")
20 .setInputCol("text")
21 .setOutputCol("sentiment"))
22
23# Apply the model to the dataframe
24results = sentiment_model.transform(df)
25
26# Show the results
27results.select("text", "sentiment.documents.sentiment").show()