<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-quick-tip-microsoft-cognitive-services-text-analytics-api/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://itsalocke.com/microsoft-cognitive-services-text-analytics-api/"> R – Locke Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div></div>
<p>Today in class, I taught some fundamentals of API consumption in R. As it was aligned to some Microsoft content, we first used <a target="_blank" rel="nofollow" href="https://haveibeenpwned.com/">HaveIBeenPwned.com</a>‘s API and then played with <a target="_blank" rel="nofollow" href="https://www.microsoft.com/cognitive-services/en-us/">Microsoft Cognitive Services</a>‘ Text Analytics API. This brief post overviews what you need to get started, and how you can chain consecutive calls to these APIs in order to perform multi-lingual sentiment analysis.<br>
<span></span></p>
<h2>Getting started</h2>
<p>To use the analytics API, you need two things:</p>
<ul>
<li>A key</li>
<li>A URL</li>
</ul>
<p>Get the key by <a target="_blank" rel="nofollow" href="https://www.microsoft.com/cognitive-services/en-us/sign-up">signing up for free</a> on the Cognitive Services site for the Text Analytics API. Make sure to verify your email address!</p>
<p>The URL can be retrieved from the <a target="_blank" rel="nofollow" href="https://www.microsoft.com/cognitive-services/en-us/text-analytics/documentation">API documentation</a> and you can even play in an <a target="_blank" rel="nofollow" href="https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0">online sandbox</a> before you start implementing it.</p>
<p>To use the API in R, you need:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=httr"><code>httr</code></a> </li>
<li><a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=jsonlite"><code>jsonlite</code></a>, which <code>httr</code> will kindly install for you</li>
</ul>
<p>To make some things easier on myself, I’m also going to use <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=dplyr"><code>dplyr</code></a> and <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=data.table"><code>data.table</code></a>.</p>
<pre><code>library(httr)
library(jsonlite)
library(data.table)
library(dplyr)
</code></pre>
<h2>Starting info</h2>
<p>To talk to the API, we need our URL, our API, and some data.</p>
<pre><code># Setup
cogapikey&lt;-&quot;XXX&quot;
cogapi&lt;-&quot;https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/languages&quot;

text=c(&quot;is this english?&quot;
       ,&quot;tak er der mere kage&quot;
       ,&quot;merci beaucoup&quot;
       ,&quot;guten morgen&quot;
       ,&quot;bonjour&quot;
       ,&quot;merde&quot;
       ,&quot;That's terrible&quot;
       ,&quot;R is awesome&quot;)

# Prep data
df&lt;-data_frame(id=1:8,text)
mydata&lt;-list(documents= df)
</code></pre>
<h2>Topic detection</h2>
<p>We have some different languages and we need to first do language detection before we can analyse the sentiment of our phrases</p>
<pre><code># Construct a request
response&lt;-POST(cogapi, 
               add_headers(`Ocp-Apim-Subscription-Key`=cogapikey),
               body=toJSON(mydata))
</code></pre>
<p>Now we need to consume our response such that we can add the language code to our existing data.frame. The structure of the response JSON doesn’t play well with others so I use data.table’s nifty <code>rbindlist</code>. It is a **very good* candidate for <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=purrr"><code>purrr</code></a> but I’m not up to speed on that yet.</p>
<pre><code># Process response
respcontent&lt;-content(response, as=&quot;text&quot;)
respdf&lt;-data_frame(
    id=as.numeric(fromJSON(respcontent)$documents$id), 
    iso6391Name=rbindlist(
      fromJSON(respcontent)$documents$detectedLanguages
    )$iso6391Name
  )
</code></pre>
<p>Now that we have a table, we can join the two together</p>
<pre><code># Combine
df%>%
  inner_join(respdf) %>%
  select(id, language=iso6391Name, text) ->
  dft
</code></pre>
<h2>Sentiment analysis</h2>
<p>With an ID, text, and a language code, we can now request the sentiment of our text be analysed.</p>
<pre><code># New info
mydata&lt;-list(documents= dft)
cogapi&lt;-&quot;https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment&quot;
# Construct a request
response&lt;-POST(cogapi, 
               add_headers(`Ocp-Apim-Subscription-Key`=cogapikey),
               body=toJSON(mydata))
</code></pre>
<p>Processing this response is simpler than processing the language response</p>
<pre><code># Process reponse
respcontent&lt;-content(response, as=&quot;text&quot;)

fromJSON(respcontent)$documents %>%
   mutate(id=as.numeric(id)) ->
   responses

# Combine
dft %>%
  left_join(responses) ->
  dfts
</code></pre>
<p>And… et voila! A multi-language dataset with the language identified and the sentiment scored where the language can be scored. <img alt="😀" class="wp-smiley" src="https://i1.wp.com/s.w.org/images/core/emoji/2.2.1/72x72/1f600.png?w=456&ssl=1" data-recalc-dims="1"></p>
<table>
<thead>
<tr>
<th align="right">id</th>
<th align="left">language</th>
<th align="left">text</th>
<th align="right">score</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">1</td>
<td align="left">en</td>
<td align="left">is this english?</td>
<td align="right">0.2852910</td>
</tr>
<tr>
<td align="right">2</td>
<td align="left">da</td>
<td align="left">tak er der mere kage</td>
<td align="right">NA</td>
</tr>
<tr>
<td align="right">3</td>
<td align="left">fr</td>
<td align="left">merci beaucoup</td>
<td align="right">0.8121097</td>
</tr>
<tr>
<td align="right">4</td>
<td align="left">de</td>
<td align="left">guten morgen</td>
<td align="right">NA</td>
</tr>
<tr>
<td align="right">5</td>
<td align="left">fr</td>
<td align="left">bonjour</td>
<td align="right">0.8118965</td>
</tr>
<tr>
<td align="right">6</td>
<td align="left">fr</td>
<td align="left">merde</td>
<td align="right">0.0515683</td>
</tr>
<tr>
<td align="right">7</td>
<td align="left">en</td>
<td align="left">That’s terrible</td>
<td align="right">0.1738841</td>
</tr>
<tr>
<td align="right">8</td>
<td align="left">en</td>
<td align="left">R is awesome</td>
<td align="right">0.9546152</td>
</tr>
</tbody>
</table>
<div class="twitter-share"><a target="_blank" class="twitter-share-button" rel="nofollow" href="https://twitter.com/intent/tweet?url=http%3A%2F%2Fitsalocke.com%2Fmicrosoft-cognitive-services-text-analytics-api%2F&via=stefflocke&%23038;related=stefflocke%3ASteph">Tweet</a></div>
<p>The post <a target="_blank" rel="nofollow" href="http://itsalocke.com/microsoft-cognitive-services-text-analytics-api/">R Quick tip: Microsoft Cognitive Services’ Text Analytics API</a> appeared first on <a target="_blank" rel="nofollow" href="http://itsalocke.com/">Locke Data</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://itsalocke.com/microsoft-cognitive-services-text-analytics-api/"> R – Locke Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/KF8eySbgpMQ"></a></div>