/
embedding_glove.R
272 lines (252 loc) · 8.16 KB
/
embedding_glove.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
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
#' Global Vectors for Word Representation
#'
#' The GloVe pre-trained word vectors provide word embeddings created using
#' varying numbers of tokens.
#'
#' Citation info:
#'
#' InProceedings\{pennington2014glove, \cr
#' author = \{Jeffrey Pennington and Richard Socher and Christopher D. \cr
#' Manning\}, \cr
#' title = \{GloVe: Global Vectors for Word Representation\}, \cr
#' booktitle = \{Empirical Methods in Natural Language Processing (EMNLP)\}, \cr
#' year = 2014 \cr
#' pages = \{1532-1543\} \cr
#' url = \{http://www.aclweb.org/anthology/D14-1162\} \cr
#' \}
#'
#' @references Jeffrey Pennington, Richard Socher, and Christopher D. Manning.
#' 2014. GloVe: Global Vectors for Word Representation.
#'
#' @inheritParams lexicon_afinn
#' @param dimensions A number indicating the number of vectors to include. One
#' of 50, 100, 200, or 300 for glove6b, or one of 25, 50, 100, or 200 for
#' glove27b.
#' @return A tibble with 400k, 1.9m, 2.2m, or 1.2m rows (one row for each unique
#' token in the vocabulary) and the following variables:
#' \describe{
#' \item{token}{An individual token (usually a word)}
#' \item{d1, d2, etc}{The embeddings for that token.}
#' }
#' @source \url{https://nlp.stanford.edu/projects/glove/}
#' @keywords datasets
#' @family embeddings
#' @examples
#' \dontrun{
#' embedding_glove6b(dimensions = 50)
#'
#' # Custom directory
#' embedding_glove42b(dir = "data/")
#'
#' # Deleting dataset
#' embedding_glove6b(delete = TRUE, dimensions = 300)
#'
#' # Returning filepath of data
#' embedding_glove840b(return_path = TRUE)
#' }
#' @name embedding_glove
NULL
#' @rdname embedding_glove
#' @export
#' @importFrom fs file_exists dir_exists dir_create path
#' @importFrom readr read_rds
#' @importFrom utils menu
embedding_glove6b <- function(dir = NULL,
dimensions = c(50, 100, 200, 300),
delete = FALSE,
return_path = FALSE,
clean = FALSE,
manual_download = FALSE) {
this_glove <- "6b"
available_dims <- c(50, 100, 200, 300)
all_names <- construct_glove_name(this_glove, available_dims)
dimensions <- as.character(dimensions)
dimensions <- match.arg(dimensions, as.character(available_dims))
name <- construct_glove_name(this_glove, dimensions)
load_dataset(
data_name = "glove6b", name = name, dir = dir,
delete = delete, return_path = return_path, clean = clean,
clean_manual = all_names,
manual_download = manual_download
)
}
#' @keywords internal
construct_glove_name <- function(tokens = c("6b", "27b"),
dimensions = c(25, 50, 100, 200, 300)) {
tokens <- match.arg(tokens)
dimensions <- as.character(dimensions)
dimensions <- match.arg(
dimensions,
choices = as.character(c(25, 50, 100, 200, 300)),
several.ok = TRUE
)
paste0(
paste(
"glove",
tokens,
dimensions,
sep = "_"
),
".rds"
)
}
#' @rdname embedding_glove
#' @export
#' @importFrom fs file_exists dir_exists dir_create path
#' @importFrom readr read_rds
#' @importFrom utils menu
embedding_glove27b <- function(dir = NULL,
dimensions = c(25, 50, 100, 200),
delete = FALSE,
return_path = FALSE,
clean = FALSE,
manual_download = FALSE) {
this_glove <- "27b"
available_dims <- c(25, 50, 100, 200)
all_names <- construct_glove_name(this_glove, available_dims)
dimensions <- as.character(dimensions)
dimensions <- match.arg(dimensions, as.character(available_dims))
name <- construct_glove_name(this_glove, dimensions)
load_dataset(
data_name = "glove27b", name = name, dir = dir,
delete = delete, return_path = return_path, clean = clean,
clean_manual = all_names,
manual_download = manual_download
)
}
#' @rdname embedding_glove
#' @export
#' @importFrom fs file_exists dir_exists dir_create path
#' @importFrom readr read_rds
#' @importFrom utils menu
embedding_glove42b <- function(dir = NULL,
delete = FALSE,
return_path = FALSE,
clean = FALSE,
manual_download = FALSE) {
name <- "glove_42b.rds"
load_dataset(
data_name = "glove42b", name = name, dir = dir,
delete = delete, return_path = return_path, clean = clean,
manual_download = manual_download
)
}
#' @rdname embedding_glove
#' @export
#' @importFrom fs file_exists dir_exists dir_create path
#' @importFrom readr read_rds
#' @importFrom utils menu
embedding_glove840b <- function(dir = NULL,
delete = FALSE,
return_path = FALSE,
clean = FALSE,
manual_download = FALSE) {
name <- "glove_840b.rds"
load_dataset(
data_name = "glove840b", name = name, dir = dir,
delete = delete, return_path = return_path, clean = clean,
manual_download = manual_download
)
}
#' @importFrom utils download.file
#' @keywords internal
download_glove6b <- function(folder_path) {
file_path <- path(folder_path, "glove.6B.zip")
if (file_exists(file_path)) {
return(invisible())
}
download.file(
url = "http://nlp.stanford.edu/data/glove.6B.zip",
destfile = file_path
)
}
#' @importFrom utils download.file
#' @keywords internal
download_glove42b <- function(folder_path) {
file_path <- path(folder_path, "glove.42B.300d.zip")
if (file_exists(file_path)) {
return(invisible())
}
download.file(
url = "http://nlp.stanford.edu/data/glove.42B.300d.zip",
destfile = file_path
)
}
#' @importFrom utils download.file
#' @keywords internal
download_glove840b <- function(folder_path) {
file_path <- path(folder_path, "glove.840B.300d.zip")
if (file_exists(file_path)) {
return(invisible())
}
download.file(
url = "http://nlp.stanford.edu/data/glove.840B.300d.zip",
destfile = file_path
)
}
#' @importFrom utils download.file
#' @keywords internal
download_glove27b <- function(folder_path) {
file_path <- path(folder_path, "glove.twitter.27B.zip")
if (file_exists(file_path)) {
return(invisible())
}
download.file(
url = "http://nlp.stanford.edu/data/glove.twitter.27B.zip",
destfile = file_path
)
}
#' @keywords internal
process_glove6b <- function(folder_path, name_path) {
# Processing all datasets when they only need one adds time. We'll
# specifically deal with the one they requested, which means we need to
# extract the dimensions back out of the name to build the raw filename.
filename <- gsub(folder_path, "", name_path)
dimensions <- unlist(strsplit(filename, "_|\\."))[[3]]
raw_name <- paste0("glove.6B.", dimensions, "d.txt")
file <- unz(path(folder_path, "glove.6B.zip"), raw_name)
write_glove(file, name_path, dimensions)
}
#' @keywords internal
process_glove42b <- function(folder_path, name_path) {
dimensions <- 300
raw_name <- "glove.42B.300d.txt"
file <- unz(path(folder_path, "glove.42B.300d.zip"), raw_name)
write_glove(file, name_path, dimensions)
}
#' @keywords internal
process_glove840b <- function(folder_path, name_path) {
dimensions <- 300
raw_name <- "glove.840B.300d.txt"
file <- unz(path(folder_path, "glove.840B.300d.zip"), raw_name)
write_glove(file, name_path, dimensions)
}
#' @keywords internal
process_glove27b <- function(folder_path, name_path) {
filename <- gsub(folder_path, "", name_path)
dimensions <- unlist(strsplit(filename, "_|\\."))[[3]]
raw_name <- paste0("glove.twitter.27B.", dimensions, "d.txt")
file <- unz(path(folder_path, "glove.twitter.27B.zip"), raw_name)
write_glove(file, name_path, dimensions)
}
#' @importFrom readr read_delim write_rds
#' @keywords internal
write_glove <- function(file, name_path, dimensions) {
embeddings <- read_delim(
file,
delim = " ",
quote = "",
col_names = c(
"token",
paste0("d", seq_len(dimensions))
),
col_types = paste0(
c(
"c",
rep("d", dimensions)
),
collapse = ""
)
)
write_rds(embeddings, name_path)
}