<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/text-featurization-with-the-microsoft-ml-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2017/08/text-featurization-microsoftml.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>Last week I wrote about how you can use the <a rel="nofollow" href="http://blog.revolutionanalytics.com/2017/08/image-featurizer.html" target="_blank">MicrosoftML package in Microsoft R to featurize images</a>: reduce an image to a vector of 4096 numbers that quantify the essential characteristics of the image, according to an AI vision model. You can perform a similar featurization process with text as well, but in this case you have a lot more control of the features used to represent the text.</p>
<p>Tsuyoshi Matsuzaki demonstrates the process in a <a rel="nofollow" href="https://blogs.msdn.microsoft.com/tsmatsuz/2017/03/18/microsoftml-text-analysis-featurizetext/" target="_blank">post at the MSDN Blog</a>. The post explores the <a rel="nofollow" href="http://www.cs.jhu.edu/~mdredze/datasets/sentiment/" target="_blank">Multi-Domain Sentiment Dataset</a>, a collection of product reviews from Amazon.com. The dataset includes reviews from 975,194 products on Amazon.com from a variety of domains, and for each product there is a text review and a star rating of 1, 2, 4, or 5. (There are no 3-star rated reviews in the data set.) Here's one example, selected at random:</p>
<blockquote>
<p>What a useful reference! I bought this book hoping to brush up on my French after a few years of absence, and found it to be indispensable. It's great for quickly looking up grammatical rules and structures as well as vocabulary-building using the helpful vocabulary lists throughout the book. My personal favorite feature of this text is Part V, Idiomatic Usage. This section contains extensive lists of idioms, grouped by their root nouns or verbs. Memorizing one or two of these a day will do wonders for your confidence in French. This book is highly recommended either as a standalone text, or, preferably, as a supplement to a more traditional textbook. In either case, it will serve you well in your continuing education in the French language.</p>
</blockquote>
<p>The review contains many positive terms (&quot;useful&quot;, &quot;indespensable&quot;, &quot;highly recommended&quot;), and in fact is associated with a 5-star rating for this book. The goal of the blog post was to find the terms most associated with positive (or negative) reviews. One way to do this is to use the <code><a rel="nofollow" href="https://docs.microsoft.com/en-us/r-server/r-reference/microsoftml/featurizetext" target="_blank">featurizeText</a></code> function in thje Microsoft ML package included with <a rel="nofollow" href="https://docs.microsoft.com/en-us/r-server/" target="_blank">Microsoft R Client and Microsoft R Server</a>. Among other things, this function can be used to extract ngrams (sequences of one, two, or more words) from arbitrary text. In this example, we extract all of the one and two-word sequences represented at least 500 times in the reviews. Then, to assess which have the most impact on ratings, we use their presence or absence as predictors in a linear model:</p>
<div>
<pre>transformRule <span>=</span> list(
  featurizeText(
    vars <span>=</span> c(Features <span>=</span> <span>&quot;REVIEW_TEXT&quot;</span>),
    <span># ngramLength=2: include not only &quot;Azure&quot;, &quot;AD&quot;, but also &quot;Azure AD&quot;</span>
    <span># skipLength=1 : &quot;computer&quot; and &quot;compuuter&quot; is the same</span>
    wordFeatureExtractor <span>=</span> ngramCount(
      weighting <span>=</span> <span>&quot;tfidf&quot;</span>,
      ngramLength <span>=</span> <span>2</span>,
      skipLength <span>=</span> <span>1</span>),
    language <span>=</span> <span>&quot;English&quot;</span>
  ),
  selectFeatures(
    vars <span>=</span> c(<span>&quot;Features&quot;</span>),
    mode <span>=</span> minCount(<span>500</span>)
  )
)

<span># train using transforms !</span>
model <span>&lt;-</span> rxFastLinear(
  RATING <span>~</span> Features,
  data <span>=</span> train,
  mlTransforms <span>=</span> transformRule,
  type <span>=</span> <span>&quot;regression&quot;</span> <span># not binary (numeric regression)</span>
)
</pre>
</div>
<p>We can then look at the coefficients associated with these features (presence of n-grams) to assess their impact on the overall rating. By this standard, the top 10 words or word-pairs contributing to a negative rating are:</p>
<pre>boring       -7.647399
waste        -7.537471
not          -6.355953
nothing      -6.149342
money        -5.386262
bad          -5.377981
no           -5.210301
worst        -5.051558
poorly       -4.962763
disappointed -4.890280
</pre>
<p>Similarly, the top 10 words or word-pairs associated with a positive rating are:</p>
<pre>will      3.073104
the|best  3.265797
love      3.290348
life      3.562267
wonderful 3.652950
,|and     3.762862
you       3.889580
excellent 3.902497
my        4.454115
great     4.552569
</pre>
<p>Another option is simply to look at the sentiment score for each review, which can be extracted using the <code><a rel="nofollow" href="https://docs.microsoft.com/en-us/r-server/r-reference/microsoftml/getsentiment" target="_blank">getSentiment</a></code> function. </p>
<div>
<pre>sentimentScores <span>&lt;-</span> rxFeaturize(data<span>=</span>data, 
                    mlTransforms <span>=</span> getSentiment(vars <span>=</span> 
                                     list(SentimentScore <span>=</span> <span>&quot;REVIEW_TEXT&quot;</span>)))
</pre>
</div>
<p>As we expect, a negative seniment (in the 0-0.5 range) is associated with 1- and 2-star reviews, while a positive sentiment (0.5-1.0) is associated with the 4- and 5-star reviews.</p>
<p><a rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2a5df00970c-pi" target="_blank"><img border="0" alt="Sentiment-boxplots" title="Sentiment-boxplots" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2a5df00970c-800wi"></a></p>
<p>You can find more details on this analysis, including the Microsoft R code, at the link below.</p>
<p>Microsoft Technologies Blog for Enterprise Developers: <a rel="nofollow" href="https://blogs.msdn.microsoft.com/tsmatsuz/2017/03/18/microsoftml-text-analysis-featurizetext/" target="_blank">Analyze your text in R (MicrosoftML)</a></p>
</div>
	<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://blog.revolutionanalytics.com/2017/08/text-featurization-microsoftml.html"> Revolutions</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/hXvhR8X7aiE" height="1"></a></div>