This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_corpora.R
143 lines (114 loc) · 5.33 KB
/
create_corpora.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
138
139
140
141
142
143
library(tidyverse)
library(DBI)
library(RSQLite)
library(storr)
library(glue)
library(tidytext)
db <- dbConnect(SQLite(), "data/corpus.sqlite3")
shn <- dbConnect(SQLite(), "data/shiny.sqlite3")
dbExecute(db, "PRAGMA foreign_keys = ON")
dbExecute(shn, "PRAGMA foreign_keys = ON")
st <- storr_dbi("storr_data", "storr_keys", shn, binary = TRUE)
bigrams <- tbl(db, "bigrams")
unigrams <- tbl(db, "unigrams")
document_unigram <- tbl(db, "document_unigram")
document_bigram <- tbl(db, "document_bigram")
documents <- tbl(db, "documents")
document_metadata <- tbl(db, "document_metadata")
corpora <- tbl(shn, "corpus")
corpus_document <- tbl(shn, "corpus_document")
corpus_rdata <- tbl(shn, "corpus_rdata")
unigram_stems <- tbl(db, "unigram_stems")
#' @param special_bigrams These bigrams will be explicitly added to the "tokens" table
create_corpus <- function(db, shn, st, including_all_unigrams = NULL, including_all_bigrams = NULL, special_bigrams = c("artificial intelligence", "machine learning", "big data", "a i", "computer science"), min_token_n = 20, corpus_label) {
if (corpora %>%
filter(label == corpus_label) %>%
collect() %>%
nrow() != 0) {
stop(glue("A corpus labelled \"{corpus_label}\" already exists."))
}
english_docs <- document_metadata %>%
filter(language %in% c("EN", "en", "eng") | is.na(language), year >= 2013)
corpus_document_ids <- documents %>%
semi_join(english_docs, by = "document_id") %>%
select(document_id)
if (!is.null(including_all_unigrams)) {
inclusive_unigrams <- unigrams %>%
filter(unigram %in% including_all_unigrams)
inclusive_unigram_docs <- inclusive_unigrams %>%
left_join(document_unigram, by = "unigram_id")
corpus_document_ids <- corpus_document_ids %>%
semi_join(inclusive_unigram_docs, by = "document_id")
}
if (!is.null(including_all_bigrams)) {
inclusive_bigrams <- bigrams %>%
filter(bigram %in% including_all_bigrams)
inclusive_bigram_docs <- inclusive_bigrams %>%
left_join(document_bigram, by = "bigram_id")
corpus_document_ids <- corpus_document_ids %>%
semi_join(inclusive_bigram_docs, by = "document_id")
}
message("Collecting document ids...")
dbExecute(db, "DROP TABLE IF EXISTS temp_corpus_table")
corpus_document_ids %>%
compute("temp_corpus_table")
temp_corpus_table <- tbl(db, "temp_corpus_table")
inclusive_bigrams <- bigrams %>%
filter(bigram %in% c(special_bigrams, including_all_bigrams))
# Coalesce stemmed words (this hurts browsing tbh)
# %>%
# inner_join(unigram_stems, by = "unigram_id") %>%
# select(document_id, gram = stem, n) %>%
# group_by(document_id, gram)%>%
#summarize(n = sum(n)),
corpus_tokens <- dplyr::union(
document_unigram %>%
inner_join(unigrams, by = "unigram_id") %>% # join all unigrams
filter((is_numeric == 0 & nchar >= 3) | unigram == "ai") %>%
semi_join(temp_corpus_table, by = "document_id") %>%
select(document_id, gram = unigram, n),
document_bigram %>%
inner_join(inclusive_bigrams, by = "bigram_id") %>% # join only included & special bigrams
semi_join(temp_corpus_table, by = "document_id") %>%
select(document_id, gram = bigram, n)
)
dbAppendTable(shn, "corpus", tibble(label = corpus_label))
new_corpus_id <- corpora %>%
filter(label == corpus_label) %>%
collect() %>%
pull(corpus_id)
new_corpus_docs <- collect(temp_corpus_table) %>%
mutate(corpus_id = new_corpus_id) %>%
select(corpus_id, document_id)
dbAppendTable(shn, "corpus_document", new_corpus_docs)
message("Collecting tokens...")
collected_tokens <- collect(corpus_tokens) %>%
group_by(gram) %>%
# Only include tokens that appear over min_token_n times across the entire corpus
# This has to happen after collecting because it involves filtering on a window function
# which is not suported in sqlite
filter(n_distinct(document_id) >= min_token_n) %>%
ungroup()
collected_token_key <- glue("collected-{new_corpus_id}-tokens")
st$set(collected_token_key, collected_tokens)
dbAppendTable(shn, "corpus_rdata", tibble(corpus_id = new_corpus_id, object_type = "tidy-tokens", storr_key = collected_token_key))
message("Storing DFM...")
token_dfm <- cast_dfm(collected_tokens, document_id, gram, n)
dfm_key <- glue("{new_corpus_id}-dfm")
st$set(dfm_key, token_dfm)
dbAppendTable(shn, "corpus_rdata", tibble(corpus_id = new_corpus_id, object_type = "dfm", storr_key = dfm_key))
return(new_corpus_id)
}
drop_corpora <- function() {
dbExecute(db, "delete from corpus")
dbExecute(db, "delete from storr_keys")
dbExecute(db, "delete from storr_data")
dbExecute(shn, "delete from corpus")
dbExecute(shn, "delete from storr_keys")
dbExecute(shn, "delete from storr_data")
}
drop_corpora()
create_corpus(db, shn, st, including_all_unigrams = NULL, including_all_bigrams = "artificial intelligence", corpus_label = "JSTOR Artificial Intelligence")
create_corpus(db, shn, st, including_all_unigrams = NULL, including_all_bigrams = "big data", corpus_label = "JSTOR Big Data")
create_corpus(db, shn, st, including_all_unigrams = NULL, including_all_bigrams = "machine learning", corpus_label = "JSTOR Machine Learning")
create_corpus(db, shn, st, including_all_unigrams = "ethics", including_all_bigrams = "artificial intelligence", corpus_label = "JSTOR 'Ethics' and 'Artificial Intelligence'")