forked from gregkinman-b48856/sentiment_mining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_miner.R
151 lines (115 loc) · 4.27 KB
/
text_miner.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"
Greg Kinman - B48856
Freescale Semiconductor, Inc.
text_miner.R
Mines web-scraped text.
"
library(tm)
library(igraph)
main <- function() {
"
Driver function for text processing.
"
ne <- processor("value_1.csv")
nu <- processor("value_2.csv")
po <- processor("value_3.csv")
elicitor(ne, "Negative")
elicitor(nu, "Neutral")
elicitor(po, "Positive")
}
processor <- function(csv_filename) {
"
Stems and removes stopwords from the text of the first column of a .csv file.
Input:
1. csv_filename: character: the name of the .csv file that contains the data
Output:
1. final_data: data.frame: a data frame containing the processed text data and its metadata
"
# Loads the file data into a data frame.
data <- read.csv(csv_filename)
# Pulls just the text column (the leftmost column) into a corpus.
text <- data[,1]
text_corpus <- Corpus(DataframeSource(data.frame(text)))
# Cleans up the text.
text_corpus <- tm_map(text_corpus, tolower)
text_corpus <- tm_map(text_corpus, removePunctuation)
text_corpus <- tm_map(text_corpus, removeNumbers)
removeURL <- function(x) gsub("http:[[:alnum:]]*", "", x)
text_corpus <- tm_map(text_corpus, removeURL)
my_stopwords <- c(stopwords("english"), "can", "will", "just", "much", "use")
text_corpus <- tm_map(text_corpus, removeWords, my_stopwords)
# Stems and re-completes the text.
text_corpus_copy <- text_corpus
text_corpus <- tm_map(text_corpus, stemDocument)
text_corpus <- tm_map(text_corpus, stemCompletion, dictionary=text_corpus_copy)
# Replaces the leftmost column in the original data frame with the processed text.
metadata <- data[,-1]
Snippets <- t(as.data.frame(text_corpus))
final_data <- data.frame(Snippets, metadata)
return(final_data)
}
elicitor <- function(text_data, sentiment) {
"
Finds things out about the text data.
Input:
1. text_data: data.frame: the text data
2. sentiment: character: the sentiment of the dataset given
"
text_snippets <- text_data$Snippets
text_corpus <- Corpus(DataframeSource(data.frame(text_snippets)))
text_tdm <- TermDocumentMatrix(text_corpus)
# Finds most frequent terms.
for (freq in seq(50, 5, -5)) {
frequent_terms <- findFreqTerms(text_tdm, lowfreq = freq)
if (length(frequent_terms) > 9) {
this_freq <- freq
break
}
}
# Removes search query words, since we want related words only.
no_words <- c("freescale", "i.mx", "imx", "kinetis", "qoriq", "riotboard", "riot", "board")
related_terms <- vector(mode = "character", length=0)
for (word_1 in frequent_terms) {
allowed = TRUE
for (word_2 in no_words) {
if (word_1 == word_2) {
allowed = FALSE
}
}
if (allowed == TRUE) {
related_terms <- c(related_terms, word_1)
}
}
print(sentiment)
print(":")
print(related_terms)
# Bar plots of most frequent terms:
frequencies <- rowSums(as.matrix(text_tdm))
frequencies <- subset(frequencies, frequencies>=this_freq)
barplot(frequencies, las=2, horiz=TRUE)
# Finds word associations for the product families.
print(findAssocs(text_tdm, "freescale", 0.4))
print(findAssocs(text_tdm, "kinetis", 0.4))
print(findAssocs(text_tdm, "riotboard", 0.4))
print(findAssocs(text_tdm, "imx", 0.4))
# Clustering:
tdm_2 <- removeSparseTerms(text_tdm, sparse=0.95)
mat <- as.matrix(tdm_2)
dist_mat <- dist(scale(mat))
fit <- hclust(dist_mat, method="ward.D")
plot(fit)
rect.hclust(fit, k=8)
groups <- cutree(fit, k=8)
# Network analysis (need to figure out how to make this a graph only on a subset of the nodes):
#text_tdm <- as.matrix(text_tdm)
#text_tdm[text_tdm>=1] <- 1
#text_tdm <- as.matrix(text_tdm) %*% as.matrix(t(text_tdm))
#g <- graph.adjacency(text_tdm, weighted=TRUE, mode="undirected")
#g <- simplify(g)
#V(g)$label <- V(g)$name
#V(g)$degree <- degree(g)
#set.seed(3952)
#layout1 <- layout.fruchterman.reingold(g)
#plot(g, layout=layout1)
}
main()