-
Notifications
You must be signed in to change notification settings - Fork 1
/
09_quanteda_E3.R
155 lines (114 loc) · 4.48 KB
/
09_quanteda_E3.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
writeLines("It is part of my CNPq-funded project and seeks to make corpus tools and R accessible. If you have any doubts or wish to make any research contact please send me an email. Rodrigo de Lima-Lopes [email protected]")
# Packages ----------------------------------------------------------------
library(quanteda)
library(quanteda.textplots)
library(quanteda.textstats)
library(ggplot2)
#Creating the corpus
presidents.C <- corpus(presidents)
# Which are the variables?
head(docvars(presidents.C))
#Creating subcorpora
lula.c <- corpus_subset(presidents.C, screen_name == "LulaOficial")
ciro.c <- corpus_subset(presidents.C, screen_name == "cirogomes")
JB.c <- corpus_subset(presidents.C, screen_name == "jairbolsonaro")
# Tokenisation
#Lula
lula.toc <- tokens(lula.c,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_numbers = TRUE,
verbose = TRUE)
lula.toc <- tokens_remove(lula.toc,
stopwords("pt"),
valuetype = "fixed",
verbose = TRUE
) %>% tokens_tolower()
#Ciro
ciro.toc <- tokens(ciro.c,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_numbers = TRUE,
verbose = TRUE)
ciro.toc <- tokens_remove(ciro.toc,
stopwords("pt"),
valuetype = "fixed",
verbose = TRUE
) %>% tokens_tolower()
# JB
JB.toc <- tokens(JB.c,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_numbers = TRUE,
verbose = TRUE)
JB.toc <- tokens_remove(JB.toc,
stopwords("pt"),
valuetype = "fixed",
verbose = TRUE
) %>% tokens_tolower()
# Kwic
kwic(JB.toc,"Brasil") |> View()
kwic(lula.toc,"Brasil") |> View()
kwic(ciro.toc,"Brasil") |> View()
#Bigrams
lula.col <- textstat_collocations(lula.toc, method = "lambda",
size = 2,
min_count = 2,
smoothing = 0.5,
tolower = TRUE,
verbose = TRUE)
ciro.col <- textstat_collocations(ciro.toc, method = "lambda",
size = 2,
min_count = 2,
smoothing = 0.5,
tolower = TRUE,
verbose = TRUE)
JB.col <- textstat_collocations(JB.toc, method = "lambda",
size = 2,
min_count = 2,
smoothing = 0.5,
tolower = TRUE,
verbose = TRUE)
# Comparing the candidates
a.lula_Ciro <- corpus_subset(presidents.C, screen_name != "jairbolsonaro")
b.lula_JB <- corpus_subset(presidents.C, screen_name != "cirogomes")
c.ciro_JB <- corpus_subset(presidents.C, screen_name != "LulaOficial")
# Lula vs ciro
a.tk <- tokens(a.lula_Ciro,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_numbers = TRUE,
verbose = TRUE) %>%
tokens_remove(pattern = stopwords("pt")) %>%
tokens_group(groups = screen_name)
dfm.a <- dfm(a.tk, verbose = TRUE)
textstat_keyness(dfm.a,
target = "LulaOficial",
measure = "lr") |>
textplot_keyness(n= 25)
# Lula vs JB
b.tk <- tokens(b.lula_JB,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_numbers = TRUE,
verbose = TRUE) %>%
tokens_remove(pattern = stopwords("pt")) %>%
tokens_group(groups = screen_name)
dfm.b <- dfm(b.tk, verbose = TRUE)
textstat_keyness(dfm.b,
target = "LulaOficial",
measure = "lr") |>
textplot_keyness(n= 25)
# Ciro vs JB
c.tk <- tokens(c.ciro_JB,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_numbers = TRUE,
verbose = TRUE) %>%
tokens_remove(pattern = stopwords("pt")) %>%
tokens_group(groups = screen_name)
dfm.c <- dfm(c.tk, verbose = TRUE)
textstat_keyness(dfm.c,
target = "cirogomes",
measure = "lr") |>
textplot_keyness(n= 25)