-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreproNYTime-2018-12-18b.Rmd
337 lines (245 loc) · 9.98 KB
/
PreproNYTime-2018-12-18b.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
327
328
329
330
331
332
333
334
---
title: "PreproNYTime2006"
author: "Jean-Francois Chartier"
date: "6 décembre 2018"
output:
html_document:
number_sections: true
toc: true
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE, cache.lazy = FALSE)
```
#Get Data
##get documents
```{r}
library(xml2)
library(magrittr)
nyXML=xml2::read_xml("dataForTest.xml", encoding="ANSI")
#nyXML=xml2::read_xml("nytCorpus2003-2005LemmatiseAntidictionnaireFiltreToutSaufNomAdjectifVerbeUTF8_formatJFC.xml", encoding="ANSI")
#xml_name(nyXML)
all.docs.nodes=xml_find_all(nyXML, ".//document")
```
##get authors
all unique authors
id rows are used as id authors
```{r}
#important de concat author names
#
authorFrequency=xml2::xml_find_all(all.docs.nodes, ".//auteur")%>%xml_text(.)%>%table(.)%>%as.data.frame(., stringsAsFactors=F)%>% set_colnames(., c("author.name", "frequency"))
authorFrequency$idAuthor=1:length(authorFrequency$author.name)
#uniqueAuthors=authorFrequency$author.name
#old script
#uniqueAuthors=xml2::xml_find_all(all.docs.nodes, ".//auteur")%>%xml_text(.) %>%unique(.)
saveRDS(authorFrequency, "authorFrequency.rds")
```
##get id authors by document
```{r}
#replace author's name by id
allIdAuthorsByDoc=sapply(all.docs.nodes, FUN = function(x){
#x=all.docs.nodes[[12]]
authors_i= xml2::xml_find_all(x, ".//auteur")
#print(length(authors_i))
if (length(authors_i)==0){
idsAuthorsOfDoc=list()
} else if(length(authors_i)>1){
#print(" here1")
idsAuthorsOfDoc=vector(mode = "list", length = length(authors_i))
for (i in 1:length(authors_i)){
idToSlect_i=authorFrequency$author.name==xml_text(authors_i[i])
idsAuthorsOfDoc[i]=authorFrequency$idAuthor[idToSlect_i]
#old call
#idsAuthorsOfDoc[i]=authorFrequency$author.name==xml_text(authors_i[i])%>%subset(authorFrequency$idAuthor, subset = .)
}
} else {
#print("here2")
#print(xml_text(authors_i))
idsAuthorsOfDoc=vector(mode = "list", length = length(authors_i))
#old call
#idsAuthorsOfDoc[1]=list(which(uniqueAuthors==xml_text(authors_i)))
idToSlect=authorFrequency$author.name==xml_text(authors_i)
idsAuthorsOfDoc[1]=list(authorFrequency$idAuthor[idToSlect])
}
})
```
##get info from documents
```{r}
allSeg=lapply(all.docs.nodes, FUN = function(x){
seg_i = x %>% xml2::xml_find_first(., ".//segment")%>%xml_text(.)
})%>%unlist(.)
allDate=lapply(all.docs.nodes, FUN = function(x){
date_i= x %>% xml2::xml_find_first(., ".//date")%>%xml_integer(.)
})%>%unlist(.)
df.docs=data.frame(segment = allSeg, date = allDate, stringsAsFactors = F)
df.docs$idAuthors=allIdAuthorsByDoc
```
#Preprocessing data
##cleaning corpus
```{r}
library(stringr)
# tokenisation selon quanteda
preprocesCorpus=stringr::str_replace_all(df.docs$segment,"[\r\n]" , "")
#remove all non graphical caracther
preprocesCorpus=stringr::str_replace_all(preprocesCorpus,"[^[:graph:]]", " ")
#remove whitespace
preprocesCorpus=stringr::str_squish(preprocesCorpus)
```
##Tokinization and word filtering
```{r}
library(quanteda)
preprocesCorpus=quanteda::tokens(x=preprocesCorpus,what="word", remove_punct = TRUE, remove_numbers = TRUE, remove_separators = TRUE,remove_hyphens = TRUE, remove_symbols=TRUE, remove_url = TRUE)
preprocesCorpus=quanteda::tokens_tolower(preprocesCorpus)
#myStopWords=unique(c(stopwords("en", source = "smart"), c("yes", "no", "thing", "can", "okay", "ok", "just", "good", "like", "something", "one", "moment", "say", "go", "speeches", "pages", "online", "default.aspx", "www.bankofengland.co.uk")))
myStopWords=unique(c(stopwords("en", source = "smart")))
# filtrer selon un antidictionnaire et singleton
preprocesCorpus=quanteda::tokens_remove(preprocesCorpus, case_insensitive = F, valuetype = "glob", pattern=myStopWords, min_nchar=3)
#lemmatization
#no need to lemmatize, the corpus was already lemmatized
#preprocesCorpus=sapply(preprocesCorpus, FUN = function(seg) paste0(textstem::lemmatize_words(seg), collapse = " "))
#preprocesCorpus=quanteda::tokens(preprocesCorpus)
print(c("corpus size after preprocessing : " , length(paste(unlist(preprocesCorpus)))))
print(c("vocabulary size after preprocessing : ", length(unique(paste(unlist(preprocesCorpus))) )))
df.docs$tokens=preprocesCorpus
```
#Modeling
##vectorization of documents
```{r ,cache=T}
#Vectorize documents
myNyTimeMatrix = quanteda::dfm(x=df.docs$tokens, tolower=FALSE)
#set filter
minDocFreq = 2
maxDocFreq = length(myNyTimeMatrix)*.66
#filter to rare and to frequent words and ngrams
myNyTimeMatrix<-quanteda::dfm_trim(x=myNyTimeMatrix, min_docfreq = minDocFreq, max_docfreq = maxDocFreq, docfreq_type="count")
# imprimer nombre de dimensions de la matrice
print(paste("nombre de mots differents apres filtrage base sur la frequence documentaire : ", length(myNyTimeMatrix@Dimnames$features)))
saveRDS(myNyTimeMatrix, "myNyTimeTrainningMatrix.rds")
```
##filter empty documents
```{r}
nonEmptyVectors = apply(X = as.matrix(myNyTimeMatrix), MARGIN = 1, FUN = function(x) sqrt(sum(x^2))>0)
myNyTimeMatrix=myNyTimeMatrix[nonEmptyVectors]
df.docs=df.docs[nonEmptyVectors, ]
```
##LDA based topic modeling of documents
```{r}
library(topicmodels)
burnin <- 100
iter <- 1000
thin <- 500
seed <-123 # keep fixed, to garantee reproductibility
nstart <- 1
best <- TRUE
k<-200 # number of topics
#convert quanteda format to tm format
dtm_matrix = quanteda::as.DocumentTermMatrix(myNyTimeMatrix)
#launch topic modeling
topics<-topicmodels::LDA(dtm_matrix, k, method="Gibbs", control=list(nstart=nstart, seed = seed, best=best, burnin = burnin, iter = iter, thin=thin))
```
##save topic model
```{r}
saveRDS(topics, "trainTopicModelNyTimes.rds")
```
#Journalists' semantic preferences
##filter out document wihout author
```{r}
numberAuthors=sapply(df.docs$idAuthors, FUN = function(x) length(x))
df.docs=df.docs[numberAuthors>0,]
```
##aggregate to journalist*word matrix
```{r}
docAuthorAdjencency = quanteda::dfm(x=df.docs$idAuthors %>% as.tokens(.), tolower=FALSE)
authorWordTFMatrix=matrix(nrow = ncol(docAuthorAdjencency), ncol = ncol(myNyTimeMatrix))
#rownames are id authors
# important here to select from docAuthorAdjencency, because after filtering empty segment, we have authors in the corpus who are never instanciated in the matrix
rownames(authorWordTFMatrix)=colnames(docAuthorAdjencency)
for (j in 1: ncol(docAuthorAdjencency)){
idDocs=which((docAuthorAdjencency)[,j]%>%as.matrix()>0)
myNyTimeMatrix[idDocs,] %>% colSums(.)->authorWordTFMatrix[j,]
}
```
##Topic a posteriori of journalists
```{r}
posteriorForAuthors=topicmodels::posterior(topics, newdata = authorWordTFMatrix)
saveRDS(posteriorForAuthors, "posteriorTopicsForAuthors.rds")
```
#Compute relation between journalist
## get author cooccurrence matrix
the number of time 2 authors co-wrote an article
```{r}
authorAuthorMatrix=(t(docAuthorAdjencency)) %*% docAuthorAdjencency
#to retrive relation between 2 authors you need to used @dimnames
#which(authorAuthorMatrix@Dimnames[[1]]==1)
authorCoSignature=as.matrix(authorAuthorMatrix)%>%reshape2::melt(., value.name="co.writting.frequency")
```
##semantic similarity relation between authors
```{r}
library(proxy)
authorByAuthorSemSim=proxy::simil(authorWordTFMatrix, by_rows = T, method = "cosine")%>%as.matrix(.)%>%reshape2::melt(., value.name="semantic.similarity")
```
## social structural equivalence
```{r}
authorByAuthorSocialEqui=proxy::simil(as.matrix(authorAuthorMatrix), by_rows = T, method = "cosine")%>%as.matrix(.)%>%reshape2::melt(., value.name="structural.equivalence")
```
##Build igraph structure
```{r}
library(igraph)
#library(network)
#library(intergraph) # used to encode object from one igraph to network
graphOFAuthors= igraph::graph_from_adjacency_matrix(authorAuthorMatrix, mode = "undirected", diag = F)
```
## get social proximity relation
define social proximity betwwen 2 nodes as the shortest path divided by the graph diameter
```{r}
distBetweenAuthors=distances(graph = graphOFAuthors)
#because the minimal possible distance is 1, we obtain a normalized proximity by 1/geodesic.length
socialProxAuthor=1/distBetweenAuthors
#replace the diagonal with 0,
socialProxAuthor[is.infinite(socialProxAuthor)]=0
socialProxAuthor=socialProxAuthor%>%reshape2::melt(., value.name="social.proximity")
#plot(graphOFAuthors)
```
##get assortativity
not sure if relevant. The hypothesis here is that agents with similair degree (>0) in a network should influence eauch other more than people with different positions
```{r}
```
##save relation dataframe
```{r}
colnames(socialProxAuthor)=c("idAuthor.1", "idAuthor.2", names(socialProxAuthor)[3])
relationsBetweenAuthors=cbind(socialProxAuthor, structural.equivalence=authorByAuthorSocialEqui$structural.equivalence, semantic.similarity=authorByAuthorSemSim$semantic.similarity, co.writting.frequency=authorCoSignature$co.writting.frequency)
saveRDS(relationsBetweenAuthors, "relationsBetweenAuthors.rds")
```
#Compute centralities
##Degree centrality
```{r}
#degreeCentrality=authorAuthorMatrix%>%rowMeans()
#names(degreeCentrality)=authorAuthorMatrix@Dimnames[[1]]
#use igraph function for consistency
degreeCentrality=degree(graphOFAuthors, mode = "all", normalized = T)
```
##Closeness centrality
as the mean of social proximities
```{r}
# noes not work because the graph is disconnected. Use instead rowmeans from proximity matrix
#closeCentrality=closeness(graphOFAuthors, mode = "all")
#be sure that socialProxAuthor has no infinite values
proxA=(1/distBetweenAuthors)
proxA[is.infinite(proxA)]=0
closeCentrality=rowMeans(proxA)
```
## betweenness centrality
```{r}
betweennessCentrality=betweenness(graphOFAuthors, directed = F, normalized = T)
```
##save centrality df
```{r}
n=nrow(authorAuthorMatrix)
centralityAuthors= data.frame(idAuthor=names(degreeCentrality), degree.centrality=degreeCentrality, closeness.centrality=closeCentrality, betweenness.centrality=betweennessCentrality)
saveRDS(centralityAuthors, "centralityAuthors.rds")
```
#Save Corpus Info Data Frame
```{r}
saveRDS(df.docs, "nyTimeCorpusInfoDataFrame-2018-12-10.rds")
```