{"id":11629,"date":"2022-01-27T15:44:26","date_gmt":"2022-01-27T23:44:26","guid":{"rendered":"https:\/\/threecloud.wpengine.com\/?p=11629"},"modified":"2023-09-19T15:37:10","modified_gmt":"2023-09-19T22:37:10","slug":"sentiment-analysis-textblob-library-python","status":"publish","type":"post","link":"https:\/\/3cloudsolutions.com\/resources\/sentiment-analysis-textblob-library-python\/","title":{"rendered":"Sentiment Analysis Using the Textblob Library in Python"},"content":{"rendered":"<p><img decoding=\"async\" style=\"width: 800px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/Images\/Blog%20\/Data%20Science%20\/python_textblob.png\" alt=\"python_textblob\" width=\"800\" \/><\/p>\n<p>For many beginners, learning to code for a <a href=\"https:\/\/ccganalytics.com\/solutions\/data-science\" target=\"_blank\" rel=\"noopener\">data science project<\/a> can be just as intimidating as flying a Boeing 757 for the first time. Fortunately, Python offers a myriad of powerful libraries to get us beginners quickly into the cockpit to do some cool analyses. One of such Python libraries is Textblob, which provides a simple API for diving into <a href=\"https:\/\/ccganalytics.com\/resources\/case-studies\/professional-services-company-leverages-ai-nlp-to-streamline-construction-proposal-process\" target=\"_blank\" rel=\"noopener\">common natural language processing (NLP) tasks<\/a> such as part-of-speech tagging, noun phrase extraction, sentiment analysis, and much more.In this blog, we are going to learn how to write a simple python program that performs sentiment analysis of Martin Luther King, Jr.\u2019s \u2018I Have a Dream\u2019 speech and writes the results to a .csv file.<\/p>\n<p>Before we hit the throttle and leave the runway, I want to give a brief background of myself and how 3Cloud cultivated my interest in data science. I started at 3Cloud fresh out of college in the summer of 2017. Having no professional experience in neither consulting nor data science, 3Cloud put me through their consultant boot camp\u2014a rigorous 3-week program that covers the ins and outs of consulting, data warehousing, cloud services, data modeling, advanced analytics, and many other tools and soft-skills that every employee needs to succeed. The experience was immensely useful and enjoyable, but one of the courses that really grabbed my attention was the course on advanced analytics.That course was my first introduction to python as a data science language. Our instructor, Ahmed Sherif, gave us real data sets to work with and really pushed us to \u201cthink in python\u201d.By the end of that 2-hour session, I knew that I wanted to do some more cool analyses with python, which led me to write the program I\u2019m about to walk you through.<\/p>\n<p>So let\u2019s get started, shall we?<\/p>\n<p>First, I chose to use Notepad++ to write my code, but feel free to use whatever IDE or text-writing tool you\u2019re comfortable with (FYI <a href=\"https:\/\/jupyter.org\/\" rel=\" noopener\">Jupyter<\/a> is great if you\u2019re a beginner).<\/p>\n<p><img decoding=\"async\" style=\"width: 500px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image1-1.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image1-1\" width=\"500\" \/><\/p>\n<p>In lines 4 and 5, we are importing the <strong>Textblob<\/strong> and <strong>csv<\/strong> libraries.The former is how we will invoke the NLP sentiment analysis functions.The latter is how we will invoke the functions necessary to write our sentiment analysis results to a .csv file.<\/p>\n<p><img decoding=\"async\" style=\"width: 500px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image2.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image2\" width=\"500\" \/><\/p>\n<p>In line\u2019s 9 and 10, we have declared two file path variables. <strong>File_path<\/strong> is the location of the \u201cI Have a Dream\u201d speech and s<strong>entiment_csv_path<\/strong> is the [eventual] location of the sentiment analysis results .csv file.<\/p>\n<p><img decoding=\"async\" style=\"width: 500px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image3.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image3\" width=\"500\" \/><\/p>\n<p>In line 15, we create array <strong>fieldnames<\/strong> that will be used to populate the headers of our .csv file. A quick note about each of these 5 headers. <strong>Sentence_ID<\/strong> represents a unique identifier number to identify each sentence of the speech. <strong>Polarity<\/strong> and <strong>Subjectivity<\/strong> represent the respective sentiment analysis scores for each uniquely identified sentence.<strong>Sentence<\/strong> is to be populated with the text from each uniquely identified sentence. Lastly, <strong>Strong Opinion?<\/strong> is to represent a Boolean value (0=F, 1=T) if the program determines a sentence to be a \u2018strong opinion\u2019\u2014I\u2019ll elaborate on this shortly.<\/p>\n<p><img decoding=\"async\" style=\"width: 500px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image4.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image4\" width=\"500\" \/><\/p>\n<p>In line 19, we declare the <strong>sentence_ID<\/strong> variable which is set to 0. It will later be incremented as the program traverses the text file and identifies each sentence.<\/p>\n<p><img decoding=\"async\" style=\"width: 500px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image5.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image5\" width=\"500\" \/><\/p>\n<p>In lines 22-25 we do three things.Frist, we create the .csv file in the file path location stored in <strong>sentiment_csv_path<\/strong>. Then, we write the headers to the .csv file with the array <strong>fieldnames<\/strong>. Then finally, we close the file.<\/p>\n<p><img decoding=\"async\" style=\"width: 500px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image6-2.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image6-2\" width=\"500\" \/><\/p>\n<p>Lines 29 is where we open the speech file found in the location stored in <strong>file_path.<\/strong>Line 30 read the open speech file through the <strong>TextBlob<\/strong> library.<\/p>\n<p><img decoding=\"async\" style=\"width: 500px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image7.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image7\" width=\"500\" \/><\/p>\n<p>Line 33 takes the open speech file and splits it up into sentences.<\/p>\n<p><img decoding=\"async\" style=\"width: 500px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image8.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image8\" width=\"500\" \/><\/p>\n<p>Now we get to the meat and potatoes of this program. Line 38 is the beginning of a <strong>for<\/strong> loop that loops through each sentence of the speech file. Lines 39-40 assign polarity and subjectivity scores to a sentence coming in from the speech file.<\/p>\n<p>Then in lines 45-48, the program determines if the sentence is a strong opinion based on the polarity and subjectivity scores\u2014If the absolute value of the polarity is greater than 0.7 and the subjectivity is greater than 0.7, then assign the sentence a value of 1 (true)\u2014Otherwise assign a value of 0 (false).<\/p>\n<p>Lines 51-54 open up that .csv file once more, then populate it with data corresponding to each of the 5 headers (<strong>Sentence_ID, Polarity, Subjectivity, Sentence, and Strong Opinion?<\/strong>).<\/p>\n<p>In line 56, we reach the end of the loop.If there is another sentence to be processed, <strong>sentence_ID<\/strong> is incremented by 1. Once there are no more sentences left, the loop completes (breaks).<\/p>\n<p><img decoding=\"async\" style=\"width: 217px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image9.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image9\" width=\"217\" \/><\/p>\n<p>Finally, once the program has looped through each sentence of the speech file, analyzed it through textblob, and published the results to a .csv file, we close the file in line 59. Although you\u2019ll still be able to view the complete results if you skip this step, it is still a programming best practice\u2014especially if you need to access the speech file later in the program via a different library.<\/p>\n<p><img decoding=\"async\" style=\"width: 700px;\" src=\"https:\/\/cdn2.hubspot.net\/hubfs\/5670923\/SENTIMENT%20ANALYSIS%20USING%20THE%20TEXTBLOB%20LIBRARY%20IN%20PYTHON_image10.png\" alt=\"SENTIMENT ANALYSIS USING THE TEXTBLOB LIBRARY IN PYTHON_image10\" width=\"700\" \/><\/p>\n<p>Once your program successfully completes, you should be able to open the .csv file (I opened in Excel here) and have all the analysis results for easy consumption.Now that the results are neatly organized onto a .csv file, there are a multitude of options to store, consume, and creatively visualize this data!<\/p>\n<p>To those of you reading this post who are new to programming and programming for data science\u2014I hope you\u2019ve found this post as helpful and maybe just as inspiring my first exposure to it in 3Cloud&#8217;s consultant boot camp.<\/p>\n<p>To read more on our capabilities, <a href=\"https:\/\/3cloudsolutions.com\/get-started\/\">get in touch today<\/a>.<\/p>\n<p><em>E<\/em><em>ditor\u2019s Note: The post was originally published in [January, 2018] and has been updated for freshness, accuracy and comprehensiveness.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For many beginners, learning to code for a data science project can be just as&mldr;<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[260],"tags":[429,488],"class_list":["post-11629","post","type-post","status-publish","format-standard","hentry","category-data-ai","tag-data-and-ai","tag-python","topics-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/11629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/comments?post=11629"}],"version-history":[{"count":0,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/11629\/revisions"}],"wp:attachment":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media?parent=11629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/categories?post=11629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/tags?post=11629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}