-
Notifications
You must be signed in to change notification settings - Fork 2
/
Processing facebook posts
214 lines (174 loc) · 5.97 KB
/
Processing facebook posts
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
### Workflow for processing Facebook posts extracted with getPage() function###
### The code was developed as part of the Ancient Identities in Modern Britain (IARH) project
### Part 1 imports the Facebook posts saved in multiple files and puts them in the list.
### Part 2 extracts only those posts that have relevant keywords and identifies which language they are written in.
### Part 3 obtains the summary of which keywords appear in the posts and how frequently and which languages the posts are written in
### Part 4 separates posts by language
### Part 5 verifies whether posts containing keywords are actually relevant
### Project: Ancient Identities in Modern Britain (IARH); ancientidentities.org
### Author: Marta Krzyzanska
# Set working directory
setwd("~path")
# Require "textcat" and "data.table" packages. If you have not installed them already, run install.packages() first.
library("textcat")
library("data.table")
#Require functions:
#searchPosts() - code available here: https://github.com/IARHeritages/Heritage-of-Brexit/blob/master/searchPosts()
#requestVerification() and verifyText() - code available here: https://github.com/IARHeritages/Heritage-of-Brexit/blob/master/verifyText()%20(and%20requestVerification())
#Requires defining or importing keywords - code for importing keywords available here:
#https://github.com/IARHeritages/Heritage-of-Brexit/blob/master/Importing%20and%20redefining%20keywords%20to%20search%20facebook%20posts
####Part 1: Load all the extracted posts and save them in the list, save the list put together.
#In this case we had 74 extracted files
listPosts<-c()
i=1
while (i<75){
load(paste("my_posts(",i,").R",sep=""))
listPosts[[i]] <- my_posts
i=i+1 }
#Save the list of lists of posts as one file
save(listPosts,file="listPosts.R")
######Part 2: Extract only the data with the relevant keywords and detect the language of the posts
i=1
j=length(listPosts)+1
while(i<j){
print (paste("Searching keywords in list",i,"out of",j,sep=" "))
k=1
l=length(listPosts[[i]])+1
while(k<l){
if(length(listPosts[[i]][[k]])>0){
print (paste("page",k,sep=" "))
listPosts[[i]][[k]]$keywords <-NA
m=1
n=length(listPosts[[i]][[k]]$message)+1
while(m<n){
listPosts[[i]][[k]]$keywords[m] <- searchPost(listPosts[[i]][[k]]$message[m],postKeywords)
m=m+1
}
listPosts[[i]][[k]] <- subset(listPosts[[i]][[k]],listPosts[[i]][[k]]$keywords!="")
listPosts[[i]][[k]]$language<-textcat(listPosts[[i]][[k]]$message)
k=k+1
}
else{
print (paste("page",k,sep=" "))
k=k+1
}
}
i=i+1
}
####Part3: Get the relevant statistics
##1: Merge all the dataframes into one table (only for statistics purposes):
listPostsTable <- rbindlist(listPosts[[1]])
i=2
j= length(listPosts)+1
while(i<j){
lpi <- rbindlist(listPosts[[i]])
listPostsTable <- rbind(listPostsTable,lpi)
i=i+1}
### Get all the unique languages and all the unique entries for the keywords
languages <- as.data.frame(table(listPostsTable$language))
languages <- languages[with(languages, order(-Freq)), ]
uniqueKeywords <- as.data.frame(table(listPostsTable$keyword))
uniqueKeywords <- uniqueKeywords[with(uniqueKeywords, order(-Freq)), ]
####Part 4 option 1: extract only english posts or separate posts by language
listEnglishPosts <- listPosts
i=1
j=length(listPosts)+1
while(i<j){
print (paste("Subsetting posts in list",i,"out of",(j-1),sep=" "))
k=1
l=length(listPosts[[i]])+1
while(k<l){
if(length(listPosts[[i]][[k]])>0){
print (paste("page",k,sep=" "))
listEnglishPosts[[i]][[k]] <- subset(listEnglishPosts[[i]][[k]],listEnglishPosts[[i]][[k]]$language=="english")
k=k+1
}
else{
print (paste("page",k,sep=" "))
k=k+1
}
}
i=i+1
}
####Part 4 option 2: separate list of posts by language
postsByLanguage<- c()
y=1
z=length(languages[[1]])+1
while(y<z){
listPostsByLanguage <-c()
i=1
j=length(listPosts)+1
while(i<j){
postsLanguage <- c()
print (paste("Subsetting posts in list",i,"out of",(j-1),sep=" "))
k=1
l=length(listPosts[[i]])+1
while(k<l){
if(length(listPosts[[i]][[k]])>0 && length(listPosts[[i]][[k]][[1]])>0){
print (paste("page",k,sep=" "))
postsLanguage[[k]] <- subset(listPosts[[i]][[k]],listPosts[[i]][[k]]$language==as.character(languages[[1]][[y]]))
k=k+1
}
else{
print (paste("page",k,sep=" "))
k=k+1
}
}
listPostsByLanguage[[i]] <- postsLanguage
i=i+1
}
postsByLanguage[[y]] <- listPostsByLanguage
y=y+1
}
####Part 5: Verify the relevance of the posts
#Define or import keywords that guarantee the post to be relevant
confirmedKeywords <- c(#Enter keywords)
#Define empty dataframe for dataframes that have no posts in them
emptyDF <- listEnglishPosts[[3]][[1]]
#Mark the entries containing keywords that guarantee that the post is relevant as verified, and ask the user for the manual varification
for the rest of them.
i=3
j=length(listEnglishPosts)+1
while(i<j){
print (paste("Subsetting posts in list",i,"out of",(j-1),sep=" "))
k=1
l=length(listEnglishPosts[[i]])+1
while(k<l){
if(length(listEnglishPosts[[i]][[k]])>0 && length(listEnglishPosts[[i]][[k]][[1]])>0){
print (paste("page",k,sep=" "))
listEnglishPosts[[i]][[k]]$verified <- "NA"
m=1
n=length(listEnglishPosts[[i]][[k]]$message)+1
while(m<n){
o=1
p=length(confirmedKeywords)+1
while (o<p){
y <- grepl(confirmedKeywords[o], listEnglishPosts[[i]][[k]]$message[m],ignore.case = TRUE)
if(y==TRUE){
listEnglishPosts[[i]][[k]]$verified[m] <- "yes"
o=o+1}
else{
o=o+1}
}
if(listEnglishPosts[[i]][[k]]$verified[m] == "NA"){
if(verifyText(listEnglishPosts[[i]][[k]]$message[m],listEnglishPosts[[i]][[k]]$keyword[m])==T){
listEnglishPosts[[i]][[k]]$verified[m] <- "yes"
}
else{
listEnglishPosts[[i]][[k]]$verified <- "no"
}
}
m=m+1
}
k=k+1
print (k)
}
else{
listEnglishPosts[[i]][[k]] <- emptyDF
print (paste("page",k,sep=" "))
k=k+1
print(k)
}
}
i=i+1
}