Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 1 | #' @import jsonlite |
| 2 | #' @import curl |
Marc Kupietz | d235a71 | 2019-09-16 18:31:33 +0200 | [diff] [blame] | 3 | #' @import utils |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 4 | |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 5 | defaultFields <- c("corpusSigle", "textSigle", "pubDate", "pubPlace", |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 6 | "availability", "textClass", "snippet") |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 7 | |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 8 | contentFields <- c("snippet") |
| 9 | |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 10 | maxResultsPerPage <- 50; |
| 11 | |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 12 | QueryParameterFromUrl <- function(url, parameter) { |
| 13 | regex <- paste0(".*[?&]", parameter, "=([^&]*).*") |
| 14 | if (grepl(regex, url)) { |
| 15 | return(gsub(regex, '\\1', url, perl = TRUE)) |
| 16 | } else { |
| 17 | return("") |
| 18 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 19 | } |
| 20 | |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 21 | KorAPQueryStringFromUrl <- function(KorAPUrl) { |
| 22 | return(URLdecode(gsub(".*[?&]q=([^&]*).*", '\\1', KorAPUrl, perl = TRUE))) |
| 23 | } |
| 24 | |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 25 | #' Send a query to a KorAP connection. |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 26 | #' @param con object obtained from \code{\link{KorAPConnection}}, that contains all necessary connection information |
| 27 | #' @param query string that contains the corpus query. The query langauge depends on the \code{ql} parameter. Either \code{query} must be provided or \code{KorAPUrl} |
| 28 | #' @param vc string describing the virtual corpus in which the query should be performed. An empty string (default) means the whole corpus, as far as it is license-wise accessible. |
| 29 | #' @param KorAPUrl instead of providing the query and vc string parameters, you can also simply copy a KorAP query URL from your browser and use it here (and in \code{KorAPConnection}) to provide all necessary information for the query. |
Marc Kupietz | b125bdd | 2019-09-09 12:05:59 +0200 | [diff] [blame] | 30 | #' @param metadataOnly boolean that determines whether queries should return only metadata without any snippets. This can also be useful to prevent access rewrites. Note that the default value is TRUE, unless the connection is authorized (currently not possible). |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 31 | #' @param ql string to choose the query language (see \href{https://github.com/KorAP/Kustvakt/wiki/Service:-Search-GET#user-content-parameters}{section on Query Parameters} in the Kustvakt-Wiki for possible values. |
Marc Kupietz | b125bdd | 2019-09-09 12:05:59 +0200 | [diff] [blame] | 32 | #' @param fields (meta)data fields that will be fetched for every match |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 33 | #' @return A KorAP query object that, among other information, contains the total number of results in \code{$meta$totalResults}. The resulting object can be used to fetch all query results (with \code{\link{KorAPFetchAll}}) or the next page of results (with \code{\link{KorAPFetchNext}}). |
| 34 | #' A correspunding URL to be used within a web browser is contained in \code{$webUIRequestUrl} |
| 35 | #' Please make sure to check \code{$collection$rewrites} to see if any unforseen access rewrites of the query's virtual corpus had to be performed. |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 36 | #' |
| 37 | #' @examples |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 38 | #' con <- KorAPConnection() |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 39 | #' q <- KorAPQuery(con, "Ameisenplage") |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 40 | #' q <- KorAPFetchAll(q) |
| 41 | #' summary(q$collectedMatches) |
| 42 | #' |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 43 | #' q <- KorAPQuery(con, KorAPUrl = "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp&cutoff=1") |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 44 | #' q$meta$totalResults |
| 45 | #' |
| 46 | #' @seealso \code{\link{KorAPConnection}}, \code{\link{KorAPFetchNext}}, \code{\link{KorAPFetchRest}}, \code{\link{KorAPFetchAll}}, \code{\link{KorAPCorpusStats}} |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 47 | #' |
| 48 | #' @references |
| 49 | #' \url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026} |
| 50 | #' |
| 51 | #' @export |
Marc Kupietz | f568f3f | 2019-09-16 17:03:46 +0200 | [diff] [blame] | 52 | KorAPQuery <- function(con, query, vc="", KorAPUrl, metadataOnly = TRUE, ql = "poliqarp", fields = defaultFields, |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 53 | accessRewriteFatal = TRUE) { |
Marc Kupietz | f568f3f | 2019-09-16 17:03:46 +0200 | [diff] [blame] | 54 | if (missing(query) && missing(KorAPUrl) || ! (missing(query) || missing(KorAPUrl))) { |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 55 | stop("Exactly one of the parameters query and KorAPUrl must be specified.") |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 56 | } |
Marc Kupietz | f568f3f | 2019-09-16 17:03:46 +0200 | [diff] [blame] | 57 | if (missing(query)) { |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 58 | query <- QueryParameterFromUrl(KorAPUrl, "q") |
| 59 | vc <- QueryParameterFromUrl(KorAPUrl, "vc") |
| 60 | ql <- QueryParameterFromUrl(KorAPUrl, "ql") |
| 61 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 62 | request <- paste0('?q=', URLencode(query, reserved=TRUE), |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 63 | ifelse(vc != '', paste0('&vc=', URLencode(vc, reserved=TRUE)), ''), |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 64 | '&ql=', ql); |
| 65 | webUIRequestUrl <- paste0(con$KorAPUrl, request) |
| 66 | requestUrl <- paste0(con$apiUrl, 'search', request, |
| 67 | '&fields=', paste(defaultFields, collapse = ","), |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 68 | ifelse(metadataOnly, '&access-rewrite-disabled=true', '')) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 69 | result <- fromJSON(paste0(requestUrl, '&count=1')) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 70 | result$fields <- fields[!fields %in% contentFields] |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 71 | result$requestUrl <- requestUrl |
| 72 | result$request <- request |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 73 | result$vc <- vc |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 74 | result$webUIRequestUrl <- webUIRequestUrl |
Marc Kupietz | cb725f8 | 2019-08-30 18:04:57 +0200 | [diff] [blame] | 75 | result$nextStartIndex <- 0 |
| 76 | result$hasMoreMatches <- (result$meta$totalResults > 0) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 77 | return(result) |
| 78 | } |
| 79 | |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 80 | #' Fetch the next bunch of results of a KorAP query. |
| 81 | #' @param queryObject object obtained from \code{\link{KorAPQuery}} |
| 82 | #' @param offset start offset for query results to fetch |
| 83 | #' @param maxFetch maximum number of query results to fetch |
| 84 | #' @param verbose |
| 85 | #' @return The \code{queryObject} input parameter with updated fields \code{$collectedMatches}, \code{$matches} (latest bunch only), \code{$nextStartIndex}, , \code{$hasMoreMatches} |
| 86 | #' |
| 87 | #' @examples |
| 88 | #' q <- KorapFetchNext(KorAPQuery(KorAPConnection(), "Ameisenplage")) |
| 89 | #' |
| 90 | #' @seealso \code{\link{KorAPFetchRest}}, \code{\link{KorAPFetchAll}} |
| 91 | #' |
| 92 | #' @references |
| 93 | #' \url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026} |
| 94 | #' |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 95 | #' @export |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 96 | KorAPFetchNext <- function(queryObject, offset = queryObject$nextStartIndex, maxFetch = maxResultsPerPage, verbose = FALSE) { |
| 97 | if (queryObject$meta$totalResults == 0 || offset >= queryObject$meta$totalResults) { |
| 98 | return(queryObject) |
| 99 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 100 | |
| 101 | page <- 1 |
| 102 | results <- 0 |
| 103 | |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 104 | collectedMatches <- queryObject$collectedMatches |
| 105 | |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 106 | repeat { |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 107 | res <- fromJSON(paste0(queryObject$requestUrl, '&count=', min(ifelse(!is.na(maxFetch), maxFetch - results, maxResultsPerPage), maxResultsPerPage) ,'&offset=', offset + results)) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 108 | if (res$meta$totalResults == 0) { return(data.frame()) } |
Marc Kupietz | b306552 | 2019-09-09 11:34:19 +0200 | [diff] [blame] | 109 | for (field in queryObject$fields) { |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 110 | if (!field %in% colnames(res$matches)) { |
| 111 | res$matches[, field] <- NA |
| 112 | } |
| 113 | } |
Marc Kupietz | b306552 | 2019-09-09 11:34:19 +0200 | [diff] [blame] | 114 | currentMatches <- res$matches[queryObject$fields] |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 115 | factorCols <- colnames(subset(currentMatches, select=-c(pubDate))) |
| 116 | currentMatches[factorCols] <- lapply(currentMatches[factorCols], factor) |
| 117 | currentMatches$pubDate = as.Date(currentMatches$pubDate, format = "%Y-%m-%d") |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 118 | if (!is.list(collectedMatches)) { |
| 119 | collectedMatches <- currentMatches |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 120 | } else { |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 121 | collectedMatches <- rbind(collectedMatches, currentMatches) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 122 | } |
Marc Kupietz | c2c59bd | 2019-08-30 16:50:49 +0200 | [diff] [blame] | 123 | if (verbose) { |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 124 | cat(paste0("Retrieved page: ", page, "/", ceiling((res$meta$totalResults) / res$meta$itemsPerPage), ': ', res$meta$benchmark, '\n')) |
Marc Kupietz | c2c59bd | 2019-08-30 16:50:49 +0200 | [diff] [blame] | 125 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 126 | page <- page + 1 |
| 127 | results <- results + res$meta$itemsPerPage |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 128 | if (offset + results >= res$meta$totalResults || (!is.na(maxFetch) && results >= maxFetch)) { |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 129 | break |
| 130 | } |
| 131 | } |
Marc Kupietz | cb725f8 | 2019-08-30 18:04:57 +0200 | [diff] [blame] | 132 | res$nextStartIndex <- res$meta$startIndex + res$meta$itemsPerPage |
Marc Kupietz | b306552 | 2019-09-09 11:34:19 +0200 | [diff] [blame] | 133 | res$fields <- queryObject$fields |
| 134 | res$requestUrl <- queryObject$requestUrl |
| 135 | res$request <- queryObject$request |
| 136 | res$webUIRequestUrl <- queryObject$webUIRequestUrl |
Marc Kupietz | cb725f8 | 2019-08-30 18:04:57 +0200 | [diff] [blame] | 137 | res$hasMoreMatches <- (res$meta$totalResults > res$nextStartIndex) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 138 | res$collectedMatches <- collectedMatches |
Marc Kupietz | cb725f8 | 2019-08-30 18:04:57 +0200 | [diff] [blame] | 139 | return(res) |
| 140 | } |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 141 | |
| 142 | #' Fetch all results of a KorAP query. |
| 143 | #' @param queryObject object obtained from \code{\link{KorAPQuery}} |
| 144 | #' @param verbose |
| 145 | #' @return The \code{queryObject} input parameter with updated fields \code{$collectedMatches}, \code{$matches} (latest bunch only), \code{$nextStartIndex}, \code{$hasMoreMatches} |
| 146 | #' |
| 147 | #' @examples |
| 148 | #' q <- KorAPFetchAll(KorAPQuery(KorAPConnection(), "Ameisenplage")) |
| 149 | #' q$collectedMatches |
| 150 | #' |
| 151 | #' @seealso \code{\link{KorAPFetchRest}}, \code{\link{KorAPFetchNext}} |
| 152 | #' |
| 153 | #' @references |
| 154 | #' \url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026} |
| 155 | #' |
| 156 | #' @export |
| 157 | KorAPFetchAll <- function(queryObject, verbose = FALSE) { |
| 158 | return(KorAPFetchNext(queryObject, offset = 0, maxFetch = NA, verbose = verbose)) |
| 159 | } |
| 160 | |
| 161 | #' Fetches all remaining results of a KorAP query. |
| 162 | #' @param queryObject object obtained from \code{\link{KorAPQuery}} |
| 163 | #' @param verbose |
| 164 | #' @return The \code{queryObject} input parameter with updated fields \code{$collectedMatches}, \code{$matches} (latest bunch only), \code{$nextStartIndex}, \code{$hasMoreMatches} |
| 165 | #' |
| 166 | #' @examples |
| 167 | #' q <- KorAPFetchRest(KorAPQueryNext(KorAPQuery(KorAPConnection(), "Ameisenplage"))) |
| 168 | #' q$collectedMatches |
| 169 | #' |
| 170 | #' @seealso \code{\link{KorAPFetchAll}}, \code{\link{KorAPFetchNext}} |
| 171 | #' |
| 172 | #' @references |
| 173 | #' \url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026} |
| 174 | #' |
| 175 | #' @export |
| 176 | KorAPFetchRest <- function(queryObject, verbose = FALSE) { |
| 177 | return(KorAPFetchNext(queryObject, maxFetch = NA, verbose = verbose)) |
| 178 | } |