{"word_count": 502, "character_count": 4749}
Another popular application of classification techniques is on texmining (see e.g. an old post on French president speaches). Consider the following example,  inspired by Nobert Ryciak's post, with 12 wikipedia pages, on various topics,&#62; library(tm)&#62; library(stringi)&#62; library(proxy)&#62; titles = c("Boosting_(machine_learning)",+            "Random_forest",+            "K-nearest_neighbors_algorithm",+            "Logistic_regression",+            "Boston_Bruins",+            "Los_Angeles_Lakers",+            "Game_of_Thrones",+            "House_of_Cards_(U.S._TV_series)",+            "True Detective (TV series)",+            "Picasso",+            "Henri_Matisse",+            "Jackson_Pollock")&#62; articles = character(length(titles))&#62; wiki = "http://en.wikipedia.org/wiki/"&#62; for (i in 1:length(titles)) {+   articles[i] = stri_flatten(readLines(stri_paste(wiki, titles[i])), col = " ")+ }Here, we store all the contents of the pages in a corpus (from the text mining package).&#62; docs = Corpus(VectorSource(articles))This is what we have in that corpus&#62; a = stri_flatten(readLines(stri_paste(wiki, titles[1])), col = " ")&#62; a = Corpus(VectorSource(a))&#62; a[[1]]Thoughts on Hypothesis Boosting&#60;/i&#62;&#60;/a&#62;, Unpublished manuscript (Machine Learning class project, December 1988)&#60;/span&#62;&#60;/li&#62; &#60;li id="cite_note-4"&#62;&#60;span class="mw-cite-backlink"&#62;&#60;b&#62;&#60;a href="#cite_ref-4"&#62;^&#60;/a&#62;&#60;/b&#62;&#60;/span&#62; &#60;span class="reference-text"&#62;&#60;cite class="citation journal"&#62;&#60;a href="/wiki/Michael_Kearns" title="Michael Kearns"&#62;Michael Kearns&#60;/a&#62;; &#60;a href="/wiki/Leslie_Valiant" title="Leslie Valiant"&#62;Leslie Valiant&#60;/a&#62; (1989). &#60;a rel="nofollow" class="external text" href="http://dl.acm.org/citation.cfm?id=73049"&#62;"Crytographic limitations on learning Boolean formulae and finite automata"&#60;/a&#62;. &#60;i&#62;Symposium on TThis is because we read an html page.&#62; a = tm_map(a, function(x) &#62; a = tm_map(a, function(x) stri_replace_all_fixed(x, "\t", " "))&#62; a = tm_map(a, PlainTextDocument)&#62; a = tm_map(a, stripWhitespace)&#62; a = tm_map(a, removeWords, stopwords("english"))&#62; a = tm_map(a, removePunctuation)&#62; a = tm_map(a, tolower)&#62; a can  set  weak learners create  single strong learner  a weak learner  defined    classifier    slightly correlated   true classification  can label examples better  random guessing in contrast  strong learner   classifier   arbitrarily wellcorrelated   true classification robert Now we have the text of the wikipedia document. What we did wasreplace all “” elements with a space. We do it because there are not a part of text document but in general a html code.replace all “/t” with a space.convert previous result (returned type was “string”) to “PlainTextDocument”, so that we can apply the other functions from tm package, which require this type of argument.remove extra whitespaces from the documents.remove punctuation marks.remove from the documents words which we find redundant for text mining (e.g. pronouns, conjunctions). We set this words as stopwords(“english”) which is a built-in list for English language (this argument is passed to the function removeWords.transform characters to lower case.Now we can do it on the entire corpus&#62; docs2 = tm_map(docs, function(x) stri_replace_all_regex(x, "&#60;.+?&#62;", " "))&#62; docs3 = tm_map(docs2, function(x) stri_replace_all_fixed(x, "\t", " "))&#62; docs4 = tm_map(docs3, PlainTextDocument)&#62; docs5 = tm_map(docs4, stripWhitespace)&#62; docs6 = tm_map(docs5, removeWords, stopwords("english"))&#62; docs7 = tm_map(docs6, removePunctuation)&#62; docs8 = tm_map(docs7, tolower)Now, we simply count words in each page,&#62; dtm &#60;- DocumentTermMatrix(docs8)&#62; dtm2 &#60;- as.matrix(dtm)&#62; dim(dtm2)[1] 12 13683&#62; frequency &#60;- colSums(dtm2)&#62; frequency &#60;- sort(frequency, decreasing=TRUE)&#62; mots=frequency[frequency&#62;20]&#62; s=dtm2[1,which(colnames(dtm2) %in% names(mots))]&#62; for(i in 2:nrow(dtm2)) s=cbind(s,dtm2[i,which(colnames(dtm2) %in% names(mots))])&#62; colnames(s)=titles&#160;Once we have that dataset, we can use a PCA to visualise the 'variables' i.e. the pages&#62; library(FactoMineR)&#62; PCA(s)We can also use non-supervised classification to group pages. But first, let us normalize the dataset&#62; s0=s/apply(s,1,sd)Then, we can run a cluster dendrogram, using the Ward distance&#62; h &#60;- hclust(dist(t(s0)), method = "ward")&#62; plot(h, labels = titles, sub = "")Groups are consistent with intuition: painters are in the same cluster, as well as TV series, sports teams, and statistical techniques.