0% found this document useful (0 votes)
3 views2 pages

R Code

The document outlines a process for text mining in R, including creating a text vector, installing necessary packages, and generating a Document Term Matrix. It demonstrates how to analyze word frequency and visualize the results using a word cloud and bar chart. Additionally, it includes sentiment analysis on a new set of text data, categorizing sentiments as Positive, Negative, or Neutral.

Uploaded by

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

R Code

The document outlines a process for text mining in R, including creating a text vector, installing necessary packages, and generating a Document Term Matrix. It demonstrates how to analyze word frequency and visualize the results using a word cloud and bar chart. Additionally, it includes sentiment analysis on a new set of text data, categorizing sentiments as Positive, Negative, or Neutral.

Uploaded by

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

# Step 1: Create text vector

text <- c("R is easy",


"R is useful",
"Text mining is easy",
"Data is useful")

# Step 2: Install and load packages


[Link]("tm")
[Link]("wordcloud")

library(tm)
library(wordcloud)

# Step 3: Create corpus


corpus <- Corpus(VectorSource(text))

# Step 4: Convert text to lowercase


corpus <- tm_map(corpus, content_transformer(tolower))

# Step 5: Create Document Term Matrix


dtm <- DocumentTermMatrix(corpus)

# Step 6: Convert DTM to matrix


m <- [Link](dtm)

# Step 7: Find word frequency


word_freq <- colSums(m)

# Step 8: Display frequency table


word_freq

# Step 9: Create word cloud


wordcloud(names(word_freq), freq = word_freq, [Link] = 1)

# Step 10: Draw bar chart


barplot(word_freq,
main = "Word Frequency",
xlab = "Words",
ylab = "Frequency")

text <- c("I love this product",


"This service is very bad",
"The quality is excellent",
"I hate the delivery",
"The item is good")

[Link]("syuzhet")
library(syuzhet)

score <- get_sentiment(text)

category <- ifelse(score > 0, "Positive",


ifelse(score < 0, "Negative", "Neutral"))
[Link](Text = text, Score = score, Category = category)

You might also like