-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.Rmd
326 lines (199 loc) · 10.4 KB
/
part2.Rmd
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
---
title: "Text Mining"
author: "Hamza Saadi"
date: "September 21, 2018"
output: html_document
---
We need to relaod the libraries and the data:
```{r message=FALSE,warning=FALSE,cache =TRUE}
library(tidyverse) #data manipulation and visualization
library(textmineR)
library(wordcloud)
library(SentimentAnalysis)
DF <- read.csv("CleanedArticles.csv", # Read CSV file with "Latin1" Encoding
encoding = "latin1", # also we specified the class of each Column
colClasses = c("character","Date",rep("character",3)),# using attr colClasses
stringsAsFactors=F) # and set StringsAsFactors to False
glimpse(DF) # take a glimpse to see
```
Before starting let's how many articles do we have over time.
```{r message=FALSE,warning=FALSE,cache =TRUE}
DF <- DF %>%
mutate(Year = as.integer(format(Publication.Date,"%Y")), #Creat new Variable Year
n_char = nchar(Full.Text)) # Creat new variabl character length
ggplot(DF, aes(Year))+
geom_bar(aes(fill = Type), position = position_stack(reverse = TRUE)) +
coord_flip() +
theme(legend.position = "top")+
xlab( "Years" ) +
ylab( "Articles Frequency" )
```
As we can see that Lkeria start make classification for articles only after year 2014.
What about the lentgh of documents?
```{r message=FALSE,warning=FALSE,cache =TRUE}
ggplot(DF, aes(x=n_char))+
geom_density(color="darkblue", fill="lightblue") +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 90, hjust = 1))+
scale_x_continuous(breaks = round(seq(0, max(DF$n_char), by = 1000),1))
```
Not let's focuse on more important things, Documents clustering.
Since we're going to study only subject "Housing", so we're going to select only articles of types:
* `AADL` & `LPA` (both have almost same characteristics)
* `LPP`
```{r message=FALSE,warning=FALSE,cache =TRUE}
AADL_LPA <- DF %>%
filter(Type %in% c("aadl","lpa"))%>% # Select AADL, LPA TYPE
select(Title,Publication.Date,Writer,Type,Full.Text) # Select the wanted columns
LPP <- DF %>%
filter(Type == "lpp")%>% # Select AADL, LPP TYPE
select(Title,Publication.Date,Writer,Full.Text) # Select the wanted columns
glimpse(AADL_LPA)
```
Before starting clustering and messing up things, let's see the definition.
* `Clustering` :
`Hierarchical Clustering` involves creating clusters that have a predetermined ordering from top to bottom. For example, all files and folders on the hard disk are organized in a hierarchy. There are two types of hierarchical clustering, `Divisive` and `Agglomerative`.
![](Clustering_h1.png)
* `Divisive` method :
In divisive or top-down clustering method we assign all of the observations to a single cluster and then partition the cluster to two least similar clusters. Finally, we proceed recursively on each cluster until there is one cluster for each observation. There is evidence that divisive algorithms produce more accurate hierarchies than agglomerative algorithms in some circumstances but is conceptually more complex.
* `Agglomerative` method:
In `Agglomerative` or bottom-up clustering method we assign each observation to its own cluster. Then, compute the similarity (e.g., distance) between each of the clusters and join the two most similar clusters. Finally, repeat steps 2 and 3 until there is only a single cluster left. The related algorithm is shown below.
* `Tf-idf` stands for term frequency-inverse document frequency, and the tf-idf weight is a weight often used in information retrieval and text mining. This weight is a statistical measure used to evaluate how important a word is to a document in a collection or corpus. The importance increases proportionally to the number of times a word appears in the document but is offset by the frequency of the word in the corpus. Variations of the `tf-idf` weighting scheme are often used by search engines as a central tool in scoring and ranking a document's relevance given a user query. more [here](http://www.tfidf.com/).
+ `TF`: Term Frequency: which measures how frequently a term occurs in a document. Since every document is different in length, it is possible that a term would appear much more times in long documents than shorter ones. Thus, the term frequency is often divided by the document length (aka. the total number of terms in the document) as a way of normalization:
TF(t) = (Number of times term t appears in a document) / (Total number of terms in the document).
+ `IDF`: Inverse Document Frequency, which measures how important a term is. While computing TF, all terms are considered equally important. However it is known that certain terms, such as "is", "of", and "that", may appear a lot of times but have little importance. Thus we need to weigh down the frequent terms while scale up the rare ones, by computing the following:
IDF(t) = log_e(Total number of documents / Number of documents with term t in it).
```{r message=FALSE,warning=FALSE,cache =TRUE}
AADL_LPA$Title[AADL_LPA$Title==""] <- NA # replace one character string with NA for AADL_LPA
AADL_LPA <- AADL_LPA[!is.na(AADL_LPA$Title),] # remove those lines
LPP$Full.Text[LPP$Full.Text==""] <- NA # replace one character string with NA for LPP
LPP <- LPP[!is.na(LPP$Full.Text),] # remove those line
dtm <- CreateDtm(doc_vec = (AADL_LPA$Full.Text), # character vector of documents
doc_names = AADL_LPA$Title, # document names
ngram_window = c(1, 2), # minimum and maximum n-gram length
stopword_vec = c(tm::stopwords("french"), # stopwords from tm
tm::stopwords("SMART")), # this is the default value
lower = TRUE, # lowercase - this is the default value
remove_punctuation = TRUE, # punctuation - this is the default
remove_numbers = TRUE, # numbers - this is the default
verbose = FALSE)
# construct the matrix of term counts to get the IDF vector
tf_mat <- TermDocFreq(dtm)
# TF-IDF and cosine similarity
tfidf <- t(dtm[ , tf_mat$term ]) * tf_mat$idf
tfidf <- t(tfidf) # Transpose
csim <- tfidf / sqrt(rowSums(tfidf * tfidf)) # cosine similarity
csim <- csim %*% t(csim)
cdist <- as.dist(1 - csim) # Clalculate distances
hc <- hclust(cdist, method = "ward.D")
clustering <- cutree(hc, 10)
png("cluster_total.png",width = 4096,height = 2160)
plot(hc, main = "Hierarchical clustering of AADL & LPA Articles",
ylab = "", xlab = "", yaxt = "n")
rect.hclust(hc, 10, border = "red")
dev.off()
```
Now Let's see what clustering gives us:
![](cluster_total.png)
```{r message=FALSE,warning=FALSE,cache =TRUE}
p_words <- colSums(dtm) / sum(dtm)
cluster_words <- lapply(unique(clustering), function(x){
rows <- dtm[ clustering == x , ]
# for memory's sake, drop all words that don't appear in the cluster
rows <- rows[ , colSums(rows) > 0 ]
colSums(rows) / sum(rows) - p_words[ colnames(rows) ]
})
# create a summary table of the top 5 words defining each cluster
cluster_summary <- data.frame(cluster = unique(clustering),
size = as.numeric(table(clustering)),
top_words = sapply(cluster_words, function(d){
paste(
names(d)[ order(d, decreasing = TRUE) ][ 1:5 ],
collapse = ", ")
}),
stringsAsFactors = FALSE)
cluster_summary
```
```{r cache=TRUE}
dd <- data.frame(clustering)
dd$titles <- row.names(dd)
row.names(dd) <- NULL
names(dd) <- c("Cluster","title")
```
# {.tabset}
## Cluster 1
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==1,]$title)
```
## Cluster 2
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==2,]$title)
```
## Cluster 3
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==3,]$title)
```
## Cluster 4
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==4,]$title)
```
## Cluster 5
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==5,]$title)
```
## Cluster 6
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==6,]$title)
```
## Cluster 7
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==7,]$title)
```
## Cluster 8
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==8,]$title)
```
## Cluster 9
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==9,]$title)
```
## Cluster 10
```{r cache=TRUE,echo=FALSE}
head(dd[dd$Cluster==10,]$title)
```
#
So after reading the top 5 titles of each cluster, we see that we will keep only clusters: 3, 6, 7, 10.
```{r message=FALSE,warning=FALSE,cache =TRUE}
AADL_LPA$cluster <- dd$Cluster
Selected <- AADL_LPA[AADL_LPA$cluster %in% c(3,6,7,10),]
to_remove <- paste(paste0(tm::stopwords(kind="fr"),collapse = " | "),
"[[:punct:]]",
"da",
"dinars",
"\\d+",
sep="|")
Selected <- Selected[Selected$Full.Text != "",]
Selected$Full.Text<- Selected$Full.Text %>%
tolower()%>%
str_replace_all(to_remove, " ")
costum_sentiment <- function(x){
analyzeSentiment(x,
language="french",
rules=list("SentimentLM"=list(ruleSentiment, loadDictionaryGI())))
}
Selected$Sentiment <- Selected$Full.Text %>%
sapply(FUN= costum_sentiment,USE.NAMES = F)%>%
unlist(use.names = F)
glimpse(Selected)
```
```{r message=FALSE,warning=FALSE,cache =TRUE}
Selected %>%
select(Publication.Date,Title,Sentiment)%>%
head()
```
```{r message=FALSE,warning=FALSE,cache =TRUE}
ggplot(data = Selected,aes(x = Publication.Date,y = Sentiment,color=Type))+ # Plot1 for AADL_LPA
geom_point()+
geom_smooth(method='lm', formula = y ~ x + I(x^4),se = F)
```