-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbase.R
More file actions
428 lines (379 loc) · 18 KB
/
base.R
File metadata and controls
428 lines (379 loc) · 18 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
library(rbace)
library(stringr)
library(dplyr)
source('preprocess.R')
# get_papers
#
# Params:
#
# * query: search query
# * params: parameters for the search in JSON format
# * from: publication date lower bound in the form YYYY-MM-DD
# * to: publication date upper bound in the form YYYY-MM-DD
# * article_types: in the form of an array of identifiers of article types
# * sorting: can be one of "most-relevant" and "most-recent"
# * limit: number of search results to return
# * retry_opts: BASE retry options, see `?rbace::bs_retry_options` for documentation.
# `?httr::RETRY` has more detailed explanation of the options. default values are used if
# none are supplied.
#
# It is expected that get_papers returns a list containing two data frames named "text" and "metadata"
#
# "text" contains the text for similarity analysis; it is expected to have two columns "id" and "content"
#
# "metadata" contains all metadata; its columns are expected to be named as follows:
# * "id": a unique ID, preferably the DOI
# * "title": the title
# * "authors": authors, preferably in the format "LASTNAME1, FIRSTNAME1;LASTNAME2, FIRSTNAME2"
# * "paper_abstract": the abstract
# * "published_in": name of the journal or venue
# * "year": publication date
# * "url": URL to the landing page
# * "readers": an indicator of the paper's popularity, e.g. number of readers, views, downloads etc.
# * "subject": keywords or classification, split by ;
# * "oa_state": open access status of the item; has the following possible states: 0 for no, 1 for yes, 2 for unknown
# * "link": link to the PDF; if this is not available, a list of candidate URLs that may contain a link to the PDF
blog <- getLogger('api.base')
get_papers <- function(query, params,
retry_opts=rbace::bs_retry_options(3,60,3,4)) {
blog$info(paste("vis_id:", .GlobalEnv$VIS_ID, "Search:", query))
start.time <- Sys.time()
if (!is.null(query)) {
exact_query <- preprocess_query((query))
} else {
exact_query <- NULL
}
blog$info(paste("vis_id:", .GlobalEnv$VIS_ID, "exact query:", exact_query))
limit = params$limit
# prepare query fields
document_types = paste("dctypenorm:", "(", paste(params$document_types, collapse=" OR "), ")", sep="")
sortby_string = ifelse(params$sorting == "most-recent", "dcyear desc", "")
return_fields <- "dcdocid,dctitle,dcdescription,dcsource,dcdate,dcsubject,dccreator,dclink,dcoa,dcidentifier,dcrelation,dctype,dctypenorm,dcprovider,dclang,dclanguage,dccoverage"
if (!is.null(exact_query) && exact_query != '') {
base_query <- paste(paste0("(",exact_query,")"), document_types, collapse=" ")
} else {
base_query <- paste(document_types, collapse=" ")
}
if (!is.null(params$vis_type) && params$vis_type == "timeline") {
if (!is.null(params$exclude_date_filters)) {
params$exclude_date_filters <- NULL
}
}
if (!is.null(params$exclude_date_filters)
&& (params$exclude_date_filters == TRUE || params$exclude_date_filters == "true")) {
} else {
date_string = paste0("dcdate:[", params$from, " TO ", params$to , "]")
base_query <- paste(date_string, base_query)
}
# apply language filter if parameter is set
lang_id <- params$lang_id
if (!is.null(lang_id) && lang_id != "all-lang") {
lang_query = paste("dclang:", "(", paste(params$lang_id, collapse=" OR "), ")", sep="")
base_query <- paste(base_query, lang_query)
}
q_advanced = params$q_advanced
if (!is.null(q_advanced)) {
base_query <- paste(base_query, q_advanced)
}
min_descsize <- if (is.null(params$min_descsize)) 300 else params$min_descsize
filter <- I(paste0('descsize:[', min_descsize, '%20TO%20*]'))
limit <- params$limit
repo = params$repo
coll = params$coll
if(!is.null(repo) && repo=="fttriple") {
non_public = TRUE
} else {
non_public = FALSE
}
cc <- params$custom_clustering
if (!is.null(cc)) {
if (cc %in% names(fieldmapper)) {
# this is the generic case for existing metadata
custom_clustering_query <- paste(fieldmapper[[cc]], ":", "*", sep="")
base_query <- paste(base_query, custom_clustering_query)
} else {
# this is the speciality case for custom clustering on annotations
custom_clustering_query <- paste("dcsubject:", cc, "*", sep="")
base_query <- paste(base_query, custom_clustering_query)
custom_clustering_query <- paste('textus:', '"', cc, ':"', sep="")
base_query <- paste(base_query, custom_clustering_query)
custom_clustering_query <- paste(cc, ':*', sep="")
base_query <- paste(base_query, custom_clustering_query)
}
}
blog$info(paste("vis_id:", .GlobalEnv$VIS_ID, "BASE query:", base_query))
# execute search
offset = 0
res_raw <- get_raw_data(limit,
base_query,
return_fields,
sortby_string,
filter,
repo,
coll,
retry_opts,
offset,
non_public)
res <- res_raw$docs
if (nrow(res)==0){
stop(paste("No results retrieved."))
}
metadata <- etl(res, repo, non_public)
metadata <- sanitize_abstract(metadata)
metadata <- mark_duplicates(metadata)
metadata$has_dataset <- unlist(lapply(metadata$resulttype, function(x) "Dataset" %in% x))
req_limit <- 9
r <- 0
# check if custom clustering annotation param is in metadata
if (!is.null(cc)) {
if (!(cc %in% names(fieldmapper))) {
has_custom_clustering_annotation <- unlist(lapply(metadata$subject_orig, function(x) grepl(paste0(cc, ":"), x, fixed=TRUE)))
metadata <- metadata[has_custom_clustering_annotation,]
}}
# don't deduplicate if params$deduplicate_base is set to FALSE
if (!is.null(params$deduplicate_base) && params$deduplicate_base != FALSE) {
# log to skip deduplication
blog$info(paste("vis_id:", .GlobalEnv$VIS_ID, "Deduplication skipped"))
} else {
while (nrow(metadata) - sum(metadata$is_duplicate) < limit && attr(res_raw, "numFound") > offset+120 && r < req_limit) {
offset <- offset+120
res_raw <- get_raw_data(limit,
base_query,
return_fields,
sortby_string,
filter,
repo,
coll,
retry_opts,
offset,
non_public)
res <- bind_rows(res, res_raw$docs)
metadata <- etl(res, repo, non_public)
metadata <- unique(metadata, by = "id")
metadata <- sanitize_abstract(metadata)
metadata <- mark_duplicates(metadata)
metadata$has_dataset <- unlist(lapply(metadata$resulttype, function(x) "Dataset" %in% x))
# check if custom clustering annotation param is in metadata
if (!is.null(cc)) {
if (!(cc %in% names(fieldmapper))) {
has_custom_clustering_annotation <- unlist(lapply(metadata$subject_orig, function(x) grepl(paste0(cc, ":"), x, fixed=TRUE)))
metadata <- metadata[has_custom_clustering_annotation,]
}}
r <- r+1
}
}
# check if custom clustering annotation param is in metadata
if (!is.null(cc)) {
if (!(cc %in% names(fieldmapper))) {
has_custom_clustering_annotation <- unlist(lapply(metadata$subject_orig, function(x) grepl(paste0(cc, ":"), x, fixed=TRUE)))
metadata <- metadata[has_custom_clustering_annotation,]
}}
blog$info(paste("vis_id:", .GlobalEnv$VIS_ID, "Deduplication retrieval requests:", r))
metadata <- unique(metadata, by = "id")
# Add all keywords, including classification to text content for clustering
text <- data.frame(id = metadata$id,
content = paste(metadata$title, metadata$paper_abstract,
metadata$subject_orig, metadata$published_in, metadata$authors,
sep=" "))
input_data=list("metadata" = metadata, "text"=text)
end.time <- Sys.time()
time.taken <- end.time - start.time
blog$info(paste("vis_id:", .GlobalEnv$VIS_ID, "Time taken:", time.taken, sep=" "))
return(input_data)
}
etl <- function(res, repo, non_public) {
metadata = data.frame(matrix(nrow=length(res$dcdocid)))
metadata$id = res$dcdocid
metadata$relation = check_metadata(res$dcrelation)
metadata$identifier = check_metadata(res$dcidentifier)
metadata$title = check_metadata(res$dctitle)
metadata$title = gsub(" \\.\\.\\.$", "", metadata$title)
metadata$paper_abstract = check_metadata(res$dcdescription)
metadata$published_in = check_metadata(res$dcsource)
metadata$year = check_metadata(res$dcdate)
subject_all = check_metadata(res$dcsubject)
metadata$subject_orig = subject_all
subject_cleaned = gsub("DOAJ:[^;]*(;|$)?", "", subject_all) # remove DOAJ classification
subject_cleaned = gsub("/dk/atira[^;]*(;|$)?", "", subject_cleaned) # remove atira classification
subject_cleaned = gsub("ddc:[0-9]+(;|$)?", "", subject_cleaned) # remove Dewey Decimal Classification
subject_cleaned = gsub("([\\w\\/\\:-])*?\\/ddc\\/([\\/0-9\\.])*", "", subject_cleaned) # remove Dewey Decimal Classification in URI form
subject_cleaned = gsub("[A-Z,0-9]{2,}-[A-Z,0-9\\.]{2,}(;|$)?", "", subject_cleaned) #remove LOC classification
subject_cleaned = gsub("[^\\(;]+\\(General\\)(;|$)?", "", subject_cleaned) # remove general subjects
subject_cleaned = gsub("[^\\(;]+\\(all\\)(;|$)?", "", subject_cleaned) # remove general subjects
subject_cleaned = gsub("[^:;]+ ?:: ?[^;]+(;|$)?", "", subject_cleaned) #remove classification with separator ::
subject_cleaned = gsub("[^\\[;]+\\[[A-Z,0-9]+\\](;|$)?", "", subject_cleaned) # remove WHO classification
subject_cleaned = gsub("Info:\\w+-(\\w+\\/)+", "", subject_cleaned) # remove Info:eu-repo/classification/
subject_cleaned = gsub("([A-Za-z]+:[A-Za-z0-9 \\/\\.-]+);?", "", subject_cleaned, perl=TRUE) # clean up annotations with prefix e.g. theme:annotation
if (!is.null(params$vis_type) && params$vis_type == "timeline") {
subject_cleaned = gsub("FOS ", "", subject_cleaned) # remove FOS classification tag, but keep classifcation name
arxiv_classification_string = "(cs|econ|eess|math|astro-ph|nlin|q-bio|q-fin|stat)\\.[A-Z]{2}|cond-mat\\.[a-z\\-]+|hep-(ex|lat|ph|th)|math-ph|nucl-(ex|th)|physics\\.[a-z\\-]+|(astro-ph|gr-qc|quant-ph|cond-mat)"
subject_cleaned = gsub(arxiv_classification_string, "", subject_cleaned, perl=TRUE) # remove arXiv classification short code, but keep classifcation name
} else {
subject_cleaned = gsub("FOS [A-Za-z ]+", "", subject_cleaned) # remove FOS classifications (Fields of Science and Technology)
arxiv_classification_string = "(([A-Za-z ]+ )?cond-mat\\.[a-z\\-]+)|([\\w ]+ )?(cs|econ|eess|math|astro-ph|nlin|q-bio|q-fin|stat)\\.[A-Z]{2}|cond-mat\\.[a-z\\-]+|hep-(ex|lat|ph|th)|math-ph|nucl-(ex|th)|physics\\.[a-z\\-]+|([\\w ]+ )(astro-ph|gr-qc|quant-ph|cond-mat)"
subject_cleaned = gsub(arxiv_classification_string, "", subject_cleaned, perl=TRUE) # remove arXiv classification, except on streamgraphs
}
subject_cleaned = gsub("([A-Za-z]+:[A-Za-z0-9 \\/\\.]+);?", "", subject_cleaned, perl=TRUE) # clean up annotations with prefix e.g. theme:annotation
subject_cleaned = gsub("(wikidata)?\\.org/entity/[qQ]([\\d]+)?", "", subject_cleaned) # remove wikidata classification
subject_cleaned = gsub("</keyword><keyword>", "", subject_cleaned) # remove </keyword><keyword>
subject_cleaned = gsub("\\[No keyword\\]", "", subject_cleaned)
if (!is.null(params$vis_type) && params$vis_type == "timeline") {
subject_cleaned = remove_keywords_with_text_in_square_brackets(subject_cleaned)
} else {
subject_cleaned = remove_text_in_square_brackets_from_keywords(subject_cleaned)
}
subject_cleaned = gsub("\\[[^\\[]+\\][^\\;]+(;|$)?", "", subject_cleaned) # remove classification
subject_cleaned = gsub("[0-9]{2,} [A-Z]+[^;]*(;|$)?", "", subject_cleaned) #remove classification
subject_cleaned = gsub(" -- ", "; ", subject_cleaned) #replace inconsistent keyword separation
subject_cleaned = gsub("[-]{2,}", "; ", subject_cleaned) #replace inconsistent keyword separation
subject_cleaned = gsub("[A-Z]\\.\\d\\.\\d+", "", subject_cleaned) #replace inconsistent keyword separation
subject_cleaned = gsub(" \\( ", "; ", subject_cleaned) #replace inconsistent keyword separation
subject_cleaned = gsub("(\\w* \\w*(\\.)( \\w* \\w*)?)", "; ", subject_cleaned) # remove overly broad keywords separated by .
subject_cleaned = gsub("\\. ", "; ", subject_cleaned) # replace inconsistent keyword separation
subject_cleaned = gsub(" ?\\d[:?-?]?(\\d+.)+", "", subject_cleaned) # replace residuals like 5:621.313.323 or '5-76.95'
subject_cleaned = gsub(": ", "", subject_cleaned) # clean up keyword separation
subject_cleaned = gsub("^; $", "", subject_cleaned) # clean up keyword separation
subject_cleaned = gsub(";+", ";", subject_cleaned) # clean up keyword separation
subject_cleaned = gsub(",+", ",", subject_cleaned) # clean up keyword separation
subject_cleaned = gsub(",", ", ", subject_cleaned) # clean up keyword separation
subject_cleaned = gsub("\\s+", " ", subject_cleaned) # clean up keyword separation
subject_cleaned = stringi::stri_trim(subject_cleaned) # clean up keyword separation
metadata$subject = subject_cleaned
metadata$authors = check_metadata(res$dccreator)
metadata$link = check_metadata(res$dclink)
metadata$oa_state = res$dcoa
metadata$url = metadata$id
metadata$relevance = c(nrow(metadata):1)
metadata$resulttype = lapply(res$dctypenorm, decode_dctypenorm)
metadata$type = check_metadata(res$dctype)
metadata$typenorm = check_metadata(res$dctypenorm)
metadata$doi = unlist(lapply(metadata$link, find_dois))
metadata$lang = check_metadata(res$dclang)
metadata$language = check_metadata(res$dclanguage)
metadata$content_provider = check_metadata(res$dcprovider)
metadata$coverage = check_metadata(res$dccoverage)
if(repo=="fttriple" && non_public==TRUE) {
metadata$content_provider <- "GoTriple"
}
return (metadata)
}
preprocess_query <- function(query) {
# remove pluses between terms
query_wt_plus = gsub("(?!\\B\"[^\"]*)[\\+]+(?![^\"]*\"\\B)", " ", query, perl=T)
# remove multiple minuses and spaces after minuses
query_wt_multi_minus = gsub("(?!\\B\"[^\"]*)((^|\\s))[\\-]+[\\s]*(?![^\"]*\"\\B)", "\\1-", query_wt_plus, perl=T)
# remove multiple spaces inside the query
query_wt_multi_spaces = gsub("(?!\\B\"[^\"]*)[\\s]{2,}(?![^\"]*\"\\B)", " ", query_wt_multi_minus, perl=T)
# trim query, if needed
query_cleaned = gsub("^\\s+|\\s+$", "", query_wt_multi_spaces, perl=T)
# add "textus:" to each word/phrase to enable verbatim search
# make sure it is added after any opening parentheses to enable queries such as "(a and b) or (a and c)"
exact_query = gsub('([\"]+(.*?)[\"]+)|(?<=\\(\\b|\\+|-\\"\\b|\\s-\\b|^-\\b)|(?!or\\b|and\\b|[-]+[\\"\\(]*\\b)(?<!\\S)(?=\\S)(?!\\(|\\+)'
, "textus:\\1", query_cleaned, perl=T)
return(exact_query)
}
get_raw_data <- function(limit, base_query, return_fields, sortby_string, filter, repo, coll, retry_opts, offset, non_public) {
t <- 0
while (t < retry_opts$times) {
res_raw <- try(
(bs_search(hits=limit
, fields = return_fields,
, query = base_query
, sortby = sortby_string
, filter = filter
, target = repo
, coll = coll
, retry = retry_opts
, offset = offset
, non_public = non_public)))
if (inherits(res_raw, "try-error")) {
if (grepl("Timeout was reached: [api.base-search.net]", res_raw, fixed=TRUE)) {
t <- t + 1
Sys.sleep(2)
blog$info(paste("vis_id:", .GlobalEnv$VIS_ID, "BASE API Timeout retry attempt:", t, sep=" "))
} else {
stop("Timeout was reached: [api.base-search.net]")
}
} else {
break
}
}
return(res_raw)
}
find_dois <- function(link) {
if ((startsWith(link, "http://doi.org"))
|| (startsWith(link, "https://doi.org"))
|| (startsWith(link, "http://dx.doi.org"))
|| (startsWith(link, "https://dx.doi.org"))) {
doi <- str_replace(link, "http:", "https:")
} else {
doi <- ""
}
return(doi)
}
decode_dctypenorm <- function(dctypestring) {
typecodes <- strsplit(dctypestring, "; ")
typecodes <- lapply(typecodes, function(x) {dctypenorm_decoder[x]})
typecodes <- unlist(unname(typecodes[[1]]))
return(typecodes)
}
remove_keywords_with_text_in_square_brackets <- function(x) {
# This function removes whole keywords that contain text in square brackets.
# Example: 'Climate [MeSH]' | 'Some keywords [Chemical]'.
gsub("[^;]*\\[[^]]+\\][^;]*;?", "", x)
}
remove_text_in_square_brackets_from_keywords <- function(x) {
# This function removes text in square brackets.
# Example: 'Climate [MeSH]' -> 'Climate'| 'Some keywords [Chemical]' -> 'Some keywords'.
gsub("\\[[^]]*\\]", "", x)
}
dctypenorm_decoder <- list(
"4"="Audio",
"11"="Book",
"111"="Book part",
"13"="Conference object",
"16"="Course material",
"7"="Dataset",
"5"="Image/video",
"12"="Journal/newspaper",
"121"="Journal/newspaper article",
"122"="Journal/newspaper other content",
"17"="Lecture",
"19"="Manuscript",
"3"="Map",
"52"="Moving image/video",
"2"="Musical notation",
"F"="Other/Unknown material",
"1A"="Patent",
"14"="Report",
"15"="Review",
"6"="Software",
"51"="Still image",
"1"="Text",
"18"="Thesis",
"181"="Thesis: bachelor",
"183"="Thesis: doctoral and postdoctoral",
"182"="Thesis: master"
)
fieldmapper <- list(
"relation"="dcrelation",
"identifier"="identifier",
"title"="dctitle",
"paper_abstract"="dcdescription",
"published_in"="dcsource",
"year"="dcdate",
"subject"="dcsubject",
"authors"="dccreator",
"link"="dclink",
"oa_state"="dcoa",
"url"="dcdocid",
"relevance"="relevance",
"resulttype"="dctypenorm",
"type"="dctype",
"typenorm"="dctypenorm",
"doi"="doi",
"lang"="dclang",
"language"="dclanguage",
"content_provider"="dcprovider",
"coverage"="dccoverage"
)