{"id":38302,"date":"2020-01-21T06:00:12","date_gmt":"2020-01-21T13:00:12","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/premier-developer\/?p=38302"},"modified":"2020-01-22T13:01:50","modified_gmt":"2020-01-22T20:01:50","slug":"using-azure-cognitive-services-text-analytics-api-version-3-preview-for-sentiment-analysis","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/using-azure-cognitive-services-text-analytics-api-version-3-preview-for-sentiment-analysis\/","title":{"rendered":"Using Azure Cognitive Services Text Analytics API Version 3 Preview for Sentiment Analysis"},"content":{"rendered":"<p>In this post, App Dev Manager Fidelis Ekezue explains how to use Azure Cognitive Services Text Analytics API Version 3 Preview for Sentiment Analysis in nine simple steps.<\/p>\n<hr \/>\n<p>Businesses, public and private sectors respectively, often solicit unstructured comments and reviews from the public and consumers of their policies and products. Traditionally, these comments and reviews are paper based, however, the advancement in information technology has moved the medium for these comments and reviews online. The move to soliciting comments and reviews online has three major impacts namely businesses can collect comments from their consumers quickly and more efficiently, larger number of comments can be obtained within a short period, and opportunity for bots to submit comments. There is an increasing concern of the effect of bots-generated comments and batch comments from advocacy groups inundating the process with a high number of submissions that can skew the outcome. Sifting through large unstructured text data manually and extracting valuable information is time-consuming and difficult. We can analyze many comments today because of advances in Machine Learning, Artificial Intelligence and Natural Language Processing (NLP). One such tool is the <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cognitive-services\/text-analytics\/overview\">Microsoft Cognitive Services Text Analytics API<\/a> Version 3. With this version of the API, the evaluates text and returns overall sentiment scores for the text and the sentiment scores for individual sentences in the text.<\/p>\n<p>In this blog, I will demonstrate how Text Analytics API Version 3 Preview of the Microsoft Azure Cognitive Services can be used to analyze large unstructured data. This analysis aims to understand the sentiments expressed in a solicited public comment process and determines the degree of the positivity or negativity of the comments. Text Analytics V3 expects the document in JSON with the following format: ID, text, and language. One limitation of Text Analytics V3 is that the document must have less than 5,120 characters. The data used for this demonstration is in CSV format and the comments varied from one sentence to several sentences of multiple pages. Given these constraints, I developed python codes in Jupyter notebook to do the following:<\/p>\n<ul>\n<li>Transformed each comment line into a JSON document as expected by the API<\/li>\n<li>Segmented each sentence as separate comment<\/li>\n<li>Called the API to analyze the sentiment one document at a time.<\/li>\n<li>Wrote the sentiment to a CSV file. This file is imported to Power BI to plot the results. A tutorial on <a href=\"https:\/\/powerbi.microsoft.com\/en-us\/\">Microsoft PowerBI<\/a> can be found<a href=\"https:\/\/docs.microsoft.com\/en-us\/power-bi\/guided-learning\/\"> here<\/a>.<\/li>\n<\/ul>\n<p>The Text Analytics API uses pre-trained models from Microsoft&#8217;s extensive work on NLP to analyze the comments and classify them according to the sentiments into four categories, positive, negative, mixed, and neutral. The data used for this demonstration consist of public comments for <a href=\"https:\/\/public.medicaid.gov\/connect.ti\/public.comments\/questionnaireResults?qid=1886531\">North Carolina\u2019s Medicaid Reform<\/a>. The public comment period was from June 2016 to July 2016 and a total of 1,953 responses were received. This analysis did not check for duplicate comments as it focuses on showing how Text Analytics API can be used to analyze and quantify sentiments in comments. This demonstration assumes that you have an Azure subscription and know how to create Azure Cognitive Services Resource. If you do not have an Azure subscription, you can sign up for <a href=\"https:\/\/azure.microsoft.com\/en-us\/free\/\">a free trial account<\/a> and if you are unfamiliar with how to create Azure Cognitive Services, follow this<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cognitive-services\/cognitive-services-apis-create-account?tabs=multiservice%2Cwindows\"> tutorial<\/a>.<\/p>\n<p>Get the source code for this tutorial on GitHub <a href=\"https:\/\/github.com\/fekezue\/sentiment-analysis-with-text-analytics-api-version3-preview\">here<\/a>.<\/p>\n<h2>Step 1: Create Azure Resource Group<\/h2>\n<p><img decoding=\"async\" width=\"721\" height=\"357\" class=\"wp-image-38333\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-32.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-32.png 721w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-32-300x149.png 300w\" sizes=\"(max-width: 721px) 100vw, 721px\" \/><\/p>\n<h2>Step 2: Create Azure Cognitive Services<\/h2>\n<p><img decoding=\"async\" width=\"989\" height=\"443\" class=\"wp-image-38334\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-33.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-33.png 989w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-33-300x134.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-33-768x344.png 768w\" sizes=\"(max-width: 989px) 100vw, 989px\" \/><\/p>\n<p>Supply the name, subscription, location, service plan and resource group. For the resource group, use the resource group created in Step 1 and then click Create.<\/p>\n<p><img decoding=\"async\" width=\"1826\" height=\"574\" class=\"wp-image-38335\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-34.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-34.png 1826w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-34-300x94.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-34-1024x322.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-34-768x241.png 768w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-34-1536x483.png 1536w\" sizes=\"(max-width: 1826px) 100vw, 1826px\" \/><\/p>\n<p>After the resource is created, click on the Go to resource<\/p>\n<p><img decoding=\"async\" width=\"813\" height=\"223\" class=\"wp-image-38336\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-35.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-35.png 813w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-35-300x82.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-35-768x211.png 768w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/p>\n<h2>Step 3: Make Note of the Key and Endpoint<\/h2>\n<p><img decoding=\"async\" width=\"611\" height=\"262\" class=\"wp-image-38337\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-36.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-36.png 611w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-36-300x129.png 300w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><\/p>\n<p>Note that the Text Analytics API Version 3 Preview is only available in certain regions. As of the time of this writing (January 2020), it is available in the following regions:<\/p>\n<p><img decoding=\"async\" width=\"486\" height=\"264\" class=\"wp-image-38338\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-37.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-37.png 486w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-37-300x163.png 300w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/p>\n<h2>Step 4: Setup Python Environment in Jupyter Notebook and Load Helper Files<\/h2>\n<p>You can use <a href=\"https:\/\/notebooks.azure.com\/\">Azure Notebook<\/a> or your preferred Jupyter Notebook like the version that is installed when you install Python from <a href=\"https:\/\/www.anaconda.com\/distribution\/\">Anaconda<\/a> or <a href=\"https:\/\/colab.research.google.com\/\">Google Colab<\/a>. If you are unfamiliar with Azure Jupyter Notebook, please see this <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/notebooks\/tutorial-create-run-jupyter-notebook\">tutorial.<\/a> The Python version used for the demonstration is 3.7.<\/p>\n<p><img decoding=\"async\" width=\"1968\" height=\"306\" class=\"wp-image-38339\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-38.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-38.png 1968w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-38-300x47.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-38-1024x159.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-38-768x119.png 768w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-38-1536x239.png 1536w\" sizes=\"(max-width: 1968px) 100vw, 1968px\" \/><\/p>\n<h2>Step 5: Load the Downloaded Comments File in CSV into a Data Frame<\/h2>\n<p><img decoding=\"async\" width=\"1507\" height=\"781\" class=\"wp-image-38340\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-39.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-39.png 1507w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-39-300x155.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-39-1024x531.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-39-768x398.png 768w\" sizes=\"(max-width: 1507px) 100vw, 1507px\" \/><\/p>\n<h2>Step 6: Load the Downloaded Comments File in CSV into a Data Frame<\/h2>\n<p>The downloaded CSV file can be read from your desktop, OneDrive or uploaded to your Azure storage. The next statement assume you are pointing to the correct location of the file and your file is named as in the snippet. If that is not the case, you can change the statement to point to your own file.<\/p>\n<p><img decoding=\"async\" width=\"1665\" height=\"960\" class=\"wp-image-38341\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-40.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-40.png 1665w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-40-300x173.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-40-1024x590.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-40-768x443.png 768w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-40-1536x886.png 1536w\" sizes=\"(max-width: 1665px) 100vw, 1665px\" \/><\/p>\n<h2>Step 7: Convert the Comments in the Data Frame to JSON Format<\/h2>\n<p>Some of the comments consist of multiple sentences and quite large. Passing the entire file of comments may note be optimal, so the steps below takes each line of comments and convert them to a JSON document in the formation that the Text Analytics API accepts.<\/p>\n<p><img decoding=\"async\" width=\"1971\" height=\"955\" class=\"wp-image-38342\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-41.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-41.png 1971w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-41-300x145.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-41-1024x496.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-41-768x372.png 768w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-41-1536x744.png 1536w\" sizes=\"(max-width: 1971px) 100vw, 1971px\" \/><\/p>\n<h2>Step 8: Get Comment Sentiment<\/h2>\n<p>For each line, the code below gets the comment sentiment by passing the JSON to the Text Analytics API. The result is return for summarization<\/p>\n<p><img decoding=\"async\" width=\"1534\" height=\"939\" class=\"wp-image-38343\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-42.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-42.png 1534w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-42-300x184.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-42-1024x627.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-42-768x470.png 768w\" sizes=\"(max-width: 1534px) 100vw, 1534px\" \/><\/p>\n<h2>Step 9: Summary of Comment Sentiments<\/h2>\n<p><img decoding=\"async\" width=\"1786\" height=\"751\" class=\"wp-image-38344\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-43.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-43.png 1786w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-43-300x126.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-43-1024x431.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-43-768x323.png 768w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-43-1536x646.png 1536w\" sizes=\"(max-width: 1786px) 100vw, 1786px\" \/><\/p>\n<h2><img decoding=\"async\" width=\"967\" height=\"811\" class=\"wp-image-38345\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-44.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-44.png 967w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-44-300x252.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-44-768x644.png 768w\" sizes=\"(max-width: 967px) 100vw, 967px\" \/><\/h2>\n<p><img decoding=\"async\" width=\"933\" height=\"217\" class=\"wp-image-38346\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-45.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-45.png 933w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-45-300x70.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-45-768x179.png 768w\" sizes=\"(max-width: 933px) 100vw, 933px\" \/><\/p>\n<h2>Sentiment Summary with PowerBI<\/h2>\n<p><img decoding=\"async\" width=\"1024\" height=\"554\" class=\"wp-image-38347\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-46.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-46.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-46-300x162.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-46-768x416.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, I will demonstrate how Text Analytics API Version 3 Preview of the Microsoft Azure Cognitive Services can be used to analyze large unstructured data. This analysis aims to understand the sentiments expressed in a solicited public comment process and determines the degree of the positivity or negativity of the comments.<\/p>\n","protected":false},"author":582,"featured_media":38314,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14,25],"tags":[24,72,3],"class_list":["post-38302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","category-azure","tag-azure","tag-cognitive-services","tag-team"],"acf":[],"blog_post_summary":"<p>In this blog, I will demonstrate how Text Analytics API Version 3 Preview of the Microsoft Azure Cognitive Services can be used to analyze large unstructured data. This analysis aims to understand the sentiments expressed in a solicited public comment process and determines the degree of the positivity or negativity of the comments.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/38302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=38302"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/38302\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/38314"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=38302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=38302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=38302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}