Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 1 | #' Class KorAPQuery |
| 2 | #' |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame] | 3 | #' This class provides methods to perform different kinds of queries on the KorAP API server. |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 4 | #' `KorAPQuery` objects, which are typically created by the [corpusQuery()] method, |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame] | 5 | #' represent the current state of a query to a KorAP server. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 6 | #' |
| 7 | #' @include KorAPConnection.R |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 8 | #' @import httr |
| 9 | #' |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame] | 10 | #' @include RKorAPClient-package.R |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 11 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 12 | #' @export |
| 13 | KorAPQuery <- setClass("KorAPQuery", slots = c( |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 14 | "korapConnection", |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 15 | "request", |
| 16 | "vc", |
| 17 | "totalResults", |
| 18 | "nextStartIndex", |
| 19 | "fields", |
| 20 | "requestUrl", |
| 21 | "webUIRequestUrl", |
| 22 | "apiResponse", |
| 23 | "collectedMatches", |
| 24 | "hasMoreMatches" |
| 25 | )) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 26 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 27 | #' Method initialize |
| 28 | #' |
| 29 | #' @rdname KorAPQuery-class |
| 30 | #' @param .Object … |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 31 | #' @param korapConnection KorAPConnection object |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 32 | #' @param request query part of the request URL |
| 33 | #' @param vc definition of a virtual corpus |
| 34 | #' @param totalResults number of hits the query has yielded |
| 35 | #' @param nextStartIndex at what index to start the next fetch of query results |
| 36 | #' @param fields what data / metadata fields should be collected |
| 37 | #' @param requestUrl complete URL of the API request |
| 38 | #' @param webUIRequestUrl URL of a web frontend request corresponding to the API request |
| 39 | #' @param apiResponse data-frame representation of the JSON response of the API request |
Marc Kupietz | 7776dec | 2019-09-27 16:59:02 +0200 | [diff] [blame] | 40 | #' @param hasMoreMatches logical that signals if more query results can be fetched |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 41 | #' @param collectedMatches matches already fetched from the KorAP-API-server |
Marc Kupietz | 97a1bca | 2019-10-04 22:52:09 +0200 | [diff] [blame] | 42 | #' |
| 43 | #' @importFrom tibble tibble |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 44 | #' @export |
| 45 | setMethod("initialize", "KorAPQuery", |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 46 | function(.Object, korapConnection = NULL, request = NULL, vc="", totalResults=0, nextStartIndex=0, fields=c("corpusSigle", "textSigle", "pubDate", "pubPlace", |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 47 | "availability", "textClass", "snippet"), |
| 48 | requestUrl="", webUIRequestUrl = "", apiResponse = NULL, hasMoreMatches= FALSE, collectedMatches = NULL) { |
| 49 | .Object <- callNextMethod() |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 50 | .Object@korapConnection = korapConnection |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 51 | .Object@request = request |
| 52 | .Object@vc = vc |
| 53 | .Object@totalResults = totalResults |
| 54 | .Object@nextStartIndex = nextStartIndex |
| 55 | .Object@fields = fields |
| 56 | .Object@requestUrl = requestUrl |
| 57 | .Object@webUIRequestUrl = webUIRequestUrl |
| 58 | .Object@apiResponse = apiResponse |
| 59 | .Object@hasMoreMatches = hasMoreMatches |
| 60 | .Object@collectedMatches = collectedMatches |
| 61 | .Object |
| 62 | }) |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 63 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 64 | setGeneric("corpusQuery", function(kco, ...) standardGeneric("corpusQuery") ) |
| 65 | setGeneric("fetchAll", function(kqo, ...) standardGeneric("fetchAll") ) |
| 66 | setGeneric("fetchNext", function(kqo, ...) standardGeneric("fetchNext") ) |
| 67 | setGeneric("fetchRest", function(kqo, ...) standardGeneric("fetchRest") ) |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 68 | setGeneric("frequencyQuery", function(kco, ...) standardGeneric("frequencyQuery") ) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 69 | |
| 70 | maxResultsPerPage <- 50 |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 71 | |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 72 | ## quiets concerns of R CMD check re: the .'s that appear in pipelines |
| 73 | if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 74 | |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 75 | #' Corpus query |
| 76 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 77 | #' **`corpusQuery`** performs a corpus query via a connection to a KorAP-API-server |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 78 | #' |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 79 | #' @rdname KorAPQuery-class |
| 80 | #' @aliases corpusQuery |
| 81 | #' |
| 82 | #' @importFrom urltools url_encode |
| 83 | #' @importFrom purrr pmap |
| 84 | #' @importFrom dplyr bind_rows |
| 85 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 86 | #' @param kco [KorAPConnection()] object (obtained e.g. from `new("KorAPConnection")` |
| 87 | #' @param query string that contains the corpus query. The query language depends on the `ql` parameter. Either `query` must be provided or `KorAPUrl`. |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 88 | #' @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. |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 89 | #' @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 `KorAPConnection`) to provide all necessary information for the query. |
Marc Kupietz | 7776dec | 2019-09-27 16:59:02 +0200 | [diff] [blame] | 90 | #' @param metadataOnly logical 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 | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 91 | #' @param ql string to choose the query language (see [section on Query Parameters](https://github.com/KorAP/Kustvakt/wiki/Service:-Search-GET#user-content-parameters) in the Kustvakt-Wiki for possible values. |
Akron | 5e13546 | 2019-09-27 16:31:38 +0200 | [diff] [blame] | 92 | #' @param fields (meta)data fields that will be fetched for every match. |
Marc Kupietz | 43a6ade | 2020-02-18 17:01:44 +0100 | [diff] [blame] | 93 | #' @param accessRewriteFatal abort if query or given vc had to be rewritten due to insufficient rights (not yet implemented). |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 94 | #' @param verbose print some info |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 95 | #' @param as.df return result as data frame instead of as S4 object? |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 96 | #' @param expand logical that decides if `query` and `vc` parameters are expanded to all of their combinations |
| 97 | #' @return Depending on the `as.df` parameter, a table or a [KorAPQuery()] object that, among other information, contains the total number of results in `@totalResults`. The resulting object can be used to fetch all query results (with [fetchAll()]) or the next page of results (with [fetchNext()]). |
| 98 | #' A corresponding URL to be used within a web browser is contained in `@webUIRequestUrl` |
| 99 | #' Please make sure to check `$collection$rewrites` to see if any unforeseen access rewrites of the query's virtual corpus had to be performed. |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 100 | #' |
| 101 | #' @examples |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 102 | #' # Fetch metadata of every query hit for "Ameisenplage" and show a summary |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 103 | #' \donttest{ |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 104 | #' new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll() |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 105 | #' } |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 106 | #' |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 107 | #' # Use the copy of a KorAP-web-frontend URL for an API query of "Ameise" in a virtual corpus |
| 108 | #' # and show the number of query hits (but don't fetch them). |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 109 | #' |
| 110 | #' new("KorAPConnection", verbose = TRUE) %>% |
| 111 | #' corpusQuery(KorAPUrl = |
| 112 | #' "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp") |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 113 | #' |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 114 | #' # Plot the time/frequency curve of "Ameisenplage" |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 115 | #' \donttest{ |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 116 | #' new("KorAPConnection", verbose=TRUE) %>% |
| 117 | #' { . ->> kco } %>% |
| 118 | #' corpusQuery("Ameisenplage") %>% |
| 119 | #' fetchAll() %>% |
| 120 | #' slot("collectedMatches") %>% |
| 121 | #' mutate(year = lubridate::year(pubDate)) %>% |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 122 | #' dplyr::select(year) %>% |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 123 | #' group_by(year) %>% |
Marc Kupietz | cb3c59e | 2020-06-02 10:10:43 +0200 | [diff] [blame] | 124 | #' summarise(Count = dplyr::n()) %>% |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 125 | #' mutate(Freq = mapply(function(f, y) |
| 126 | #' f / corpusStats(kco, paste("pubDate in", y))@tokens, Count, year)) %>% |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 127 | #' dplyr::select(-Count) %>% |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 128 | #' complete(year = min(year):max(year), fill = list(Freq = 0)) %>% |
| 129 | #' plot(type = "l") |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 130 | #' } |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 131 | #' @seealso [KorAPConnection()], [fetchNext()], [fetchRest()], [fetchAll()], [corpusStats()] |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 132 | #' |
| 133 | #' @references |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 134 | #' <https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026> |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 135 | #' |
| 136 | #' @export |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 137 | setMethod("corpusQuery", "KorAPConnection", |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 138 | function(kco, |
| 139 | query = if (missing(KorAPUrl)) |
| 140 | stop("At least one of the parameters query and KorAPUrl must be specified.", call. = FALSE) |
| 141 | else |
| 142 | httr::parse_url(KorAPUrl)$query$q, |
| 143 | vc = if (missing(KorAPUrl)) "" else httr::parse_url(KorAPUrl)$query$cq, |
| 144 | KorAPUrl, |
| 145 | metadataOnly = TRUE, |
| 146 | ql = if (missing(KorAPUrl)) "poliqarp" else httr::parse_url(KorAPUrl)$query$ql, |
| 147 | fields = c( |
| 148 | "corpusSigle", |
| 149 | "textSigle", |
| 150 | "pubDate", |
| 151 | "pubPlace", |
| 152 | "availability", |
| 153 | "textClass", |
| 154 | "snippet" |
| 155 | ), |
| 156 | accessRewriteFatal = TRUE, |
| 157 | verbose = kco@verbose, |
| 158 | expand = length(vc) != length(query), |
| 159 | as.df = FALSE) { |
| 160 | if (length(query) > 1 || length(vc) > 1) { |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 161 | grid <- if (expand) expand_grid(query=query, vc=vc) else tibble(query=query, vc=vc) |
| 162 | purrr::pmap(grid, function(query, vc, ...) |
| 163 | corpusQuery(kco, query=query, vc=vc, ql=ql, verbose=verbose, as.df = TRUE)) %>% |
| 164 | bind_rows() |
| 165 | } else { |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 166 | contentFields <- c("snippet") |
| 167 | if (metadataOnly) { |
| 168 | fields <- fields[!fields %in% contentFields] |
| 169 | } |
| 170 | request <- |
| 171 | paste0('?q=', |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 172 | url_encode(enc2utf8(query)), |
| 173 | ifelse (vc != '', paste0('&cq=', url_encode(enc2utf8(vc))), ''), '&ql=', ql) |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 174 | webUIRequestUrl <- paste0(kco@KorAPUrl, request) |
| 175 | requestUrl <- paste0( |
| 176 | kco@apiUrl, |
| 177 | 'search', |
| 178 | request, |
| 179 | '&fields=', |
| 180 | paste(fields, collapse = ","), |
| 181 | if (metadataOnly) '&access-rewrite-disabled=true' else '' |
| 182 | ) |
| 183 | log.info(verbose, "Searching \"", query, "\" in \"", vc, "\"", sep = |
| 184 | "") |
| 185 | res = apiCall(kco, paste0(requestUrl, '&count=0')) |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 186 | log.info(verbose, ": ", res$meta$totalResults, " hits") |
Marc Kupietz | f5769b6 | 2019-12-13 09:19:45 +0100 | [diff] [blame] | 187 | if(!is.null(res$meta$cached)) |
| 188 | log.info(verbose, " [cached]\n") |
| 189 | else |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 190 | log.info(verbose, ", took ", res$meta$benchmark, "\n", sep = "") |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 191 | if (as.df) |
| 192 | data.frame( |
| 193 | query = query, |
| 194 | totalResults = res$meta$totalResults, |
| 195 | vc = vc, |
| 196 | webUIRequestUrl = webUIRequestUrl, |
| 197 | stringsAsFactors = FALSE |
| 198 | ) |
| 199 | else |
| 200 | KorAPQuery( |
| 201 | korapConnection = kco, |
| 202 | nextStartIndex = 0, |
| 203 | fields = fields, |
| 204 | requestUrl = requestUrl, |
| 205 | request = request, |
| 206 | totalResults = res$meta$totalResults, |
| 207 | vc = vc, |
| 208 | apiResponse = res, |
| 209 | webUIRequestUrl = webUIRequestUrl, |
| 210 | hasMoreMatches = (res$meta$totalResults > 0), |
| 211 | ) |
| 212 | } |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 213 | }) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 214 | |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 215 | #' Fetch the next bunch of results of a KorAP query. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 216 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 217 | #' **`fetchNext`** fetches the next bunch of results of a KorAP query. |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 218 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 219 | #' @param kqo object obtained from [corpusQuery()] |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 220 | #' @param offset start offset for query results to fetch |
| 221 | #' @param maxFetch maximum number of query results to fetch |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 222 | #' @param verbose print progress information if true |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 223 | #' @param randomizePageOrder fetch result pages in pseudo random order if true. Use [set.seed()] to set seed for reproducible results. |
| 224 | #' @return The `kqo` input object with updated slots `collectedMatches`, `apiResponse`, `nextStartIndex`, `hasMoreMatches` |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 225 | #' |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 226 | #' @examples |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 227 | #' \donttest{q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchNext() |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 228 | #' q@collectedMatches |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 229 | #' } |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 230 | #' |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 231 | #' @references |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 232 | #' <https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026> |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 233 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 234 | #' @aliases fetchNext |
| 235 | #' @rdname KorAPQuery-class |
Marc Kupietz | cb3c59e | 2020-06-02 10:10:43 +0200 | [diff] [blame] | 236 | #' @importFrom dplyr rowwise bind_rows select summarise n |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 237 | #' @export |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 238 | setMethod("fetchNext", "KorAPQuery", function(kqo, |
| 239 | offset = kqo@nextStartIndex, |
| 240 | maxFetch = maxResultsPerPage, |
| 241 | verbose = kqo@korapConnection@verbose, |
| 242 | randomizePageOrder = FALSE) { |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 243 | if (kqo@totalResults == 0 || offset >= kqo@totalResults) { |
| 244 | return(kqo) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 245 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 246 | |
Marc Kupietz | 705488d | 2021-06-30 18:26:36 +0200 | [diff] [blame] | 247 | page <- kqo@nextStartIndex / maxResultsPerPage + 1 |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 248 | results <- 0 |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 249 | pubDate <- NULL # https://stackoverflow.com/questions/8096313/no-visible-binding-for-global-variable-note-in-r-cmd-check |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 250 | collectedMatches <- kqo@collectedMatches |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 251 | |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 252 | if (randomizePageOrder) { |
| 253 | pages <- head(sample.int(ceiling(kqo@totalResults / maxResultsPerPage)), maxFetch) - 1 |
| 254 | } |
| 255 | |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 256 | repeat { |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 257 | page = length(collectedMatches[,1]) %/% maxResultsPerPage + 1 |
| 258 | currentOffset = ifelse(randomizePageOrder, pages[page], page - 1) * maxResultsPerPage |
| 259 | query <- paste0(kqo@requestUrl, '&count=', min(if (!is.na(maxFetch)) maxFetch - results else maxResultsPerPage, maxResultsPerPage) ,'&offset=', currentOffset, '&cutoff=true') |
Marc Kupietz | 6817095 | 2021-06-30 09:37:21 +0200 | [diff] [blame] | 260 | res <- apiCall(kqo@korapConnection, query) |
| 261 | if (length(res$matches) == 0) { |
| 262 | break |
| 263 | } |
| 264 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 265 | for (field in kqo@fields) { |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 266 | if (!field %in% colnames(res$matches)) { |
| 267 | res$matches[, field] <- NA |
| 268 | } |
| 269 | } |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 270 | currentMatches <- |
Marc Kupietz | f75ab0b | 2020-06-02 12:31:18 +0200 | [diff] [blame] | 271 | res$matches %>% |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 272 | dplyr::select(kqo@fields) |
Marc Kupietz | 36d12d9 | 2019-09-27 18:13:27 +0200 | [diff] [blame] | 273 | if ("pubDate" %in% kqo@fields) { |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 274 | currentMatches$pubDate <- currentMatches$pubDate %>% as.Date(format = "%Y-%m-%d") |
| 275 | factorCols <- currentMatches %>% select(-pubDate) %>% colnames() |
Marc Kupietz | 36d12d9 | 2019-09-27 18:13:27 +0200 | [diff] [blame] | 276 | } else { |
| 277 | factorCols <- colnames(currentMatches) |
| 278 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 279 | currentMatches[factorCols] <- lapply(currentMatches[factorCols], factor) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 280 | if (!is.list(collectedMatches)) { |
| 281 | collectedMatches <- currentMatches |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 282 | } else { |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 283 | collectedMatches <- rbind(collectedMatches, currentMatches) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 284 | } |
Marc Kupietz | c2c59bd | 2019-08-30 16:50:49 +0200 | [diff] [blame] | 285 | if (verbose) { |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 286 | cat(paste0( |
| 287 | "Retrieved page ", |
| 288 | ceiling(length(collectedMatches[, 1]) / res$meta$itemsPerPage), |
| 289 | "/", |
| 290 | if (!is.na(maxFetch) && maxFetch < kqo@totalResults) |
| 291 | sprintf("%d (%d)", ceiling(maxFetch / res$meta$itemsPerPage), ceiling(kqo@totalResults / res$meta$itemsPerPage)) |
| 292 | else |
| 293 | sprintf("%d", ceiling(kqo@totalResults / res$meta$itemsPerPage)), |
| 294 | ' in ', |
| 295 | res$meta$benchmark, |
| 296 | '\n' |
| 297 | )) |
Marc Kupietz | c2c59bd | 2019-08-30 16:50:49 +0200 | [diff] [blame] | 298 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 299 | page <- page + 1 |
| 300 | results <- results + res$meta$itemsPerPage |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 301 | if (length(collectedMatches[,1]) >= kqo@totalResults || (!is.na(maxFetch) && results >= maxFetch)) { |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 302 | break |
| 303 | } |
| 304 | } |
Marc Kupietz | 6817095 | 2021-06-30 09:37:21 +0200 | [diff] [blame] | 305 | nextStartIndex <- min(res$meta$startIndex + res$meta$itemsPerPage, kqo@totalResults) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 306 | KorAPQuery(nextStartIndex = nextStartIndex, |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 307 | korapConnection = kqo@korapConnection, |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 308 | fields = kqo@fields, |
| 309 | requestUrl = kqo@requestUrl, |
| 310 | request = kqo@request, |
Marc Kupietz | 6817095 | 2021-06-30 09:37:21 +0200 | [diff] [blame] | 311 | totalResults = kqo@totalResults, |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 312 | vc = kqo@vc, |
| 313 | webUIRequestUrl = kqo@webUIRequestUrl, |
Marc Kupietz | 6817095 | 2021-06-30 09:37:21 +0200 | [diff] [blame] | 314 | hasMoreMatches = (kqo@totalResults > nextStartIndex), |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 315 | apiResponse = res, |
| 316 | collectedMatches = collectedMatches) |
| 317 | }) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 318 | |
| 319 | #' Fetch all results of a KorAP query. |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 320 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 321 | #' **`fetchAll`** fetches all results of a KorAP query. |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame] | 322 | #' |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 323 | #' @examples |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 324 | #' \donttest{ |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 325 | #' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll() |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 326 | #' q@collectedMatches |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 327 | #' } |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 328 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 329 | #' @aliases fetchAll |
| 330 | #' @rdname KorAPQuery-class |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 331 | #' @export |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 332 | setMethod("fetchAll", "KorAPQuery", function(kqo, verbose = kqo@korapConnection@verbose, ...) { |
| 333 | return(fetchNext(kqo, offset = 0, maxFetch = NA, verbose = verbose, ...)) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 334 | }) |
| 335 | |
| 336 | #' Fetches the remaining results of a KorAP query. |
| 337 | #' |
| 338 | #' @examples |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 339 | #' \donttest{ |
| 340 | #' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchRest() |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 341 | #' q@collectedMatches |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 342 | #' } |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 343 | #' |
| 344 | #' @aliases fetchRest |
| 345 | #' @rdname KorAPQuery-class |
| 346 | #' @export |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 347 | setMethod("fetchRest", "KorAPQuery", function(kqo, verbose = kqo@korapConnection@verbose, ...) { |
| 348 | return(fetchNext(kqo, maxFetch = NA, verbose = verbose, ...)) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 349 | }) |
| 350 | |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 351 | #' Query relative frequency of search term(s) |
| 352 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 353 | #' **`frequencyQuery`** combines [corpusQuery()], [corpusStats()] and |
| 354 | #' [ci()] to compute a table with the relative frequencies and |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 355 | #' confidence intervals of one ore multiple search terms across one or multiple |
| 356 | #' virtual corpora. |
| 357 | #' |
| 358 | #' @aliases frequencyQuery |
| 359 | #' @rdname KorAPQuery-class |
| 360 | #' @examples |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 361 | #' \donttest{ |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 362 | #' new("KorAPConnection", verbose = TRUE) %>% |
| 363 | #' frequencyQuery(c("Mücke", "Schnake"), paste0("pubDate in ", 2000:2003)) |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 364 | #' } |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 365 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame^] | 366 | #' @param kco [KorAPConnection()] object (obtained e.g. from `new("KorAPConnection")` |
| 367 | #' @param query string that contains the corpus query. The query language depends on the `ql` parameter. Either `query` must be provided or `KorAPUrl`. |
| 368 | #' @param conf.level confidence level of the returned confidence interval (passed through [ci()] to [prop.test()]). |
| 369 | #' @param as.alternatives LOGICAL that specifies if the query terms should be treated as alternatives. If `as.alternatives` is TRUE, the sum over all query hits, instead of the respective vc token sizes is used as total for the calculation of relative frequencies. |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 370 | #' @export |
| 371 | setMethod("frequencyQuery", "KorAPConnection", |
Marc Kupietz | 71d6e05 | 2019-11-22 18:42:10 +0100 | [diff] [blame] | 372 | function(kco, query, vc = "", conf.level = 0.95, as.alternatives = FALSE, ...) { |
| 373 | (if (as.alternatives) { |
| 374 | corpusQuery(kco, query, vc, metadataOnly = TRUE, as.df = TRUE, ...) %>% |
| 375 | group_by(vc) %>% |
| 376 | mutate(total = sum(totalResults)) |
| 377 | } else { |
| 378 | corpusQuery(kco, query, vc, metadataOnly = TRUE, as.df = TRUE, ...) %>% |
| 379 | mutate(total = corpusStats(kco, vc=vc, as.df=TRUE)$tokens) |
| 380 | } ) %>% |
Marc Kupietz | 0c29cea | 2019-10-09 08:44:36 +0200 | [diff] [blame] | 381 | ci(conf.level = conf.level) |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 382 | }) |
| 383 | |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 384 | |
| 385 | #' buildWebUIRequestUrl |
| 386 | #' |
| 387 | #' @rdname KorAPQuery-class |
| 388 | #' @importFrom urltools url_encode |
| 389 | #' @export |
| 390 | buildWebUIRequestUrl <- function(kco, |
| 391 | query = if (missing(KorAPUrl)) |
| 392 | stop("At least one of the parameters query and KorAPUrl must be specified.", call. = FALSE) |
| 393 | else |
| 394 | httr::parse_url(KorAPUrl)$query$q, |
| 395 | vc = if (missing(KorAPUrl)) "" else httr::parse_url(KorAPUrl)$query$cq, |
| 396 | KorAPUrl, |
| 397 | metadataOnly = TRUE, |
| 398 | ql = if (missing(KorAPUrl)) "poliqarp" else httr::parse_url(KorAPUrl)$query$ql, |
| 399 | fields = c( |
| 400 | "corpusSigle", |
| 401 | "textSigle", |
| 402 | "pubDate", |
| 403 | "pubPlace", |
| 404 | "availability", |
| 405 | "textClass", |
| 406 | "snippet" |
| 407 | ), |
| 408 | accessRewriteFatal = TRUE) { |
| 409 | request <- |
| 410 | paste0( |
| 411 | '?q=', |
| 412 | urltools::url_encode(enc2utf8(as.character(query))), |
| 413 | ifelse(vc != '', |
| 414 | paste0('&cq=', urltools::url_encode(enc2utf8(vc))), |
| 415 | ''), |
| 416 | '&ql=', |
| 417 | ql |
| 418 | ) |
| 419 | webUIRequestUrl <- paste0(kco@KorAPUrl, request) |
| 420 | requestUrl <- paste0( |
| 421 | kco@apiUrl, |
| 422 | 'search', |
| 423 | request, |
| 424 | '&fields=', |
| 425 | paste(fields, collapse = ","), |
| 426 | if (metadataOnly) |
| 427 | '&access-rewrite-disabled=true' |
| 428 | else |
| 429 | '' |
| 430 | ) |
| 431 | webUIRequestUrl |
| 432 | } |
| 433 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 434 | #´ format() |
| 435 | #' @rdname KorAPQuery-class |
| 436 | #' @param x KorAPQuery object |
| 437 | #' @param ... further arguments passed to or from other methods |
| 438 | #' @export |
| 439 | format.KorAPQuery <- function(x, ...) { |
| 440 | cat("<KorAPQuery>\n") |
| 441 | q <- x |
| 442 | aurl = parse_url(q@request) |
Marc Kupietz | 0d4c909 | 2020-03-23 09:02:30 +0100 | [diff] [blame] | 443 | cat(" Query: ", aurl$query$q, "\n") |
| 444 | if (!is.null(aurl$query$cq) && aurl$query$cq != "") { |
| 445 | cat(" Virtual corpus: ", aurl$query$cq, "\n") |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 446 | } |
| 447 | if (!is.null(q@collectedMatches)) { |
| 448 | cat("==============================================================================================================", "\n") |
| 449 | print(summary(q@collectedMatches)) |
| 450 | cat("==============================================================================================================", "\n") |
| 451 | } |
| 452 | cat(" Total results: ", q@totalResults, "\n") |
| 453 | cat(" Fetched results: ", q@nextStartIndex, "\n") |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 456 | #' show() |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 457 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 458 | #' @rdname KorAPQuery-class |
| 459 | #' @param object KorAPQuery object |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 460 | #' @export |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 461 | setMethod("show", "KorAPQuery", function(object) { |
| 462 | format(object) |
| 463 | }) |
Marc Kupietz | 006b47c | 2021-01-13 17:00:59 +0100 | [diff] [blame] | 464 | |