0% found this document useful (0 votes)
4 views3 pages

Text Mining Code

This document outlines the process of text mining in R, including the installation of necessary packages and loading data as a corpus. It details the steps for cleaning the text, building a term-document matrix, and generating a word cloud. Additionally, it includes instructions for saving the processed data to a CSV file and exploring frequent terms.

Uploaded by

shamst2321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Text Mining Code

This document outlines the process of text mining in R, including the installation of necessary packages and loading data as a corpus. It details the steps for cleaning the text, building a term-document matrix, and generating a word cloud. Additionally, it includes instructions for saving the processed data to a CSV file and exploring frequent terms.

Uploaded by

shamst2321
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# Install and load the required packages

# for text mining


[Link]("tm")
# for text stemming
[Link]("SnowballC")
# for word-cloud generator
[Link]("wordcloud")
# for colour palettes
[Link]("RColorBrewer")

# Add to the Library


library("tm")
library("SnowballC")
library("wordcloud")
library("RColorBrewer")

#Load the data as a corpus

docs <- Corpus(VectorSource(Text))

#Build a term-document matrix

Term <- TermDocumentMatrix(docs)


Matrix <- [Link](Term)
Sort <- sort(rowSums(Matrix),decreasing=TRUE)
Key <- [Link](word = names(Sort),freq=Sort)
Key
head(Key, 10)
#Cleaning the text

# Convert the text to lower case


docs <- tm_map(docs, content_transformer(tolower))
# Remove numbers
docs <- tm_map(docs, removeNumbers)
# Remove english common stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))
# Remove your own stop word # specify your stopwords as a character vector
docs <- tm_map(docs, removeWords, c("blabla1", "blabla2"))
# Remove punctuations
docs <- tm_map(docs, removePunctuation)

# Build a term-document matrix

Term <- TermDocumentMatrix(docs)


Matrix <- [Link](Term)
Sort <- sort(rowSums(Matrix),decreasing=TRUE)
Key <- [Link](word = names(Sort),freq=Sort)
Key
head(Key, 10)

#How to save data

[Link](Key,"[Link]", [Link] = FALSE)

#Generate the Word cloud

[Link](1234)
wordcloud(words = Key$word, freq = Key$freq, [Link] = 1,

[Link]=200, [Link]=FALSE, [Link]=0.35,

colors=[Link](8, "Dark2"))

#Explore frequent terms

findFreqTerms(Term, lowfreq = 4)

You might also like