-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathannotated_data.R
More file actions
269 lines (211 loc) · 10.7 KB
/
Copy pathannotated_data.R
File metadata and controls
269 lines (211 loc) · 10.7 KB
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
library(quanteda)
library(readtext)
library(tidyverse)
library(udpipe)
source("functions/featsinfo_to_column.R")
source("functions/roots_for_group.R")
source("functions/collect_sentiment_for_cat.R")
######################
### Sentiment data ###
######################
# prepare sentiment data
neg <- scan("data/SentiWS_v1.8c_Negative.txt", what = "char", sep = "\n", fileEncoding="utf-8")
pos <- scan("data/SentiWS_v1.8c_Positive.txt", what = "char", sep = "\n", fileEncoding="utf-8")
s <- str_split(neg, "\t")
t <- str_split(pos, "\t")
terms.neg <- sub("([A-Za-zß]+)[|][A-Za-zß]+", "\\1",lapply(s, function(l) l[[1]]))
terms.pos <- sub("([A-Za-zß]+)[|][A-Za-zß]+", "\\1",lapply(t, function(l) l[[1]]))
value.neg <- unlist(lapply(s, function(l) as.double(l[[2]])))
value.pos <- unlist(lapply(t, function(l) as.double(l[[2]])))
positive <- data.frame(term=terms.pos, value=value.pos)
negative <- data.frame(term=terms.neg, value=value.neg)
# add sentiment value
senti_dict <- rbind(positive, negative)
# OR: Load sentiment dictionary
load("RData/senti_dict.RData")
########################
### root information ###
########################
# annotate text (Warning: takes forever. like 30min)
annotated_model <- udpipe_annotate(udmodel_german, x = programs) %>%
as.data.frame()
# Instead read in this:
load("RData/annotated_corpus.RData")
# partition of corpus
regierung <- c('doc3', 'doc4', 'doc10', 'doc11', 'doc12', 'doc19', 'doc23', 'doc24', 'doc25', 'doc27')
opposition <- c('doc1', 'doc2', 'doc5', 'doc6', 'doc7', 'doc8', 'doc9', 'doc13', 'doc14', 'doc15', 'doc16', 'doc17', 'doc18', 'doc20', 'doc21', 'doc22', 'doc26')
# split into sub groups
sub_model_regierung <- subset(annotated_model, doc_id %in% regierung)
sub_model_opposition <- subset(annotated_model, doc_id %in% opposition)
# list of words a party could use to talk about themselves
# first one is name of the party
regierung_list <- list('doc3' = c('bündnis90/die grünen', 'wir'),
'doc4' = c('bündnis90/die grünen', 'wir'),
'doc10' = c('cdu', 'wir'),
'doc11' = c('cdu', 'wir'),
'doc12' = c('cdu', 'wir'),
'doc19' = c('fdp', 'wir'),
'doc23' = c('spd', 'wir'),
'doc24' = c('spd', 'wir'),
'doc25' = c('spd', 'wir'),
'doc27' = c('spd', 'wir'))
opposition_list <- list('doc1' = c('afd', 'alternative', 'wir'),
'doc2' = c('afd', 'alternative', 'wir'),
'doc5' = c('bündnis90/die grünen', 'bündnis90', 'grüne', 'wir'),
'doc6' = c('bündnis90/die grünen', 'bündnis90', 'grüne', 'wir'),
'doc7' = c('bündnis90/die grünen', 'bündnis90', 'grüne', 'wir'),
'doc8' = c('cdu', 'wir'),
'doc9' = c('cdu', 'wir'),
'doc13' = c('die linke', 'linke', 'wir'),
'doc14' = c('die linke', 'linke', 'wir'),
'doc15' = c('die linke', 'linke', 'wir'),
'doc16' = c('fdp', 'wir'),
'doc17' = c('fdp', 'wir'),
'doc18' = c('fdp', 'wir'),
'doc20' = c('fdp', 'wir'),
'doc21' = c('pds', 'wir'),
'doc22' = c('linkspartei.pds', 'wir'),
'doc26' = c('spd', 'wir'))
# use function on opposition subset of annotated model
opp_roots <- roots_for_group(sub_model_opposition, opposition_list, senti_dict)
# use function on government subset of annotated model
gov_roots <- roots_for_group(sub_model_regierung, regierung_list, senti_dict)
# optional: save to csv
write.csv2(opp_roots, 'data/opp_roots_info.csv', fileEncoding = "utf-8")
write.csv2(gov_roots, 'data/gov_roots_info.csv', fileEncoding = "utf-8")
# Self referential verbs
verbs.top.50 <- union(head(gov_roots, 50)$lemma, head(opp_roots, 50)$lemma)
freq.verbs <- data.frame(matrix(ncol=3, nrow=0))
colnames(freq.verbs) <- c("lemma", "freq_gov", "freq_opp")
sum.all.gov <- sum(gov_roots$freq)
sum.all.opp <- sum(opp_roots$freq)
for (i in 1:length(verbs.top.50)){
curr.verb <- verbs.top.50[i]
freq.gov <- sum(filter(gov_roots, lemma == curr.verb)$freq)/sum.all.gov
freq.opp <- sum(filter(opp_roots, lemma == curr.verb)$freq)/sum.all.opp
curr.row <- data.frame(lemma=curr.verb, freq_gov=freq.gov, freq_opp = freq.opp)
freq.verbs <- rbind(freq.verbs, curr.row)
}
freq.verbs
gov.greater <- ifelse(freq.verbs$freq_gov >= freq.verbs$freq_opp, TRUE, FALSE)
freq.verbs$gov.greater <- gov.greater
# Plot results
ggplot(freq.verbs, aes(y = freq_gov, x = freq_opp, label =lemma, color = gov.greater))+
geom_text(size = 5)+
scale_x_continuous(trans = 'log10', breaks = c(0, 0.001, 0.010, 0.1)) +
scale_y_continuous(trans = 'log10', breaks = c(0, 0.001, 0.010, 0.05))+
scale_color_manual(values = c('TRUE' = 'darkblue', 'FALSE' = 'darkred'), guide = "none")+
theme_minimal()+
labs(x="Relative Frequenz in Oppositionsparteien",
y="Relative Frequenz in Regierungsparteien",
title="Verben in selbstreferentiellen Sätzen")+
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))
##################################################
### extract sentiment based on value of column ###
##################################################
opp_nouns <- collect_sentiment_for_cat(sub_model_opposition, "upos", 'NOUN', senti_dict)
gov_nouns <- collect_sentiment_for_cat(sub_model_regierung, "upos", 'NOUN', senti_dict)
opp_adv <- collect_sentiment_for_cat(sub_model_opposition, "upos", 'ADJ', senti_dict)
gov_adv <- collect_sentiment_for_cat(sub_model_regierung, "upos", 'ADJ', senti_dict)
head(opp_nouns)
head(gov_nouns)
## TOP NOUNS OPPOS AND GOV
noun.top <- union(head(opp_nouns, 60)$lemma, head(gov_nouns, 40)$lemma)
sum.gov <- sum(gov_nouns$freq)
sum.opp <- sum(opp_nouns$freq)
freq.nouns <- data.frame(matrix(ncol=3, nrow=0))
colnames(freq.nouns) <- c("lemma", "freq_gov", "freq_opp")
for (i in 1:length(noun.top)){
curr.noun <- noun.top[i]
gov.freq <- sum(filter(gov_nouns, lemma == curr.noun)$freq)/sum.gov
opp.freq <- sum(filter(opp_nouns, lemma == curr.noun)$freq)/sum.opp
tmp.data <- data.frame(lemma=curr.noun, gov_freq = gov.freq, opp_freq = opp.freq)
freq.nouns <- rbind(freq.nouns, tmp.data)
}
freq.nouns$gov.greater <- ifelse(freq.nouns$gov_freq >= freq.nouns$opp_freq, TRUE, FALSE)
ggplot(freq.nouns, aes(y = gov_freq, x = opp_freq, label =lemma, color = gov.greater))+
geom_text(size = 5)+
scale_x_continuous(trans = 'log10', breaks = c(0, 0.001, 0.010, 0.1)) +
scale_y_continuous(trans = 'log10', breaks = c(0, 0.001, 0.010, 0.05))+
scale_color_manual(values = c('TRUE' = 'darkblue', 'FALSE' = 'darkred'), guide = "none")+
theme_minimal()+
labs(x="Relative Frequenz in Oppositionsparteien",
y="Relative Frequenz in Regierungsparteien",
title="Häufige Nomen bei Oppositions- und Regierungsparteien")+
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))
## OPP GOV ADJ
head(opp_adv)
head(gov_adv)
adj.top <- union(head(opp_adv, 50)$lemma, head(gov_adv, 50)$lemma)
sum.gov <- sum(gov_adv$freq)
sum.opp <- sum(opp_adv$freq)
freq.adj <- data.frame(matrix(ncol=3, nrow=0))
colnames(freq.nouns) <- c("lemma", "freq_gov", "freq_opp")
for (i in 1:length(adj.top)){
curr.adj <- adj.top[i]
gov.freq <- sum(filter(gov_adv, lemma == curr.adj)$freq)/sum.gov
opp.freq <- sum(filter(opp_adv, lemma == curr.adj)$freq)/sum.opp
tmp.data <- data.frame(lemma=curr.adj, gov_freq = gov.freq, opp_freq = opp.freq)
freq.adj <- rbind(freq.adj, tmp.data)
}
freq.adj$gov.greater <- ifelse(freq.adj$gov_freq >= freq.adj$opp_freq, TRUE, FALSE)
ggplot(freq.adj, aes(y = gov_freq, x = opp_freq, label =lemma, color = gov.greater))+
geom_text(size = 5)+
scale_x_continuous(trans = 'log10', breaks = c(0, 0.005, 0.010, 0.020)) +
scale_y_continuous(trans = 'log10', breaks = c(0, 0.020, 0.010, 0.005))+
scale_color_manual(values = c('TRUE' = 'darkblue', 'FALSE' = 'darkred'), guide = "none")+
theme_minimal()+
labs(x="Relative Frequenz in Oppositionsparteien",
y="Relative Frequenz in Regierungsparteien",
title="Häufige Adjektive bei Oppositions- und Regierungsparteien")+
theme(axis.text=element_text(size=16),
axis.title=element_text(size=20,face="bold"))
## TENSE
s.gov <- sum(gov_roots$freq)
s.opp <- sum(opp_roots$freq)
gov.type <- c("opposition", "goverment")
pres.type <- c(sum(filter(opp_roots, tense == "Pres")$freq)/s.opp,
sum(filter(gov_roots, tense == "Pres")$freq)/s.gov)
past.type <- c(sum(filter(opp_roots, tense == "Past")$freq)/s.opp,
sum(filter(gov_roots, tense == "Past")$freq)/s.gov)
tense <- data.frame(type=gov.type, pres =pres.type, past=past.type)
## VERBFORM
inf.type <- c(sum(filter(opp_roots, verbform == "Inf")$freq)/s.opp,
sum(filter(gov_roots, verbform == "Inf")$freq)/s.gov)
fin.type <- c(sum(filter(opp_roots, verbform == "Fin")$freq)/s.opp,
sum(filter(gov_roots, verbform == "Fin")$freq)/s.gov)
part.type <- c(sum(filter(opp_roots, verbform == "Part")$freq)/s.opp,
sum(filter(gov_roots, verbform == "Part")$freq)/s.gov)
verbform <- data.frame(type=gov.type, inf=inf.type, fin =fin.type, part = part.type)
### Sentiment -- Adjectives
# Government
adj.s.gov <- sum(gov_adv$freq)
tmp.sent <- ifelse(gov_adv$senti_ws == "-", 0, gov_adv$senti_ws)
gov_adv$tmp.sent <- as.numeric(tmp.sent)
neg.adj <- filter(gov_adv, tmp.sent<0)
f <- abs(sum(neg.adj$tmp.sent * neg.adj$freq/adj.s.gov))
pos.adj <- filter(gov_adv, tmp.sent>0)
g <- sum(pos.adj$tmp.sent * pos.adj$freq/adj.s.gov)
# Opposition
adj.s.opp <- sum(opp_adv$freq)
tmp.sent <- ifelse(opp_adv$senti_ws == "-", 0, opp_adv$senti_ws)
opp_adv$tmp.sent <- as.numeric(tmp.sent)
neg.adj <- filter(opp_adv, tmp.sent<0)
h <- abs(sum(neg.adj$tmp.sent * neg.adj$freq/adj.s.opp))
pos.adj <- filter(opp_adv, tmp.sent>0)
i <- sum(pos.adj$tmp.sent * pos.adj$freq/adj.s.opp )
# Erstellung von Dataframe und Plot
adv_senti <- c('positive', 'negative', 'positive', 'negative')
combi_assignment <- c('Regierung', 'Regierung', 'Opposition', 'Opposition')
adv_senti_df <- data.frame(adv_senti, combi_assignment)
adv_senti_df$Wert <- c(g, f, i, h)
adv_senti_plot <- ggplot(adv_senti_df, aes(x=adv_senti, y=Wert, fill=combi_assignment)) +
geom_bar(stat='identity', position='dodge') +
xlab('Sentiment') +
ylab('kumulierter Wert') +
ggtitle('kumulierte Sentimentwerte für Adjektive') +
guides(fill=guide_legend(title="Position")) +
scale_fill_manual("Position", values = c("Opposition" = "darkred", "Regierung" = "darkblue"))
adv_senti_plot