{"id":12164,"date":"2026-01-04T21:01:55","date_gmt":"2026-01-04T21:01:55","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=12164"},"modified":"2026-01-04T21:37:00","modified_gmt":"2026-01-04T21:37:00","slug":"sentiment-analysis-using-python","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/sentiment-analysis-using-python\/","title":{"rendered":"Sentiment analysis using Python and TextBlob"},"content":{"rendered":"<p>Let&#8217;s create a really simple FastAPI with, create yourself our app file (app.py) and requirements file (requirements.txt).<\/p>\n<p><em>We&#8217;re going to use <a href=\"https:\/\/textblob.readthedocs.io\/en\/dev\/\" target=\"_blank\">TextBlob<\/a> to process our text.<\/em><\/p>\n<p>In the requirements.txt add the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfastapi\r\nuvicorn\r\ntextblob\r\n<\/pre>\n<p>In the app.py add the following imports<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom fastapi import FastAPI\r\nfrom textblob import TextBlob\r\n<\/pre>\n<p>Now let&#8217;s create the FastAPI and a POST endpoint named sentiment, the code should look like this<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@app.post(&quot;\/sentiment&quot;)\r\ndef analyze_sentiment(payload: dict):\r\n    text = payload&#x5B;&quot;text&quot;]\r\n    blob = TextBlob(text)\r\n    polarity = blob.sentiment.polarity\r\n    subjectivity = blob.sentiment.subjectivity\r\n    return {\r\n        &quot;polarity&quot;: polarity,\r\n        &quot;subjectivity&quot;: subjectivity\r\n    }\r\n<\/pre>\n<p>Don&#8217;t forget to run pip install <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npip install -r requirements.txt\r\n<\/pre>\n<p>or if you&#8217;re using PyCharm, let this install the dependencies.<\/p>\n<p>Run the app using <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nuvicorn app:app --reload\r\n<\/pre>\n<p><em>Note: as we&#8217;re using FastAPI we can access the OpenAPI interface using http:\/\/localhost:8000\/docs<\/em><\/p>\n<p>Now from curl run <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ncurl -X POST http:\/\/localhost:8000\/sentiment -H &quot;Content-Type: application\/json&quot; -d &#039;{&quot;text&quot;: &quot;I absolutely love this!&quot;}&#039;<\/pre>\n<p>and you&#8217;ve see a result along the following lines<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{&quot;polarity&quot;:0.625,&quot;subjectivity&quot;:0.6}\r\n<\/pre>\n<p>Polarity is within the range [-1.0, 1.0], where -1.0 is a very negative sentiment, 0, neutral sentiment and 1.0 very positive sentiment. Subjectivity is in the range [0.0. 1.0] where 0.0 is very objective (i.e. facts or neutral statements) and 1.0 is very subjective (i.e. opinions, feelings or personal judgement).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s create a really simple FastAPI with, create yourself our app file (app.py) and requirements file (requirements.txt). We&#8217;re going to use TextBlob to process our text. In the requirements.txt add the following fastapi uvicorn textblob In the app.py add the following imports from fastapi import FastAPI from textblob import TextBlob Now let&#8217;s create the FastAPI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[195],"tags":[],"class_list":["post-12164","post","type-post","status-publish","format-standard","hentry","category-python"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/12164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=12164"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/12164\/revisions"}],"predecessor-version":[{"id":12169,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/12164\/revisions\/12169"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=12164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=12164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=12164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}