Tutorial: Sentiment analysis with Cognitive Services

In this tutorial, you'll learn how to easily enrich your data in Azure Synapse Analytics with Azure Cognitive Services. You'll use the Text Analytics capabilities to perform sentiment analysis.

A user in Azure Synapse can simply select a table that contains a text column to enrich with sentiments. These sentiments can be positive, negative, mixed, or neutral. A probability will also be returned.

This tutorial covers:

  • Steps for getting a Spark table dataset that contains a text column for sentiment analysis.
  • Using a wizard experience in Azure Synapse to enrich data by using Text Analytics in Cognitive Services.

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

Sign in to the Azure portal

Sign in to the Azure portal.

Create a Spark table

You'll need a Spark table for this tutorial.

  1. Download the FabrikamComments.csv file, which contains a dataset for text analytics.

  2. Upload the file to your Azure Synapse storage account in Data Lake Storage Gen2.

    Screenshot that shows selections for uploading data.

  3. Create a Spark table from the .csv file by right-clicking the file and selecting New Notebook > Create Spark table.

    Screenshot that shows selections for creating a Spark table.

  4. Name the table in the code cell and run the notebook on a Spark pool. Remember to set header=True.

    Screenshot that shows running a notebook.

    %%pyspark
    df = spark.read.load('abfss://default@azuresynapsesa.dfs.core.windows.net/data/FabrikamComments.csv', format='csv'
    ## If a header exists, uncomment the line below
    , header=True
    )
    df.write.mode("overwrite").saveAsTable("default.YourTableName")
    

Open the Cognitive Services wizard

  1. Right-click the Spark table created in the previous procedure. Select Machine Learning > Predict with a model to open the wizard.

    Screenshot that shows selections for opening the scoring wizard.

  2. A configuration panel appears, and you're asked to select a Cognitive Services model. Select Sentiment Analysis.

    Screenshot that shows selection of a Cognitive Services model.

Configure sentiment analysis

Next, configure the sentiment analysis. Select the following details:

  • Azure Cognitive Services linked service: As part of the prerequisite steps, you created a linked service to your Cognitive Services . Select it here.
  • Language: Select English as the language of the text that you want to perform sentiment analysis on.
  • Text column: Select comment (string) as the text column in your dataset that you want to analyze to determine the sentiment.

When you're done, select Open notebook. This generates a notebook for you with PySpark code that performs the sentiment analysis with Azure Cognitive Services.

Screenshot that shows selections for configuring sentiment analysis.

Run the notebook

The notebook that you just opened uses the SynapseML library to connect to Cognitive Services. The Azure Cognitive Services linked service that you provided allow you to securely reference your cognitive service from this experience without revealing any secrets.

You can now run all cells to enrich your data with sentiments. Select Run all.

The sentiments are returned as positive, negative, neutral, or mixed. You also get probabilities per sentiment. Learn more about sentiment analysis in Cognitive Services.

Screenshot that shows sentiment analysis.

Next steps