<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/using-purrr-with-apis-revamping-my-code/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://itsalocke.com/using-purrr-with-apis/"> 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>I wrote a little while back about <a rel="nofollow" href="http://itsalocke.com/microsoft-cognitive-services-text-analytics-api/" target="_blank">using Microsoft Cognitive Services APIs with R</a> to first of all detect the language of pieces of text and then do sentiment analysis on them. I wasn’t too happy with the some of the code as it was very inelegant. I knew I could code better than I had, especially as I’ve been doing a lot more work with purrr recently. However, it had sat in drafts for a while. Then David Smith kindly posted about the <a rel="nofollow" href="http://blog.revolutionanalytics.com/2017/06/interfacing-with-apis.html" target="_blank">process I used</a> which meant I had to get this nicer version of my code out ASAP!</p>
<p>Get the complete code in this <a rel="nofollow" href="https://gist.github.com/stephlocke/441ea493be926b76bac59452a3f57281" target="_blank">gist</a>.</p>
<h2>Prerequisites</h2>
<ul>
<li>API access
<ul>
<li><a rel="nofollow" href="https://www.microsoft.com/cognitive-services/en-us/sign-up" target="_blank">A key</a></li>
<li><a rel="nofollow" href="https://www.microsoft.com/cognitive-services/en-us/text-analytics/documentation" target="_blank">An endpoint</a></li>
</ul>
</li>
<li>Packages
<ul>
<li><a rel="nofollow" href="https://cran.r-project.org/package=httr" target="_blank">httr</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/package=jsonlite" target="_blank">jsonlite</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/package=dplyr" target="_blank">dplyr</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/package=purrr" target="_blank">purrr</a></li>
</ul>
</li>
</ul>
<h2>Setup</h2>
<pre><code>library(httr)
library(jsonlite)
library(dplyr)
library(purrr)

cogapikey&lt;-&quot;XXX&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;)

# Put data in an object that converts to the expected schema for the API
data_frame(text) %>% 
  mutate(id=row_number()) ->
  textdf

textdf %>% 
  list(documents=.) ->
  mydata
</code></pre>
<h2>Language detection</h2>
<p>We need to identify the most likely language for each bit of text in order to send this additional bit of info to the sentiment analysis API to be able to get decent results from the sentiment analysis.</p>
<pre><code>cogapi&lt;-&quot;https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/languages?numberOfLanguagesToDetect=1&quot;

cogapi %>% 
  POST(add_headers(`Ocp-Apim-Subscription-Key`=cogapikey),
       body=toJSON(mydata)) ->
  response

# Process response
response %>% 
  content() %>%
  flatten_df() %>% 
  select(detectedLanguages) %>% 
  flatten_df()->
  respframe

textdf %>% 
  mutate(language= respframe$iso6391Name) ->
  textdf
</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 = textdf)

# New endpoint
cogapi&lt;-&quot;https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment&quot;

# Construct a request
cogapi %>% 
  POST(add_headers(`Ocp-Apim-Subscription-Key`=cogapikey),
       body=toJSON(mydata)) ->
  response

# Process response
response %>% 
  content() %>%
  flatten_df() %>% 
  mutate(id=as.numeric(id))-> 
  respframe

# Combine
textdf %>%
  left_join(respframe) ->
  textdf
</code></pre>
<p>And… et voila! A multi-language dataset with the language identified and the sentiment scored using purrr for easier to read code.</p>
<p>Using purrr with APIs makes code nicer and more elegant as it really helps interact with hierarchies from JSON objects. I feel much better about this code now!</p>
<h2>Original</h2>
<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>
<h2>Revised</h2>
<table>
<thead>
<tr>
<th align="left">text</th>
<th align="right">id</th>
<th align="left">language</th>
<th align="right">score</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">is this english?</td>
<td align="right">1</td>
<td align="left">en</td>
<td align="right">0.2265771</td>
</tr>
<tr>
<td align="left">tak er der mere kage</td>
<td align="right">2</td>
<td align="left">da</td>
<td align="right">0.7455934</td>
</tr>
<tr>
<td align="left">merci beaucoup</td>
<td align="right">3</td>
<td align="left">fr</td>
<td align="right">0.8121097</td>
</tr>
<tr>
<td align="left">guten morgen</td>
<td align="right">4</td>
<td align="left">de</td>
<td align="right">0.8581840</td>
</tr>
<tr>
<td align="left">bonjour</td>
<td align="right">5</td>
<td align="left">fr</td>
<td align="right">0.8118965</td>
</tr>
<tr>
<td align="left">merde</td>
<td align="right">6</td>
<td align="left">fr</td>
<td align="right">0.0515683</td>
</tr>
<tr>
<td align="left">That’s terrible</td>
<td align="right">7</td>
<td align="left">en</td>
<td align="right">0.0068665</td>
</tr>
<tr>
<td align="left">R is awesome</td>
<td align="right">8</td>
<td align="left">en</td>
<td align="right">0.9973871</td>
</tr>
</tbody>
</table>
<p>Interestingly the scores for English have not stayed the same – for instance, Microsoft now sees “R is awesome” in a much more positive light. It’s also great to see German and Danish are now supported!</p>
<p>Get the complete code in this <a rel="nofollow" href="https://gist.github.com/stephlocke/441ea493be926b76bac59452a3f57281" target="_blank">gist</a>.</p>
<p>The post <a rel="nofollow" href="http://itsalocke.com/using-purrr-with-apis/" target="_blank">Using purrr with APIs – revamping my code</a> appeared first on <a rel="nofollow" href="http://itsalocke.com/" target="_blank">Locke Data</a>. Locke Data are a data science consultancy aimed at helping organisations get ready and get started with data science.</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/using-purrr-with-apis/"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...

	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/dtrkRMtMo68" height="1"></a></div>