Skip to content

Latest commit

 

History

History
223 lines (160 loc) · 10.5 KB

File metadata and controls

223 lines (160 loc) · 10.5 KB

Sentiment Analysis on Verbatim User Feedback on Microsoft Azure using Azure AI Text Analytics Cognitive Service

In this workshop, we will discuss the use of Microsoft Azure for Text Analytics to analyze the sentiments of Azure users.

Project Goal Learn how to use AI Text Analytics to extract meaningful insights from text
What will you learn How to use Azure Text Analytics Cognitive Service, How to analyze and visualize data with Python
What you'll need Azure Subscription, ability to run Jupyter Notebook
Duration 1hr-1.5 hours
Just want to try the app or see the solution? a link to the completed project sample app or solution folder
Slides Powerpoint

Pre-Learning

Check out Cognitive Services for Language

Prerequisites

For this workshop:

  1. You need to have an Azure Account. You can get either the Azure for Students version or use the Azure Free Trial version
  2. You need to have working knowledge of Jupyter Notebooks on your local machine. Alternatively, you can locally install Python and use it on VS Code or on Github Codespaces

What students will learn

In this workshop, students would learn to use Azure's Text Analytics service for performing a sentiment analysis on user data.

image of completed project

Milestone 1: Getting and Understanding the Data

The data has been manually aggregated by collecting feedback from Azure users. Check out the Data folder to access feedback data segregated into 5 files:

  1. AzComparison.txt contains a comparitive analysis of Azure with other cloud providers from the users' perspective.
  2. AzEduhub.txt contains the users' overall insights regarding Azure
  3. AzNegative.txt contains the complaints users have against Azure
  4. Azpositive.txt contains positive user reviews on Azure
  5. AzPMAdvice.txt contains users' advice for Azure's Program Managers

If you are an Azure user, you could amplify our database of user feedback by filling up this feedback form. The required modifications would be made in the data in this repository itself.

Milestone 2: Running the Notebook and Exploring the Data

After you get the data, you need to create a new ipython notebook or Detailed.ipynb notebook and start writing code there, following the instructions inside the notebook.

Note: There are two versions of the notebooks provided, and you can chose the one most suitable to you: There is also a notebook with the solution, which you can consult should you experience a problem you are not able to solve. However, we suggest you to try and solve all the problems yourself, using stack overflow as a reference to find solutions.

Milestone 3: Creating and Using the Text Analytics Endpoint

We shall now be using the Text Analytics Service to analyze the data collected. At this point, you must have your Azure Subscription ready.

Log into your Azure Portal and create a new Cognitive Resource for Language. Start creating the resource here

After the creation of the resource, navigate to the Keys and Endpoints option under the Resource Management pane and note either of the keys and the endpoint. Once you have created the resource, you should go to the portal and copy Endpoint URL and Access key into the notebook.

In order to call this service, the key and endpoint must be noted and copied.

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient, AnalyzeSentimentAction, ExtractKeyPhrasesAction

credential = AzureKeyCredential("paste_your_key_here")
cli = TextAnalyticsClient(endpoint="https://analyze-user-sentiments.cognitiveservices.azure.com/", credential=credential)

Test the use of this service by performing a simple sentiment analysis.

def analyze(doc):
    pol = cli.begin_analyze_actions(doc,actions=[AnalyzeSentimentAction(), ExtractKeyPhrasesAction() ])
    res = pol.result()
    return [
        { 
        "doc" : d,
        "sent" : sent.sentiment if not sent.is_error else None,
        "pos_score" : sent.confidence_scores.positive if not sent.is_error else None,
        "neg_score" : sent.confidence_scores.negative if not sent.is_error else None,
        "neu_score" : sent.confidence_scores.neutral if not sent.is_error else None,
        "key" : keyw.key_phrases if not keyw.is_error else []
        }
      for d,(sent,keyw) in zip(doc,res)]

analyze(["I was so glad to be a part of this festival"]) 

This code snippet is used to accept the text "I was so glad to be a part of this festival" and analyze the sentiment trying to be conveyed by the user. It returns the input string, the sending success message, the positive score,the negative score, the neutral score and the key. The output for the aforementioned code snippet depicts a positive score of 1.0 and a negative and a neutral score of 0.0 respectively. This indicates that the general sentiment expressed by the user was positive.

[{'doc': 'I was so glad to be a part of this festival',
  'sent': 'positive',
  'pos_score': 1.0,
  'neg_score': 0.0,
  'neu_score': 0.0,
  'key': ['part', 'festival']}]

Process the Data

Now, the data collected must be finally processed to derive sentimental insights from them. Analyzing the first 10 records:

def analyzex(l):
    res = []
    for i in range(0,len(l),10):
        res.extend(analyze(l[i:i+10]))
    return res

dt = {}
for k,v in data.items():
    print(f"Processing {k}")
    dt[k] = analyzex(v)

Once all the data files have been processed, the analysis is pickled, so that it can be retrieved at a later date.

import pickle
with open('data.pkl','wb') as f:
    pickle.dump(dt,f)

Text Analytics Service can now perform sentiment analysis on the user feedback to express the general sentiment the Azure user intends to convey. One example of sentriment analysis is attached below.

{'Azpositive.txt': [{'doc': 'the sheer performance and the simplification of complex tasks with less prequisitives.',
   'sent': 'neutral',
   'pos_score': 0.05,
   'neg_score': 0.13,
   'neu_score': 0.82,
   'key': ['sheer performance',
    'complex tasks',
    'less prequisitives',
    'simplification']},

Let us now split the code to find the Negative and the Positive feedback.

print("=== NEGATIVE ===\n"+'\n'.join([x['doc'] for x in dt['AzNegative.txt'] if x['sent']=='negative']))
print("=== POSITIVE ===\n"+'\n'.join([x['doc'] for x in dt['Azpositive.txt'] if x['sent']=='positive']))

The output for this is:

=== NEGATIVE ===
lack in demonstration
i'd say sometimes it's overwhelming for business practitioners due to its compact design so maybe user interface could be made more business friendly (especially for companies in countries with non-english native language such as japan)
it has a slower interface.
when creating a vm fails
the free credits last only 6 months :(
when i started to use it, i found the interface a bit tricky to get to grips with
=== POSITIVE ===
it's user friendly
it is easy to use
azure is pretty easy to use with all the learning paths etc. and it is relatively easy to migrate (pay-as-you-go). azure is integrated with many cognitive services so one can store their
simple to use
they give free credits to students :)
the free $100 a year for students

Now, individual keywords could be analyzed, and their frequency of occurence, average sentiment and dev value could be represented.

w = {}
for k,v in dt.items():
    for doc in v:
        for w in doc['key']:
            if w in kw.keys():
                kw[w].append(doc['pos_score']-doc['neg_score'])
            else:
                kw[w] = [doc['pos_score']-doc['neg_score']]

df = pd.DataFrame([
    {
        "word" : w,
        "num of occurrences" : len(l),
        "average sentiment" : pd.Series(l).mean(),
        "dev" : pd.Series(l).std()
    }
    for w,l in kw.items()])

top_words = df.sort_values('num of occurrences',ascending=False).head(10)
top_words

Display output of table

You can also view a scatterplot of the sentiments conveyed by the users.

Scatter plot

Milestone 5: Create a WordCloud

tips, tricks, callouts

Quiz or Code Challenge

Add an activity to demonstrate that students have comprehension of the material

Next steps

links to Microsoft Learn to further learning progress, and/or a path to certifications

Optional Transfer knowledge activity

suggest, or add as an addendum, a way to extend students knowledge of the topic by creating a new app or demo that builds on the original workshop materials.

Feedback

Be sure to give feedback about this workshop!

Code of Conduct