{"id":1475,"date":"2019-02-24T10:44:03","date_gmt":"2019-02-24T05:14:03","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1475"},"modified":"2019-02-24T11:41:58","modified_gmt":"2019-02-24T06:11:58","slug":"emotion-detection-using-paralleldots-api","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2019\/02\/24\/emotion-detection-using-paralleldots-api\/","title":{"rendered":"Emotion Detection using ParallelDots API"},"content":{"rendered":"<p><a href=\"https:\/\/www.rangakrish.com\/index.php\/2019\/02\/17\/identifying-emotions-from-text\/\" target=\"_blank\" rel=\"noopener\"><em><strong>Last week<\/strong><\/em><\/a>, I showed how we can use <em><strong>IBM Natural Language Understanding<\/strong> <strong>API<\/strong><\/em> to identify emotions from given text. Today, I would like to run through the same examples, but using <a href=\"https:\/\/www.paralleldots.com\/emotion-detection\" target=\"_blank\" rel=\"noopener\"><em><strong>ParallelDots API<\/strong> <strong>service<\/strong><\/em><\/a>.<\/p>\n<p>There are <a href=\"https:\/\/www.paralleldots.com\/api-wrappers\" target=\"_blank\" rel=\"noopener\"><em><strong>wrappers<\/strong><\/em><\/a>\u00a0in <em><strong>Java<\/strong><\/em>, <em><strong>Python<\/strong><\/em>, <em><strong>Ruby<\/strong><\/em>, <em><strong>C#<\/strong><\/em>, and <em><strong>PHP<\/strong><\/em> for accessing the REST service. However, I chose to write my own implementation in <em><strong>Lisp<\/strong><\/em> (you can get the program <a href=\"http:\/\/www.rangakrish.com\/downloads\/Paralleldots-Emotion-API.lisp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>). The core functions are shown below:<\/p>\n<figure id=\"attachment_1476\" aria-describedby=\"caption-attachment-1476\" style=\"width: 576px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1476\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2019\/02\/24\/emotion-detection-using-paralleldots-api\/code-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code-1.jpg\" data-orig-size=\"576,420\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1550930246&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"The Code\" data-image-description=\"&lt;p&gt;The Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code-1.jpg\" class=\"size-full wp-image-1476\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code-1.jpg?resize=576%2C420&#038;ssl=1\" alt=\"The Code\" width=\"576\" height=\"420\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code-1.jpg?w=576&amp;ssl=1 576w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code-1.jpg?resize=300%2C219&amp;ssl=1 300w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a><figcaption id=\"caption-attachment-1476\" class=\"wp-caption-text\"><strong>The Code<\/strong><\/figcaption><\/figure>\n<p>I think the code is self explanatory, so I won&#8217;t go into the details.<\/p>\n<p>Before we try our sample sentences, we have to create a &#8220;<em><strong>paralleldots<\/strong><\/em>&#8221; object first:<\/p>\n<blockquote><p>CL-USER 1 &gt; <span style=\"color: #ff0000;\">(setf pd (paralleldots +PARALLELDOTS-API-KEY+))<\/span><\/p>\n<p>#S(PARALLELDOTS :API-KEY &#8220;&lt;Key&gt;&#8221; :RESULT NIL)<\/p><\/blockquote>\n<p>Substitute your API key instead of <em><strong>&#8220;<\/strong><strong>+PARALLELDOTS-API-KEY+&#8221;<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Here is our first sentence:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">&#8220;It was scary to drive alone on the highway.&#8221;<\/span><\/p><\/blockquote>\n<p>We submit this for emotion detection by invoking the &#8220;<em><strong>emotion<\/strong><\/em>&#8221; method:<\/p>\n<blockquote><p>CL-USER 2 &gt; <span style=\"color: #ff0000;\">(emotion pd &#8220;It was scary to drive alone on the highway.&#8221;)<\/span><\/p>\n<p>((:EMOTION . &#8220;Fear&#8221;) (:*FEAR . 0.57426417) (:*SAD . 0.35364214) (:*HAPPY . 0.0052627796) (:*ANGRY . 0.044279967) (:*EXCITED . 0.018575214) (:*BORED . 0.003975709))<\/p><\/blockquote>\n<p>The result contains the dominant emotion followed by the individual emotions. For some reason, even though the company&#8217;s web site lists seven emotions, the returned result contains only six emotions (the emotion &#8220;<em><strong>sarcasm<\/strong><\/em>&#8221; is not included).<\/p>\n<p>As the above result shows, &#8220;<em><strong>Fear<\/strong><\/em>&#8221; (<strong>0.57<\/strong>) is the dominant emotion. I don&#8217;t understand why &#8220;<em><strong>Sad<\/strong><\/em>&#8221; (<strong>0.35<\/strong>) comes fairly close in the second place. If you recall IBM&#8217;s analysis for this example,<span class=\"Apple-converted-space\">\u00a0 <\/span>&#8220;<em><strong>Fear<\/strong><\/em>&#8221; was <strong>0.90<\/strong> and &#8220;<em><strong>Sadness<\/strong><\/em>&#8221; was just <strong>0.11<\/strong>. That appears reasonable.<\/p>\n<p>The actual result returned by the API contains status code as well; for completeness, I have included a method to dump the actual result of the API call.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<blockquote><p>CL-USER 3 &gt; <span style=\"color: #ff0000;\">(pprint (result pd))<\/span><\/p>\n<p>((:CODE . 200)<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0<\/span>(:EMOTION<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>(:PROBABILITIES<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0\u00a0 <\/span>(:*FEAR . 0.57426417)<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0\u00a0 <\/span>(:*SAD . 0.35364214)<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0\u00a0 <\/span>(:*HAPPY . 0.0052627796)<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0\u00a0 <\/span>(:*ANGRY . 0.044279967)<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0\u00a0 <\/span>(:*EXCITED . 0.018575214)<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0\u00a0 <\/span>(:*BORED . 0.003975709))<\/p>\n<p><span class=\"Apple-converted-space\">\u00a0 <\/span>(:EMOTION . &#8220;Fear&#8221;)))<\/p><\/blockquote>\n<p>As you can see, the earlier shown result extracts just the emotion details from this more detailed result.<\/p>\n<p>Let us move on to the next sentence:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">&#8220;I can&#8217;t wait to see the President in person!&#8221;<\/span><\/p><\/blockquote>\n<p>Here is what the system says:<\/p>\n<blockquote><p><span style=\"color: #000000;\">CL-USER 4 &gt; <span style=\"color: #ff0000;\">(emotion pd &#8220;I can&#8217;t wait to see the President in person!&#8221;)<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">((:EMOTION . &#8220;Excited&#8221;) (:*FEAR . 0.27241874) (:*SAD . 0.03887309) (:*HAPPY . 0.29172024) (:*ANGRY . 0.04447313) (:*EXCITED . 0.34099433) (:*BORED . 0.011520461))<\/span><\/p><\/blockquote>\n<p>&#8220;<em><strong>Excited<\/strong><\/em>&#8221; is the main emotion. Again, &#8220;<em><strong>Fear<\/strong><\/em>&#8221; comes close and I don&#8217;t understand why. Even in IBM&#8217;s case, I had pointed out that the &#8220;<em><strong>Joy<\/strong><\/em>&#8221; factor was just <strong>0.32, <\/strong>whereas it should\u00a0have been higher in my opinion.<\/p>\n<p>Now, the third sentence:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">&#8220;The talk was dull and uninteresting. The audience was literally yawning throughout the program.&#8221;<\/span><\/p><\/blockquote>\n<p>The analysis is:<\/p>\n<blockquote><p><span style=\"color: #000000;\">CL-USER 5 &gt; <span style=\"color: #ff0000;\">(emotion pd &#8220;The talk was dull and uninteresting. The audience was literally yawning throughout the program.&#8221;)<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\">((:EMOTION . &#8220;Bored&#8221;) (:*FEAR . 0.044667978) (:*SAD . 0.05334388) (:*HAPPY . 0.006943717) (:*ANGRY . 0.02137869) (:*EXCITED . 0.022493042) (:*BORED . 0.8511727))<\/span><\/p><\/blockquote>\n<p>Great. As expected, &#8220;<em><strong>Bored<\/strong><\/em>&#8221; (<strong>0.85<\/strong>) comes up on top. There is no disagreement here.<\/p>\n<p>Let us check out the next sentence:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">&#8220;Our whole family rejoiced when my son got the first prize.&#8221;<\/span><\/p><\/blockquote>\n<p>How does the API analyse this?<\/p>\n<blockquote><p>CL-USER 6 &gt; <span style=\"color: #ff0000;\">(emotion pd &#8220;Our whole family rejoiced when my son got the first prize.&#8221;)<\/span><\/p>\n<p>((:EMOTION . &#8220;Excited&#8221;) (:*FEAR . 0.121385224) (:*SAD . 0.068373315) (:*HAPPY . 0.31221092) (:*ANGRY . 0.08467515) (:*EXCITED . 0.38698116) (:*BORED . 0.026374208))<\/p><\/blockquote>\n<p>&#8220;<em><strong>Excited<\/strong><\/em>&#8221; (<strong>0.38<\/strong>) is the dominant emotion and &#8220;<em><strong>Happy<\/strong><\/em>&#8221; comes quite close (<strong>0.31<\/strong>). Perfect!<\/p>\n<p>Let us look at the last example:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">&#8220;I don&#8217;t understand why our politicians are so arrogant.&#8221;<\/span><\/p><\/blockquote>\n<p>Here is the analysis:<\/p>\n<blockquote><p>CL-USER 7 &gt; <span style=\"color: #ff0000;\">(emotion pd &#8220;I don&#8217;t understand why our politicians are so arrogant.&#8221;)<\/span><\/p>\n<p>((:EMOTION . &#8220;Angry&#8221;) (:*FEAR . 0.08077061) (:*SAD . 0.21222554) (:*HAPPY . 0.010213352) (:*ANGRY . 0.5890932) (:*EXCITED . 0.013984387) (:*BORED . 0.093712874))<\/p><\/blockquote>\n<p>&#8220;<em><strong>Angry<\/strong><\/em>&#8221; (<strong>0.58<\/strong>) predominates, with &#8220;<em><strong>Sad<\/strong><\/em>&#8221; coming at a distant second (<strong>0.21<\/strong>). This seems OK to me. In IBM&#8217;s case, we had &#8220;<em><strong>Disgust<\/strong><\/em>&#8221; (<strong>0.62<\/strong>) and &#8220;<em><strong>Anger<\/strong><\/em>&#8221; (<strong>0.38<\/strong>).<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Overall, <em><strong>ParallelDots<\/strong><\/em> does a decent job of identifying emotions from the given text. Although my focus in this article has been on &#8220;<em><strong>Emotions<\/strong><\/em>&#8220;, they have APIs to handle &#8220;<em><strong>Sentiment<\/strong><\/em>&#8220;, &#8220;<em><strong>Abuse<\/strong><\/em>&#8220;, &#8220;<em><strong>Intent<\/strong><\/em>&#8220;, etc. The company has a &#8220;<em><strong>Free<\/strong><\/em>&#8221; plan that includes <em><strong>1000<\/strong><\/em> hits per day, which is quite sufficient for getting started and for exploring their offering. Do give it a try.<\/p>\n<p>You can download my <em><strong>Lisp<\/strong><\/em> program from <a href=\"http:\/\/www.rangakrish.com\/downloads\/Paralleldots-Emotion-API.lisp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week, I showed how we can use IBM Natural Language Understanding API to identify emotions from given text. Today, I would like to run through the same examples, but using ParallelDots API service. There are wrappers\u00a0in Java, Python, Ruby, C#, and PHP for accessing the REST service. However, I chose to write my own [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18,107,17],"tags":[192,174],"class_list":["post-1475","post","type-post","status-publish","format-standard","hentry","category-lisp","category-natural-language-processing","category-programming","tag-emotion-detection","tag-natural-language-processing"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-nN","jetpack-related-posts":[{"id":1460,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/02\/17\/identifying-emotions-from-text\/","url_meta":{"origin":1475,"position":0},"title":"Identifying Emotions from Text","author":"admin","date":"February 17, 2019","format":false,"excerpt":"Identifying the predominant sentiment in unstructured text is used widely these days. There are several REST API services that allow you to submit a piece of text and get back the corresponding sentiment analysis. Meaningcloud, Aylien, Google's Cloud Natural Language API, and\u00a0 IBM Natural Language Understanding\u00a0Service are just a few.\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Program to Dump Emotions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/02\/Code.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1349,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/12\/30\/natural-language-interaction-with-ilexicon-using-luis\/","url_meta":{"origin":1475,"position":1},"title":"Natural Language Interaction with iLexicon Using LUIS","author":"admin","date":"December 30, 2018","format":false,"excerpt":"Some time ago, I had written a series of articles on my iLexicon project. It is a Lisp package that supports many interesting queries on English words. When I was discussing this project with a client recently, she asked me if it was possible to query the system in natural\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Talking to iLexicon","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Code2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Code2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Code2.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":856,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/02\/25\/sentiments-and-emotions-in-ilexicon\/","url_meta":{"origin":1475,"position":2},"title":"Sentiments and Emotions in iLexicon","author":"admin","date":"February 25, 2018","format":false,"excerpt":"Detecting sentiments and emotions in a piece of text are frequently performed activities in Text analysis. There are some API services available for this. For example, meaningcloud.com \u00a0has an API for detecting sentiments in the text submitted to it. Another API service provider is aylien.com . Emotion detection for text\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1285,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/12\/09\/parsing-text-with-meaningclouds-text-analytics-api\/","url_meta":{"origin":1475,"position":3},"title":"Parsing Text with MeaningCloud&#8217;s Text Analytics API","author":"admin","date":"December 9, 2018","format":false,"excerpt":"There is wide-spread interest in Natural Language Processing (NLP) today, and there are several API services available to cater to this demand. See this article for a fairly detailed list of services. All of them support multiple languages, including English. Today, I am going to share my experience in working\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Get Words Function","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Get-words.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Get-words.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Get-words.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":520,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/05\/07\/accessing-dictionary-rest-api-using-lisp\/","url_meta":{"origin":1475,"position":4},"title":"Accessing Dictionary API Using Lisp","author":"admin","date":"May 7, 2017","format":false,"excerpt":"A few days ago when I was searching for good online dictionaries, I stumbled upon Oxford Dictionary API for developers. I decided to check it out and registered for a free account. This allows me to make 3000 API calls in a month. Since I am not planning to use\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":534,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/05\/22\/definite-clause-grammars-dcg-in-lisp\/","url_meta":{"origin":1475,"position":5},"title":"Definite Clause Grammars (DCG) in Lisp","author":"admin","date":"May 22, 2017","format":false,"excerpt":"Definite Clause Grammars (DCG) are an elegant formalism for specifying context free grammars, and part of their popularity is due to their support in the Prolog language. Most books on Natural Language processing usually include a brief coverage of DCGs, even though Natural languages are not context-free. Because of the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"DCG Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=1475"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1475\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}