Chris Webb's BI Blog

Analysis Services, MDX, PowerPivot, DAX and anything BI-related

Sentiment Analysis In Excel With Azure Machine Learning And Power Query

with 19 comments

You may have seen Joseph Sirosh’s blog post last week about the ability to publish Azure Machine Learning models to the Azure Marketplace, and that MS have published a number of APIs there already. There’s a new Excel add-in that can be used with these APIs but I noticed that at least one of them, the Sentiment Analysis API, can be used direct from Power Query too.

To do this, the first thing you need to do is to go to the Azure Marketplace, sign in with your Microsoft account, and subscribe to the Lexicon Based Sentiment Analysis API. The docs say you get 25000 transactions free per month although there doesn’t appear to be a way to pay for more; that said the number of transactions remaining shown on my account kept resetting, so maybe there is no limit. The API itself is straightforward: pass it a sentence to evaluate and it will return a score between –1 and 1, where 1 represents a positive sentiment and –1 is a negative sentiment. For example, the sentence “I had a good day” returns the value 1:

image

…whereas the sentence “I had a bad day” returns –1:

image

You can now go to Power Query and click From Azure Marketplace (you will need to enter your credentials at this point if this is the first time you’ve used the Azure Marketplace from Power Query):

image

…and then, when the Navigator pane appears, double-click on Score:

image

The API is exposed as a function in Power Query (I guess because it’s an OData service operation, but I’m not sure) and double-clicking on Score invokes the function. You can enter a sentence here and the Query Editor will open to show the score:

image

image

However, to do anything useful you don’t want to invoke the function just yet – you want a query that returns the function. To do this you need to edit the query. Go to the Advanced Editor and you’ll see the M code for the query will be something like this:

