<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/text-mining-in-r-and-python-8-tips-to-get-started/"></div></div><div><a data-url="https://www.r-bloggers.com/text-mining-in-r-and-python-8-tips-to-get-started/" data-counturl="https://www.r-bloggers.com/text-mining-in-r-and-python-8-tips-to-get-started/" data-count="horizontal" data-text="Text Mining in R and Python: 8 Tips To Get Started" 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://www.datacamp.com/community/blog/text-mining-in-r-and-python-tips"> DataCamp Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>You want to get started on text mining, but most of the tutorials you start, get pretty complex very quickly? Or you can’t find a proper data set to work on?</em></p>
<p><em>DataCamp’s latest post will walk you through 8 tips and tricks that will help you to start text mining and to stay hooked on it.</em></p>
<div>
<h2>1. Get Curious About Text</h2>
<p>The first step to almost anything in data science is to get curious. Text mining is no exception to that.</p>
<p>You should get curious about text like David Robinson, data scientist at StackOverflow, described in <a target="_blank" href="http://varianceexplained.org/r/trump-tweets/">his blog</a> a couple of weeks ago, “I saw a hypothesis […] that simply begged to be investigated with data”.</p>
<p>(For those of you who are wondering what the hypothesis was, it was this:</p>

<blockquote data-lang="en" class="twitter-tweet">
<p dir="ltr" lang="en">Every non-hyperbolic tweet is from iPhone (his staff).<br>
Every hyperbolic tweet is from Android (from him). <a target="_blank" href="https://t.co/GWr6D8h5ed">pic.twitter.com/GWr6D8h5ed</a></p>
<p>— Todd Vaziri (@tvaziri) <a target="_blank" href="https://twitter.com/tvaziri/status/762005541388378112">August 6, 2016</a>
</p></blockquote>
<p></p>
<p>)</p>
<p>Or maybe, if you’re not really into verifying hypotheses, you should get curious about that cool word cloud you saw, realizing you want to reproduce it for yourself.</p>
<p>Do you still need to be convinced of how cool text mining can be?</p>
<p>Get inspired by one of the many text mining use cases that recently got a lot of attention in the media, like the text mining and analysis of <a target="_blank" href="http://kaylinwalker.com/text-mining-south-park/">South Park dialogue</a>, <a target="_blank" href="http://polygraph.cool/films/">film dialogue</a>, …</p>
</div>
<div>
<h2>2. Get The Skills and Knowledge You Need</h2>
<p>When you have gotten curious, it’s time to step up your game and start developing your knowledge and skills about text mining. You can easily do this by completing some tutorials and courses.</p>
<p>What you should look out for in these courses is that they introduce you to at least some of the steps that you find in a data science workflow, such as data preparation or preprocessing, data exploration, data analysis, …</p>
<p>DataCamp offers some material for those who are looking to get started with text mining: recently, Ted Kwartler wrote a <a target="_blank" href="https://www.datacamp.com/community/tutorials/recreate-bloomberg-terminal-news-trends-r">guest tutorial</a> on mining data from Google Trends and Yahoo’s stock service. This easy-to-follow R tutorial lets you learn text mining by doing and is a great start for any text mining starters.</p>
<p>In addition, Ted Kwartler is also the instructor of DataCamp’s R course <a target="_blank" href="https://www.datacamp.com/courses/intro-to-text-mining-bag-of-words">“Text Mining: Bag of Words”</a>, which will introduce you to a variety of essential topics for analyzing and visualizing data and lets you practice your acquired text mining skills on a real-world case study.</p>
<p>On the other hand, you also have some other material out there that is not necessarily limited to R. For Python, you could check out these tutorials and/or courses: for an introduction to text analysis in Python, you can go to <a target="_blank" href="http://nealcaren.web.unc.edu/an-introduction-to-text-analysis-with-python-part-1/">this tutorial</a>. Or you can also go through this <a target="_blank" href="https://www.kaggle.com/c/word2vec-nlp-tutorial/details/part-1-for-beginners-bag-of-words">introductory Kaggle tutorial</a>.</p>
<p>Are you, however, more interested in other resources? Go to DataCamp’s <a target="_blank" href="https://www.datacamp.com/community/tutorials/learn-data-science-resources-for-python-r">Learn Data Science – Resources for Python &amp; R</a> tutorial!</p>
</div>
<div>
<h2>3. Words, Words, Words – Finding Your Data</h2>
<p>Once you have gotten the hang out of the essential concepts and topics that you need to analyze and visualize your data, it is time to go and find the data!</p>
<p>And believe us when we tell you that there are a lot of ways to get your data. Besides the mention of Google Trends and Yahoo, you can also access data from:</p>
<ul>
<li>Twitter! Both R and Python offer packages or libraries that will allow you to connect to the Twitter API and retrieve tweets. You will learn more about this in the next section.</li>
<li>The <a target="_blank" href="http://archive.org/">Internet Archive</a>, a non-profit library of millions of free books, movies, software, music, websites, and more.</li>
<li>
<a target="_blank" href="http://www.gutenberg.org/">Project Gutenberg</a> offers over 55,000 free ebooks. Most of them are established literature and will thus be a good source if you want to do an analysis on the works of authors like Shakespeare, Jane Austen, Edgar Allan Poe.</li>
<li>For an academic approach to text mining, you can use the contents of <a target="_blank" href="http://about.jstor.org/service/data-for-research">JSTOR’s data for research</a>. It is a free, self-service tool that allows computer scientists, digital humanists, and other researchers to select and interact with content on JSTOR.</li>
<li>If you’re looking to do text mining on series or movies, just like in the examples that were given above, you might want to consider downloading the subtitles. A simple Google search can definitely provide you what you need to form your own corpus to get started on text mining.</li>
<li>You can also get your data from corpora. Two of the well-known corpora are:
<ul>
<li>The <a target="_blank" href="http://www.daviddlewis.com/resources/testcollections/reuters21578/">Reuters Text Corpus</a>. Some will argue that this is not the most diverse corpus to use, but it is excellent if you’re just starting to learn to do text mining.</li>
<li>The <a target="_blank" href="http://clu.uni.no/icame/brown/bcm.html">Brown Corpus</a> contains text from 500 sources, which are categorized by genre.</li>
</ul>
</li>
</ul>
<p>As you can see, the possibilities are endless. Everything that contains text can become the topic of your text mining case study.</p>
</div>
<div>
<h2>4. Finding The Right Tools For The Job</h2>
<p>Now that you have found the source of your data, you probably want to use the right tools to get them into your possession and to perform an analysis on it.</p>
<p>The tutorials and courses you will have followed will have given you some tools to get started.</p>
<p>But, depending on which courses or tutorials you have followed, you might miss a couple. To be complete, here’s a list of some of the packages that are used for text mining in R:</p>
<ul>
<li>One of the most used packages for text mining in R is, without a doubt, the <a target="_blank" href="https://www.rdocumentation.org/packages/tm/versions/0.6-2"><code>tm</code> package</a>. This package is often used in addition to more specific packages, like for example the twitteR package, which you can use to extract tweets and followers from the Twitter website.</li>
<li>To do web scraping with R, you should go for the <a target="_blank" href="https://www.rdocumentation.org/packages/rvest/versions/0.3.2"><code>rvest</code> library</a>. For a short tutorial on the use of <code>rvest</code>, go <a target="_blank" href="https://www.datacamp.com/community/tutorials/scraping-javascript-generated-data-with-r">here</a>.</li>
</ul>
<p>For Python, you can rely on these libraries:</p>
<ul>
<li>The <a target="_blank" href="https://pypi.python.org/pypi/textmining/1.0">text mining 1.0 package</a> contains a variety of useful functions for text mining in Python.</li>
<li>The natural language toolkit, contained within the <a target="_blank" href="http://www.nltk.org/"><code>nltk</code> package</a>. This package can be extremely useful because you have easy access to over 50 corpora and lexical resources. You can see a list of these on <a target="_blank" href="http://www.nltk.org/nltk_data/">this page</a>.</li>
<li>If you want to mine Twitter data, you have a lot of choices in packages. One of the packages that is used a lot is <a target="_blank" href="http://www.tweepy.org/"><code>Tweepy</code> package</a>.</li>
<li>For web scraping, the <a target="_blank" href="https://scrapy.org/"><code>scrapy</code> package</a> will come in handy to extract the data you need from websites. Also consider using <a target="_blank" href="https://docs.python.org/2/library/urllib2.html"><code>urllib2</code></a>, a package for opening URLs. Sometimes, however, the <code>requests</code> package is more recommended and it might even be more convenient to use. Some say that it is ‘more human’ or declarative because some things, like setting the user-agent and requesting a page is only one line of code. You will also sometimes see that some people refer to the <code>urllib</code> package, but this seems not all too popular: most developers refer to only one or two functions that they find particularly useful and use.</li>
</ul>
</div>
<div>
<h2>5. Preparation Is Half The Battle – Preprocessing Your Data</h2>
<p>It probably doesn’t come to you as a surprise when I tell you that data scientists spend 80% of their time cleaning their data.</p>
<p>Text mining is also no exception in this respect.</p>
<p>Textual data can be dirty, so you should make sure that you spend enough time to clean it.</p>
<p>If you’re unsure of what preprocessing your data means, some of the standard preprocessing steps include:</p>
<ul>
<li>Extracting text and structure so that you have the textual format you want to process,</li>
<li>Removing stopwords such as “that” or “and”,</li>
<li>Stemming, which you use to extract the root of words. This can be done with the help of a dictionary or with linguistic rules or algorithms such as Porter’s Algorithm.</li>
</ul>
<p>These steps seem hard, but preprocessing your data doesn’t need to be like that.</p>
<p>For the most part, the libraries and packages that were mentioned in the previous section can already help you a lot. For example, the <code>tm</code> library in R allows you to do some preprocessing with its built-in functions: you can do stemming and remove stop words, eliminate white spaces and convert the words to lowercase. Similarly, the <a target="_blank" href="http://www.nltk.org/"><code>nltk</code> package</a> in Python allows you to do much of the preprocessing because of the built-in functions.</p>
<p>However, you can still go a step further and also do some preprocessing based on regular expressions to describe the character patterns which interest you. This way, you will also speed up the process of data cleaning a bit.</p>
<p>For Python, you can make use of the <a target="_blank" href="https://docs.python.org/3/library/re.html"><code>re</code> library</a> and for R, there are a bunch of functions that can help you out, such as <code>grep()</code>, <code>grepl()</code>, <code>regexpr()</code>, <code>gregexpr()</code>, <code>sub()</code>, <code>gsub()</code>, and <code>strsplit()</code>.</p>
<p>If you want to know more about these functions and regular expressions in R, you can always check out <a target="_blank" href="https://www.rdocumentation.org/packages/base/versions/3.3.1/topics/regex">this page</a>.</p>
</div>
<div>
<h2>6. Data Scientist’s Adventures in Wonderland – Exploring Your Data</h2>
<p>By now, you will be excited to get started on your analysis. It is, however, always a good idea to get a look at your data before you start your analysis.</p>
<p>Some ideas to quickly get started on exploring your data with the help of the base packages or the libraries that have been mentioned above:</p>
<ul>
<li>Create a document term matrix: elements in this matrix represent the occurrence of a term (a word or an n-gram) in a document of the corpus.</li>
<li>After you have made the document term matrix, you can use a histogram to visualize the frequency of the words in your corpus.</li>
<li>You might also be interested in knowing the correlation between two or more terms in your corpus.</li>
<li>To visualize your corpus, you can also make a word cloud. In R, you can make use of the <a target="_blank" href="https://www.rdocumentation.org/packages/wordcloud/versions/2.5">wordcloud</a> library. A Python package with the same name also exists if you want to do the same in Python.</li>
</ul>
<p>The nice thing about exploring your data before diving into your analysis is that you already have an idea what you’ll be working with. If you see in the document term matrix or the histogram that you have a lot of sparse words, you can decide to remove them from your corpus.</p>
</div>
<div>
<h2>7. Level Up Your Text Mining Skills</h2>
<p>When you have preprocessed and have done a basic textual analysis of your data with the tools that have been mentioned in the previous step, you might also consider using your data set to broaden your text mining skills.</p>
<p>Because there is so much more.</p>
<p>You have only seen the tip of the iceberg when it comes to text mining.</p>
<p>Firstly, you should consider exploring the difference between text mining and Natural Language Processing (NLP). More NLP libraries in R can be found on <a target="_blank" href="https://www.rdocumentation.org/taskviews#NaturalLanguageProcessing">this page</a>. With NLP, you will discover Named Entity Recognition, POS tagging and parsers, sentiment analysis, … For Python, you can make use of the <a target="_blank" href="http://www.nltk.org/"><code>nltk</code> package</a>. You can find a a full tutorial on sentiment analysis with the <code>nltk</code> package <a target="_blank" href="http://streamhacker.com/2010/05/10/text-classification-sentiment-analysis-naive-bayes-classifier/">here</a>.</p>
<p>Besides these packages, you can check out more tools to get started on topics such as deep learning and statistical topic detection modeling (such as Latent Dirichlet Allocation or LDA), among the many others that exist. Some of the packages that you can use to approach these topics are listed below:</p>
<ul>
<li>
<p>Python packages: the Python packages <code>gensim</code> to implement word2vec, among others, and <a target="_blank" href="http://nlp.stanford.edu/projects/glove/">GloVe</a>. Also, <a target="_blank" href="http://deeplearning.net/software/theano/"><code>theano</code></a> should probably also be on your list if you want to discover deep learning further. Lastly, use <a target="_blank" href="http://radimrehurek.com/gensim/">gensim</a> if you want to implement LDA.</p>
</li>
<li>
<p>R packages: for an approach on text mining with deep learning in R, use <a target="_blank" href="https://www.rdocumentation.org/packages/text2vec/versions/0.3.0">text2vec</a>. If, however, you’re more interested in getting into sentiment analysis, the <code>syuzhet</code> library in combination with the <code>tm</code> library is probably the way to go. Finally, the <a target="_blank" href="https://www.rdocumentation.org/packages/topicmodels/versions/0.2-4">topicmodels</a> library for R is ideal for statistical topic detection modeling.</p>
</li>
</ul>
<p>And these packages are not nearly all that exists.</p>
<p>Since text mining is a pretty hot topic, there has been a lot to discover these past years in terms of research and you can expect that it will keep on being important in the years to come with multimedia mining, multilingual text mining, …</p>
</div>
<div>
<h2>8. More Than Words – Visualizing Your Results</h2>
<p>Don’t forget to communicate the results of your analysis! This is probably one of the most wonderful things that you can do, since visual representations attract people.</p>
<p>Your visualizations are your story.</p>
<p>So don’t hold back to visualize the correlations or topics you have found in your analysis.</p>
<p>For both Python and R you have specific packages that will help you to do this. You should therefore complete your list of the packages that you will have to use with these specific data visualization libraries to present your results:</p>
<p>For Python, you might consider using the <a target="_blank" href="https://networkx.github.io/"><code>NetworkX</code></a> package to visualize complex networks. However, the <a target="_blank" href="https://pypi.python.org/pypi/matplotlib/1.5.3"><code>matplotlib</code> package</a> can also come in handy for other types of visualizations. Also the <a target="_blank" href="https://pypi.python.org/pypi/plotly"><code>plotly</code> package</a>, which allows you to make interactive, publication-quality graphs online is one of go-to packages for presenting your results visually.</p>
<p>A <strong>tip</strong> for all of those who are huge fans of data visualization: try linking Python and D3, the JavaScript library for dynamic data manipulation and visualization that allows your audience to become active participants in data visualization process.</p>
<p>For R, besides the libraries that you will already know, such as <a target="_blank" href="https://www.rdocumentation.org/packages/ggplot2/versions/2.1.0"><code>ggplot2</code></a>, which is always a good idea to use, you can also use the <a target="_blank" href="https://www.rdocumentation.org/packages/igraph/versions/1.0.1"><code>igraph</code></a> library to analyze following or followed and retweeting relationships. Do you want even more? Consider checking out <a target="_blank" href="https://www.rdocumentation.org/packages/plotly/versions/4.5.2"><code>plotly</code></a> and <a target="_blank" href="https://www.rdocumentation.org/packages/networkD3/versions/0.2.13"><code>networkD3</code></a> to link R and JavaScript.</p>
</div>
<div>
<h2>Starting Your Text Mining Journey With DataCamp</h2>
<p>Do you want to get started on text mining in R? Head over to the <a target="_blank" href="https://www.datacamp.com/courses/intro-to-text-mining-bag-of-words">“Text Mining: Bag of Words”</a> course and learn text mining by doing it! This course is the best resource to quickly get started on a Text Mining case study to get introduced to a variety of essential topics for analyzing and visualizing data and to practice your acquired text mining skills on a real-world case study. Don’t you have the time to get started on the course yet? Try out Ted Kwartler’s interactive R tutorial. It’s the best resource to get started on a text mining case study quickly.</p>
<p>In short, go ahead and get started on you text mining journey with DataCamp!</p>
</div>
<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/text-mining-in-r-and-python-8-tips-to-get-started/"></div></div><div><a data-url="https://www.r-bloggers.com/text-mining-in-r-and-python-8-tips-to-get-started/" data-counturl="https://www.r-bloggers.com/text-mining-in-r-and-python-8-tips-to-get-started/" data-count="vertical" data-text="Text Mining in R and Python: 8 Tips To Get Started" 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://www.datacamp.com/community/blog/text-mining-in-r-and-python-tips"> DataCamp Blog</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/26mRzm7f4Sc"></a></div>