<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/new-package-tokenizers-joins-ropensci/"></div></div><div><a data-url="https://www.r-bloggers.com/new-package-tokenizers-joins-ropensci/" data-counturl="https://www.r-bloggers.com/new-package-tokenizers-joins-ropensci/" data-count="horizontal" data-text="New package tokenizers joins rOpenSci" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ropensci.org/blog/2016/08/23/tokenizers-joins-ropensci"> rOpenSci Blog - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The R package ecosystem for natural language processing has been flourishing in recent days. R packages for text analysis have usually been based on the classes provided by the <a target="_blank" href="https://cran.r-project.org/package=NLP/">NLP</a> or <a target="_blank" href="https://cran.r-project.org/package=tm/">tm</a> packages. Many of them depend on Java. But recently there have been a number of new packages for text analysis in R, most notably <a target="_blank" href="https://github.com/dselivanov/text2vec">text2vec</a>, <a target="_blank" href="https://github.com/kbenoit/quanteda">quanteda</a>, and <a target="_blank" href="https://github.com/juliasilge/tidytext">tidytext</a>. These packages are built on top of <a target="_blank" href="http://www.rcpp.org/">Rcpp</a> instead of <a target="_blank" href="https://cran.r-project.org/package=rJava/">rJava</a>, which makes them much more reliable and portable. And instead of the classes based on NLP, which I have never thought to be particularly idiomatic for R, they use standard R data structures. The text2vec and quanteda packages both rely on the sparse matrices provided by the rock solid <a target="_blank" href="https://cran.r-project.org/package=Matrix/">Matrix</a> package. The tidytext package is idiosyncratic (in the best possible way!) for doing all of its work in data frames rather than matrices, but a data frame is about as standard as you can get. For a long time when I would recommend R to people, I had to add the caveat that they should use Python if they were primarily interested in text analysis. But now I no longer feel the need to hedge.</p>
<p>Still there is a lot of duplicated effort between these packages on the one hand and a lot of incompatibilities between the packages on the other. The R ecosystem for text analysis is not exactly coherent or consistent at the moment.</p>
<p>My small contribution to the new text analysis ecosystem is the tokenizers package, which was recently accepted into rOpenSci after a careful <a target="_blank" href="https://github.com/ropensci/onboarding/issues/33">peer review</a> by <a target="_blank" href="https://kevinushey.github.io/">Kevin Ushey</a>. A new version of the package is <a target="_blank" href="https://cran.r-project.org/package=tokenizers/">on CRAN</a>. (Also check out the<br>
Jeroen Ooms's <a target="_blank" href="https://github.com/ropensci/hunspell">hunspell</a> package, which is a part of rOpensci.)</p>
<p>One of the basic tasks in any NLP pipeline is turning texts (which humans can read) into tokens (which machines can compute with). For example, you might break a text into words or into <a target="_blank" href="https://en.wikipedia.org/wiki/N-gram">n-grams</a>. Here is an example using the <a target="_blank" href="https://memory.loc.gov/ammem/snhtml/snhome.html">former slave interviews</a> from the Great Depression era Federal Writers' Project. (A data package with those interviews is in development <a target="_blank" href="https://github.com/lmullen/WPAnarratives">here</a>).</p>
<div>
<pre><code data-lang="text"># devtools::install_github(&quot;lmullen/WPAnarratives&quot;)
# install.packages(&quot;tokenizers&quot;)
library(WPAnarratives)
library(tokenizers)

text &lt;- head(wpa_narratives$text, 5)
class(text)

## [1] &quot;character&quot;

words &lt;- tokenize_words(text, lowercase = TRUE)
str(words)

## List of 5
##  $ : chr [1:1141] &quot;_he&quot; &quot;loved&quot; &quot;young&quot; &quot;marster&quot; ...
##  $ : chr [1:1034] &quot;_old&quot; &quot;joe&quot; &quot;can&quot; &quot;keep&quot; ...
##  $ : chr [1:824] &quot;_jesus&quot; &quot;has&quot; &quot;my&quot; &quot;chillun&quot; ...
##  $ : chr [1:779] &quot;charity&quot; &quot;anderson&quot; &quot;who&quot; &quot;believes&quot; ...
##  $ : chr [1:350] &quot;dat&quot; &quot;was&quot; &quot;one&quot; &quot;time&quot; ...

ngrams &lt;- tokenize_ngrams(text, n_min = 3, n = 5)
str(ngrams)

## List of 5
##  $ : chr [1:3414] &quot;_he loved young&quot; &quot;_he loved young marster&quot; &quot;_he loved young marster john_&quot; &quot;loved young marster&quot; ...
##  $ : chr [1:3093] &quot;_old joe can&quot; &quot;_old joe can keep&quot; &quot;_old joe can keep his&quot; &quot;joe can keep&quot; ...
##  $ : chr [1:2463] &quot;_jesus has my&quot; &quot;_jesus has my chillun&quot; &quot;_jesus has my chillun counted_&quot; &quot;has my chillun&quot; ...
##  $ : chr [1:2328] &quot;charity anderson who&quot; &quot;charity anderson who believes&quot; &quot;charity anderson who believes she&quot; &quot;anderson who believes&quot; ...
##  $ : chr [1:1041] &quot;dat was one&quot; &quot;dat was one time&quot; &quot;dat was one time when&quot; &quot;was one time&quot; ...
</code></pre>
</div>
<p>Practically all text analysis packages provide their own functions for tokenizing text, so why do R users need this package?</p>
<p>First, these tokenizers are reasonably fast. The basic string operations are handled by the <a target="_blank" href="https://cran.r-project.org/package=stringi/">stringi</a> package, which is quick while also doing the correct thing across encodings and locales. And <a target="_blank" href="http://dsnotes.com/">Dmitriy Selivanov</a> (author of the text2vec package) has written the n-gram and skip n-gram tokenizers in C++ so that those are fast too. It is probably possible to write tokenizers with better performance, but these are fast enough for even large scale text mining efforts.</p>
<p>The second and more important reason is that these tokenizers are consistent. They all take either a character vector of any length, or a list where each element is a character vector of length one. The idea is that each element of the input comprises a text. Then each function returns a list with the same length as the input vector, where each element in the list contains the tokens generated by the function. If the input character vector or list is named, then the names are preserved, so that the names can serve as identifiers.</p>
<p>And third, the tokenizers are reasonably comprehensive, including functions for characters, lines, words, word stems, sentences, paragraphs, n-grams, skip n-grams, and regular expressions.</p>
<p>My hope is that developers of other text analysis packages for R will rely on this package to provide tokenizers. (So far only tidytext has taken me up on that, but I also have to re-write my own <a target="_blank" href="https://github.com/ropensci/textreuse">textreuse</a> package now.) But even if natural language packages do not take the package as a formal dependency, most packages let you pass in your own tokenizing functions. So users can reap the benefits of a consistent set of tokenizers by using the functions in this package. The success of the &quot;<a target="_blank" href="https://twitter.com/hadleywickham/status/751805589425000450">tidyverse</a>&quot; has shown the power of buying into a convention for the structure of data and the inputs and outputs of functions. My hope is that the tokenizers package is a step in that direction for text analysis in R.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/new-package-tokenizers-joins-ropensci/"></div></div><div><a data-url="https://www.r-bloggers.com/new-package-tokenizers-joins-ropensci/" data-counturl="https://www.r-bloggers.com/new-package-tokenizers-joins-ropensci/" data-count="vertical" data-text="New package tokenizers joins rOpenSci" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ropensci.org/blog/2016/08/23/tokenizers-joins-ropensci"> rOpenSci Blog - R</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/zOVgsscyGHk"></a></div>