let
    Source = Marketplace.Subscriptions(),
    #"https://api.datamarket.azure.com/data.ashx/aml_labs/
      lexicon_based_sentiment_analysis/v1/" = 
      Source{
       [ServiceUrl="https://api.datamarket.azure.com/data.ashx/aml_labs/
       lexicon_based_sentiment_analysis/v1/"]
       }[Feeds],
    Score1 = 
       #"https://api.datamarket.azure.com/data.ashx/aml_labs/
       lexicon_based_sentiment_analysis/v1/"
       {[Name="Score"]}[Data],
    #"Invoked FunctionScore1" = Score1("I had a good day")
in
    #"Invoked FunctionScore1"

 

You need to remove the last line (called #”Invoked FunctionScore1”) which invokes the function, leaving:

let
    Source = Marketplace.Subscriptions(),
    #"https://api.datamarket.azure.com/data.ashx/aml_labs/
      lexicon_based_sentiment_analysis/v1/" = 
      Source{[ServiceUrl="https://api.datamarket.azure.com/data.ashx/aml_labs/
      lexicon_based_sentiment_analysis/v1/"]}[Feeds],
    Score1 = #"https://api.datamarket.azure.com/data.ashx/aml_labs/
      lexicon_based_sentiment_analysis/v1/"
      {[Name="Score"]}[Data]
in
    Score1

 

You can now click the Close and Load button to close the Query Editor window; you now have a function called Score that you can call in other queries. For example, take the following Excel table (called Sentences):

image

Click on the From Table button to load this table into Power Query, then in the Query Editor click the Add Custom Column button and add a new custom column called Sentiment Score with the expression

Score([Sentence])

image

You’ll then be prompted to set a data privacy level for the data you’re using, because calling this function involves sending data from your worksheet up to the API where someone could potentially see it.

image

Click the Continue button and set the privacy level for the workbook to Public, so that this data can be sent to the API:

image

Click Save and you’ll see the sentiment score has been added as a column containing a Record value. Click on the Expand icon in the SentimentScore column then OK:

image

And at last you’ll see the scores themselves:

image

Here’s the code:

let
    Source = Excel.CurrentWorkbook(){[Name="Sentences"]}[Content],
    #"Added Custom" = Table.AddColumn(Source, "SentimentScore", each Score([Sentence])),
    #"Expand SentimentScore" = 
       Table.ExpandRecordColumn(#"Added Custom", "SentimentScore", 
       {"result"}, {"SentimentScore.result"})
in
    #"Expand SentimentScore"

 

You can download the sample workbook here.

Of course, I couldn’t resist repeating this exercise with all of my Facebook status updates – which, of course, can be accessed from Power Query very easily. Here’s my monthly average sentiment score from June 2009 to now:

image

As you can see, I was in a particularly good mood this August – probably because I was on holiday for almost the whole month.

Written by Chris Webb

October 19, 2014 at 11:37 pm

19 Responses

Subscribe to comments with RSS.

  1. Reblogged this on Excel B2B and commented:
    Sentiment Analysis In Excel

    Excel B2B

    October 20, 2014 at 2:37 pm

  2. How fantastic is that…Thanks…Machine learning + PowerBI start getting some really cool potential..I will try tweets….
    Had you tried any other Machine learning service in azure..there are plenty..

    Konstantinos Ioannou

    October 20, 2014 at 2:41 pm

    • I looked at a few and I couldn’t connect to them from Power Query, unfortunately. I didn’t check all of them though and there may be others that are accessible.

      Chris Webb

      October 20, 2014 at 3:15 pm

  3. Unfortunately not available in my Market.
    Probably don’t wish to analyse my sentiment about it !!

    -Regis

    regbac

    October 20, 2014 at 8:05 pm

    • Which market are you trying to access it from?

      Jaya

      October 24, 2014 at 6:55 pm

  4. […] Abbiamo parlato di fogli di calcolo in SemanticSEOWut #13 e anche in SemanticSEOWut #15 ed ecco un nuovo capitolo, ma questa volta con Microsoft nel post “Sentiment Analysis In Excel With Azure Machine Learning And Power Query”. […]

  5. I have a Microsoft account and Office365 organisation account both based in UK. When I tried to access Lexicon Based Sentiment Analysis API – I get “Not available in your market”. Are you using different market (country/region)?

    S.S

    November 2, 2014 at 11:28 pm

    • No, I have a UK-based Microsoft account. Not sure what’s happening here…

      Chris Webb

      November 3, 2014 at 12:10 am

      • I’m able to access now. It is accessible only to Country/Region United States. I changed that setting under My Account. NB: If you have any active data subscriptions you cannot change the Country/Region. I cancelled all my data subscriptions and then changed this setting.

        S.S

        November 5, 2014 at 8:22 pm

      • Thanks S.S this did the trick !

        regbac

        November 5, 2014 at 9:57 pm

  6. […] les données de Facebook. Bien que cela a déjà été détaillé sur quelques blogs dont celui de Chris Webb, voici quand même le détail de […]

  7. Great post and blog. I was wondering if your book covers using the “M” language to parse an XML file as data source. If not could you point me in the right direction? Thanks

    John Donnelly

    November 7, 2014 at 12:43 am

    • The book does give an introduction to M, and there is a description of how Power Query treats XML, but there aren’t any specific M examples for XML. However once you’ve learned M you should be able to work out how to do what you need to do with your XML file.

      Chris Webb

      November 7, 2014 at 6:37 am

  8. Hi Chris, I was reading some of the M language documentation and I was wondering if you knew any code that can auto load an invoked function or automatically rename the function through M (for referencing purposes).

    Essentially, we’d like to have a function that takes an employees name as a parameter and returns one row with all of their relavent information and reference those values in other worksheets so they can auto populate once the function is invoked.

    Mark

    November 12, 2014 at 7:19 pm

    • Can you give me more of an idea about what you want to do? I take it that it’s not an option to store the employee name anywhere in the worksheet?

      Chris Webb

      November 14, 2014 at 12:36 pm

      • So the main query hosts all information for all employees, we’d like to have it so someone can invoke a function one a specific person, and once that function is invoked (returning 1 record), we can reference all of those values into a separate worksheet that populates a template. I think the load to worksheet option will work fine as you mentioned. I was just curious if there were functions to perform renaming of functions, queries, or specifying the load options through M.

        I also saw some try/catch documentation but wasn’t able to figure out how I can use it to perform validation when the function is invoked. So, if they enter an employee name that doesn’t exist, it will come back with an error message. I just realized the name parameter is case sensitive, just like M itself! I think a reminder message the user would be helpful in this case. Thanks for the help!

        Mark White

        November 14, 2014 at 1:49 pm

      • You can’t rename functions, but you can have functions that return other functions: https://cwebbbi.wordpress.com/2014/08/24/power-query-functions-that-return-functions/

        Chris Webb

        November 14, 2014 at 3:01 pm

  9. […] Sentiment Analysis In Excel With Azure Machine Learning And Power QueryБесплатный онлайн-сервис определения тональности текста. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 3,637 other followers

%d bloggers like this: