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 | 6dfeed9 | 2025-06-03 11:58:06 +0200 | [diff] [blame] | 8 | #' @include logging.R |
Marc Kupietz | f912959 | 2025-01-26 19:17:54 +0100 | [diff] [blame] | 9 | #' @import httr2 |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 10 | #' |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame] | 11 | #' @include RKorAPClient-package.R |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 12 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 13 | #' @export |
| 14 | KorAPQuery <- setClass("KorAPQuery", slots = c( |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 15 | "korapConnection", |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 16 | "request", |
| 17 | "vc", |
| 18 | "totalResults", |
| 19 | "nextStartIndex", |
| 20 | "fields", |
| 21 | "requestUrl", |
| 22 | "webUIRequestUrl", |
| 23 | "apiResponse", |
| 24 | "collectedMatches", |
| 25 | "hasMoreMatches" |
| 26 | )) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 27 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 28 | #' Method initialize |
| 29 | #' |
| 30 | #' @rdname KorAPQuery-class |
| 31 | #' @param .Object … |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 32 | #' @param korapConnection KorAPConnection object |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 33 | #' @param request query part of the request URL |
| 34 | #' @param vc definition of a virtual corpus |
| 35 | #' @param totalResults number of hits the query has yielded |
| 36 | #' @param nextStartIndex at what index to start the next fetch of query results |
| 37 | #' @param fields what data / metadata fields should be collected |
| 38 | #' @param requestUrl complete URL of the API request |
| 39 | #' @param webUIRequestUrl URL of a web frontend request corresponding to the API request |
| 40 | #' @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] | 41 | #' @param hasMoreMatches logical that signals if more query results can be fetched |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 42 | #' @param collectedMatches matches already fetched from the KorAP-API-server |
Marc Kupietz | 97a1bca | 2019-10-04 22:52:09 +0200 | [diff] [blame] | 43 | #' |
| 44 | #' @importFrom tibble tibble |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 45 | #' @export |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 46 | setMethod( |
| 47 | "initialize", "KorAPQuery", |
| 48 | function(.Object, korapConnection = NULL, request = NULL, vc = "", totalResults = 0, nextStartIndex = 0, fields = c( |
| 49 | "corpusSigle", "textSigle", "pubDate", "pubPlace", |
| 50 | "availability", "textClass", "snippet", "tokens" |
| 51 | ), |
| 52 | requestUrl = "", webUIRequestUrl = "", apiResponse = NULL, hasMoreMatches = FALSE, collectedMatches = NULL) { |
| 53 | .Object <- callNextMethod() |
| 54 | .Object@korapConnection <- korapConnection |
| 55 | .Object@request <- request |
| 56 | .Object@vc <- vc |
| 57 | .Object@totalResults <- totalResults |
| 58 | .Object@nextStartIndex <- nextStartIndex |
| 59 | .Object@fields <- fields |
| 60 | .Object@requestUrl <- requestUrl |
| 61 | .Object@webUIRequestUrl <- webUIRequestUrl |
| 62 | .Object@apiResponse <- apiResponse |
| 63 | .Object@hasMoreMatches <- hasMoreMatches |
| 64 | .Object@collectedMatches <- collectedMatches |
| 65 | .Object |
| 66 | } |
| 67 | ) |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 68 | |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 69 | setGeneric("corpusQuery", function(kco, ...) standardGeneric("corpusQuery")) |
| 70 | setGeneric("fetchAll", function(kqo, ...) standardGeneric("fetchAll")) |
| 71 | setGeneric("fetchNext", function(kqo, ...) standardGeneric("fetchNext")) |
| 72 | setGeneric("fetchRest", function(kqo, ...) standardGeneric("fetchRest")) |
| 73 | setGeneric("frequencyQuery", function(kco, ...) standardGeneric("frequencyQuery")) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 74 | |
| 75 | maxResultsPerPage <- 50 |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 76 | |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 77 | ## quiets concerns of R CMD check re: the .'s that appear in pipelines |
Marc Kupietz | ef1ef4a | 2025-02-19 12:12:40 +0100 | [diff] [blame] | 78 | utils::globalVariables(c(".")) |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 79 | |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 80 | #' Corpus query |
| 81 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 82 | #' **`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] | 83 | #' |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 84 | #' @rdname KorAPQuery-class |
| 85 | #' @aliases corpusQuery |
| 86 | #' |
| 87 | #' @importFrom urltools url_encode |
| 88 | #' @importFrom purrr pmap |
| 89 | #' @importFrom dplyr bind_rows |
| 90 | #' |
Marc Kupietz | 617266d | 2025-02-27 10:43:07 +0100 | [diff] [blame] | 91 | #' @param kco [KorAPConnection()] object (obtained e.g. from `KorAPConnection()` |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 92 | #' @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] | 93 | #' @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] | 94 | #' @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 | 132f005 | 2023-04-16 14:23:05 +0200 | [diff] [blame] | 95 | #' @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. |
| 96 | #' If you want your corpus queries to return not only metadata, but also KWICS, you need to authorize |
| 97 | #' your RKorAPClient application as explained in the |
| 98 | #' [authorization section](https://github.com/KorAP/RKorAPClient#authorization) |
| 99 | #' of the RKorAPClient Readme on GitHub and set the `metadataOnly` parameter to |
| 100 | #' `FALSE`. |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 101 | #' @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] | 102 | #' @param fields (meta)data fields that will be fetched for every match. |
Marc Kupietz | 43a6ade | 2020-02-18 17:01:44 +0100 | [diff] [blame] | 103 | #' @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] | 104 | #' @param verbose print some info |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 105 | #' @param as.df return result as data frame instead of as S4 object? |
Marc Kupietz | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 106 | #' @param expand logical that decides if `query` and `vc` parameters are expanded to all of their combinations. Defaults to `TRUE`, iff `query` and `vc` have different lengths |
Marc Kupietz | d9b2fd7 | 2023-04-17 19:08:50 +0200 | [diff] [blame] | 107 | #' @param context string that specifies the size of the left and the right context returned in `snippet` |
| 108 | #' (provided that `metadataOnly` is set to `false` and that the necessary access right are met). |
| 109 | #' The format of the context size specifcation (e.g. `3-token,3-token`) is described in the [Service: Search GET documentation of the Kustvakt Wiki](https://github.com/KorAP/Kustvakt/wiki/Service:-Search-GET). |
| 110 | #' If the parameter is not set, the default context size secification of the KorAP server instance will be used. |
| 111 | #' Note that you cannot overrule the maximum context size set in the KorAP server instance, |
| 112 | #' as this is typically legally motivated. |
Marc Kupietz | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 113 | #' @return Depending on the `as.df` parameter, a tibble 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()]). |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 114 | #' A corresponding URL to be used within a web browser is contained in `@webUIRequestUrl` |
| 115 | #' 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] | 116 | #' |
| 117 | #' @examples |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 118 | #' \dontrun{ |
| 119 | #' |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 120 | #' # Fetch metadata of every query hit for "Ameisenplage" and show a summary |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 121 | #' KorAPConnection() %>% |
| 122 | #' corpusQuery("Ameisenplage") %>% |
| 123 | #' fetchAll() |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 124 | #' } |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 125 | #' |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 126 | #' \dontrun{ |
| 127 | #' |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 128 | #' # Use the copy of a KorAP-web-frontend URL for an API query of "Ameise" in a virtual corpus |
| 129 | #' # and show the number of query hits (but don't fetch them). |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 130 | #' |
Marc Kupietz | 617266d | 2025-02-27 10:43:07 +0100 | [diff] [blame] | 131 | #' KorAPConnection(verbose = TRUE) %>% |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 132 | #' corpusQuery( |
| 133 | #' KorAPUrl = |
| 134 | #' "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp" |
| 135 | #' ) |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 136 | #' } |
| 137 | #' |
| 138 | #' \dontrun{ |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 139 | #' |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 140 | #' # Plot the time/frequency curve of "Ameisenplage" |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 141 | #' KorAPConnection(verbose = TRUE) %>% |
| 142 | #' { |
| 143 | #' . ->> kco |
| 144 | #' } %>% |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 145 | #' corpusQuery("Ameisenplage") %>% |
| 146 | #' fetchAll() %>% |
| 147 | #' slot("collectedMatches") %>% |
| 148 | #' mutate(year = lubridate::year(pubDate)) %>% |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 149 | #' dplyr::select(year) %>% |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 150 | #' group_by(year) %>% |
Marc Kupietz | cb3c59e | 2020-06-02 10:10:43 +0200 | [diff] [blame] | 151 | #' summarise(Count = dplyr::n()) %>% |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 152 | #' mutate(Freq = mapply(function(f, y) { |
| 153 | #' f / corpusStats(kco, paste("pubDate in", y))@tokens |
| 154 | #' }, Count, year)) %>% |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 155 | #' dplyr::select(-Count) %>% |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 156 | #' complete(year = min(year):max(year), fill = list(Freq = 0)) %>% |
| 157 | #' plot(type = "l") |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 158 | #' } |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 159 | #' @seealso [KorAPConnection()], [fetchNext()], [fetchRest()], [fetchAll()], [corpusStats()] |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 160 | #' |
| 161 | #' @references |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 162 | #' <https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026> |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 163 | #' |
| 164 | #' @export |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 165 | setMethod( |
| 166 | "corpusQuery", "KorAPConnection", |
| 167 | function(kco, |
| 168 | query = if (missing(KorAPUrl)) { |
| 169 | stop("At least one of the parameters query and KorAPUrl must be specified.", call. = FALSE) |
| 170 | } else { |
| 171 | httr2::url_parse(KorAPUrl)$query$q |
| 172 | }, |
| 173 | vc = if (missing(KorAPUrl)) "" else httr2::url_parse(KorAPUrl)$query$cq, |
| 174 | KorAPUrl, |
| 175 | metadataOnly = TRUE, |
| 176 | ql = if (missing(KorAPUrl)) "poliqarp" else httr2::url_parse(KorAPUrl)$query$ql, |
| 177 | fields = c( |
| 178 | "corpusSigle", |
| 179 | "textSigle", |
| 180 | "pubDate", |
| 181 | "pubPlace", |
| 182 | "availability", |
| 183 | "textClass", |
| 184 | "snippet", |
| 185 | "tokens" |
| 186 | ), |
| 187 | accessRewriteFatal = TRUE, |
| 188 | verbose = kco@verbose, |
| 189 | expand = length(vc) != length(query), |
| 190 | as.df = FALSE, |
| 191 | context = NULL) { |
| 192 | if (length(query) > 1 || length(vc) > 1) { |
| 193 | grid <- if (expand) expand_grid(query = query, vc = vc) else tibble(query = query, vc = vc) |
Marc Kupietz | 6ef61a8 | 2025-05-29 16:07:03 +0200 | [diff] [blame] | 194 | |
| 195 | # Initialize timing variables for ETA calculation |
| 196 | total_queries <- nrow(grid) |
| 197 | current_query <- 0 |
| 198 | start_time <- Sys.time() |
| 199 | |
Marc Kupietz | 6ef61a8 | 2025-05-29 16:07:03 +0200 | [diff] [blame] | 200 | results <- purrr::pmap(grid, function(query, vc, ...) { |
| 201 | current_query <<- current_query + 1 |
| 202 | |
| 203 | # Execute the single query directly (avoiding recursive call) |
| 204 | contentFields <- c("snippet", "tokens") |
| 205 | query_fields <- fields |
| 206 | if (metadataOnly) { |
| 207 | query_fields <- query_fields[!query_fields %in% contentFields] |
| 208 | } |
| 209 | if (!"textSigle" %in% query_fields) { |
| 210 | query_fields <- c(query_fields, "textSigle") |
| 211 | } |
| 212 | request <- |
| 213 | paste0( |
| 214 | "?q=", |
| 215 | url_encode(enc2utf8(query)), |
| 216 | ifelse(!metadataOnly && !is.null(context) && context != "", paste0("&context=", url_encode(enc2utf8(context))), ""), |
| 217 | ifelse(vc != "", paste0("&cq=", url_encode(enc2utf8(vc))), ""), |
| 218 | ifelse(!metadataOnly, "&show-tokens=true", ""), |
| 219 | "&ql=", ql |
| 220 | ) |
| 221 | webUIRequestUrl <- paste0(kco@KorAPUrl, request) |
| 222 | requestUrl <- paste0( |
| 223 | kco@apiUrl, |
| 224 | "search", |
| 225 | request, |
| 226 | "&fields=", |
| 227 | paste(query_fields, collapse = ","), |
| 228 | if (metadataOnly) "&access-rewrite-disabled=true" else "" |
| 229 | ) |
| 230 | |
| 231 | # Show individual query progress |
| 232 | log_info(verbose, "\rSearching \"", query, "\" in \"", vc, "\"", sep = "") |
| 233 | res <- apiCall(kco, paste0(requestUrl, "&count=0")) |
| 234 | if (is.null(res)) { |
| 235 | log_info(verbose, ": API call failed\n") |
| 236 | totalResults <- 0 |
| 237 | } else { |
| 238 | totalResults <- as.integer(res$meta$totalResults) |
| 239 | log_info(verbose, ": ", totalResults, " hits") |
| 240 | if (!is.null(res$meta$cached)) { |
| 241 | log_info(verbose, " [cached]") |
| 242 | } else if (!is.null(res$meta$benchmark)) { |
| 243 | if (is.character(res$meta$benchmark) && grepl("s$", res$meta$benchmark)) { |
| 244 | time_value <- as.numeric(sub("s$", "", res$meta$benchmark)) |
| 245 | formatted_time <- paste0(round(time_value, 2), "s") |
| 246 | log_info(verbose, ", took ", formatted_time) |
| 247 | } else { |
| 248 | log_info(verbose, ", took ", res$meta$benchmark) |
| 249 | } |
| 250 | } |
| 251 | log_info(verbose, "\n") |
| 252 | } |
| 253 | |
| 254 | result <- data.frame( |
| 255 | query = query, |
| 256 | totalResults = totalResults, |
| 257 | vc = vc, |
| 258 | webUIRequestUrl = webUIRequestUrl, |
| 259 | stringsAsFactors = FALSE |
| 260 | ) |
| 261 | |
| 262 | # Calculate and display ETA information if verbose and we have more than one query |
| 263 | if (verbose && total_queries > 1) { |
Marc Kupietz | 6dfeed9 | 2025-06-03 11:58:06 +0200 | [diff] [blame] | 264 | eta_info <- calculate_eta(current_query, total_queries, start_time) |
| 265 | if (eta_info != "") { |
| 266 | elapsed_time <- as.numeric(difftime(Sys.time(), start_time, units = "secs")) |
Marc Kupietz | 6ef61a8 | 2025-05-29 16:07:03 +0200 | [diff] [blame] | 267 | avg_time_per_query <- elapsed_time / current_query |
Marc Kupietz | 6ef61a8 | 2025-05-29 16:07:03 +0200 | [diff] [blame] | 268 | |
| 269 | # Create progress display |
| 270 | progress_display <- paste0( |
| 271 | "Query ", |
| 272 | sprintf(paste0("%", nchar(total_queries), "d"), current_query), |
| 273 | "/", |
| 274 | sprintf("%d", total_queries), |
| 275 | " completed. Avg: ", |
| 276 | sprintf("%.1f", avg_time_per_query), |
Marc Kupietz | 6dfeed9 | 2025-06-03 11:58:06 +0200 | [diff] [blame] | 277 | "s/query", |
| 278 | eta_info |
Marc Kupietz | 6ef61a8 | 2025-05-29 16:07:03 +0200 | [diff] [blame] | 279 | ) |
| 280 | |
| 281 | log_info(verbose, progress_display, "\n") |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return(result) |
| 286 | }) |
| 287 | |
| 288 | results %>% bind_rows() |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 289 | } else { |
Marc Kupietz | 2078bde | 2023-08-27 16:46:15 +0200 | [diff] [blame] | 290 | contentFields <- c("snippet", "tokens") |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 291 | if (metadataOnly) { |
| 292 | fields <- fields[!fields %in% contentFields] |
| 293 | } |
Marc Kupietz | 80dc643 | 2025-02-07 16:57:40 +0100 | [diff] [blame] | 294 | if (!"textSigle" %in% fields) { |
| 295 | fields <- c(fields, "textSigle") |
| 296 | } |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 297 | request <- |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 298 | paste0( |
| 299 | "?q=", |
| 300 | url_encode(enc2utf8(query)), |
| 301 | ifelse(!metadataOnly && !is.null(context) && context != "", paste0("&context=", url_encode(enc2utf8(context))), ""), |
| 302 | ifelse(vc != "", paste0("&cq=", url_encode(enc2utf8(vc))), ""), |
| 303 | ifelse(!metadataOnly, "&show-tokens=true", ""), |
| 304 | "&ql=", ql |
| 305 | ) |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 306 | webUIRequestUrl <- paste0(kco@KorAPUrl, request) |
| 307 | requestUrl <- paste0( |
| 308 | kco@apiUrl, |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 309 | "search", |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 310 | request, |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 311 | "&fields=", |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 312 | paste(fields, collapse = ","), |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 313 | if (metadataOnly) "&access-rewrite-disabled=true" else "" |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 314 | ) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 315 | log_info(verbose, "\rSearching \"", query, "\" in \"", vc, "\"", |
| 316 | sep = |
| 317 | "" |
| 318 | ) |
| 319 | res <- apiCall(kco, paste0(requestUrl, "&count=0")) |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 320 | if (is.null(res)) { |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 321 | message("API call failed.") |
| 322 | totalResults <- 0 |
| 323 | } else { |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 324 | totalResults <- as.integer(res$meta$totalResults) |
Marc Kupietz | a47d150 | 2023-04-18 15:26:47 +0200 | [diff] [blame] | 325 | log_info(verbose, ": ", totalResults, " hits") |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 326 | if (!is.null(res$meta$cached)) { |
Marc Kupietz | a47d150 | 2023-04-18 15:26:47 +0200 | [diff] [blame] | 327 | log_info(verbose, " [cached]\n") |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 328 | } else if (!is.null(res$meta$benchmark)) { |
Marc Kupietz | 7638ca4 | 2025-05-25 13:18:16 +0200 | [diff] [blame] | 329 | # Round the benchmark time to 2 decimal places for better readability |
| 330 | # If it's a string ending with 's', extract the number, round it, and re-add 's' |
| 331 | if (is.character(res$meta$benchmark) && grepl("s$", res$meta$benchmark)) { |
| 332 | time_value <- as.numeric(sub("s$", "", res$meta$benchmark)) |
| 333 | formatted_time <- paste0(round(time_value, 2), "s") |
| 334 | log_info(verbose, ", took ", formatted_time, "\n", sep = "") |
| 335 | } else { |
| 336 | # Fallback if the format is different than expected |
| 337 | log_info(verbose, ", took ", res$meta$benchmark, "\n", sep = "") |
| 338 | } |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 339 | } else { |
| 340 | log_info(verbose, "\n") |
| 341 | } |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 342 | } |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 343 | if (as.df) { |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 344 | data.frame( |
| 345 | query = query, |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 346 | totalResults = totalResults, |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 347 | vc = vc, |
| 348 | webUIRequestUrl = webUIRequestUrl, |
| 349 | stringsAsFactors = FALSE |
| 350 | ) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 351 | } else { |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 352 | KorAPQuery( |
| 353 | korapConnection = kco, |
| 354 | nextStartIndex = 0, |
| 355 | fields = fields, |
| 356 | requestUrl = requestUrl, |
| 357 | request = request, |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 358 | totalResults = totalResults, |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 359 | vc = vc, |
| 360 | apiResponse = res, |
| 361 | webUIRequestUrl = webUIRequestUrl, |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 362 | hasMoreMatches = (totalResults > 0), |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 363 | ) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 364 | } |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 365 | } |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 366 | } |
| 367 | ) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 368 | |
Marc Kupietz | 05a6079 | 2024-12-07 16:23:31 +0100 | [diff] [blame] | 369 | #' @importFrom purrr map |
| 370 | repair_data_strcuture <- function(x) { |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 371 | if (is.list(x)) { |
| 372 | as.character(purrr::map(x, ~ if (length(.x) > 1) { |
Marc Kupietz | 05a6079 | 2024-12-07 16:23:31 +0100 | [diff] [blame] | 373 | paste(.x, collapse = " ") |
| 374 | } else { |
| 375 | .x |
| 376 | })) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 377 | } else { |
Marc Kupietz | 05a6079 | 2024-12-07 16:23:31 +0100 | [diff] [blame] | 378 | ifelse(is.na(x), "", x) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 379 | } |
Marc Kupietz | 05a6079 | 2024-12-07 16:23:31 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 382 | #' Fetch the next bunch of results of a KorAP query. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 383 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 384 | #' **`fetchNext`** fetches the next bunch of results of a KorAP query. |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 385 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 386 | #' @param kqo object obtained from [corpusQuery()] |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 387 | #' @param offset start offset for query results to fetch |
| 388 | #' @param maxFetch maximum number of query results to fetch |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 389 | #' @param verbose print progress information if true |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 390 | #' @param randomizePageOrder fetch result pages in pseudo random order if true. Use [set.seed()] to set seed for reproducible results. |
| 391 | #' @return The `kqo` input object with updated slots `collectedMatches`, `apiResponse`, `nextStartIndex`, `hasMoreMatches` |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 392 | #' |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 393 | #' @examples |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 394 | #' \dontrun{ |
| 395 | #' |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 396 | #' q <- KorAPConnection() %>% |
| 397 | #' corpusQuery("Ameisenplage") %>% |
| 398 | #' fetchNext() |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 399 | #' q@collectedMatches |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 400 | #' } |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 401 | #' |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 402 | #' @references |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 403 | #' <https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026> |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 404 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 405 | #' @aliases fetchNext |
| 406 | #' @rdname KorAPQuery-class |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 407 | #' @importFrom dplyr rowwise mutate bind_rows select summarise n select |
Marc Kupietz | f488112 | 2024-12-17 14:55:39 +0100 | [diff] [blame] | 408 | #' @importFrom tibble enframe add_column |
| 409 | #' @importFrom stringr word |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 410 | #' @importFrom tidyr unnest unchop pivot_wider |
| 411 | #' @importFrom purrr map |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 412 | #' @export |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 413 | setMethod("fetchNext", "KorAPQuery", function(kqo, |
| 414 | offset = kqo@nextStartIndex, |
| 415 | maxFetch = maxResultsPerPage, |
| 416 | verbose = kqo@korapConnection@verbose, |
| 417 | randomizePageOrder = FALSE) { |
Marc Kupietz | a7a8f1b | 2024-12-18 15:56:19 +0100 | [diff] [blame] | 418 | # https://stackoverflow.com/questions/8096313/no-visible-binding-for-global-variable-note-in-r-cmd-check |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 419 | results <- key <- name <- tmp_positions <- 0 |
Marc Kupietz | a7a8f1b | 2024-12-18 15:56:19 +0100 | [diff] [blame] | 420 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 421 | if (kqo@totalResults == 0 || offset >= kqo@totalResults) { |
| 422 | return(kqo) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 423 | } |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 424 | use_korap_api <- Sys.getenv("USE_KORAP_API", unset = NA) |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 425 | # Calculate the initial page number (not used directly - keeping for reference) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 426 | collectedMatches <- kqo@collectedMatches |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 427 | |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 428 | # For randomized page order, generate a list of randomized page indices |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 429 | if (randomizePageOrder) { |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 430 | # Calculate how many pages we need to fetch based on maxFetch |
| 431 | total_pages_to_fetch <- if (!is.na(maxFetch)) { |
| 432 | # Either limited by maxFetch or total results, whichever is smaller |
| 433 | min(ceiling(maxFetch / maxResultsPerPage), ceiling(kqo@totalResults / maxResultsPerPage)) |
| 434 | } else { |
| 435 | # All pages |
| 436 | ceiling(kqo@totalResults / maxResultsPerPage) |
| 437 | } |
| 438 | |
| 439 | # Generate randomized page indices (0-based for API) |
| 440 | pages <- sample.int(ceiling(kqo@totalResults / maxResultsPerPage), total_pages_to_fetch) - 1 |
| 441 | page_index <- 1 # Index to track which page in the randomized list we're on |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 444 | if (is.null(collectedMatches)) { |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 445 | collectedMatches <- data.frame() |
| 446 | } |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 447 | |
| 448 | # Initialize the page counter properly based on nextStartIndex and any previously fetched results |
| 449 | # We add 1 to make it 1-based for display purposes since users expect page numbers to start from 1 |
| 450 | # For first call, this will be 1, for subsequent calls, it will reflect our actual position |
| 451 | current_page_number <- ceiling(offset / maxResultsPerPage) + 1 |
| 452 | |
| 453 | # For sequential fetches, keep track of which global page we're on |
| 454 | # This is important for correctly showing page numbers in subsequent fetchNext calls |
| 455 | page_count_start <- current_page_number |
| 456 | |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 457 | repeat { |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 458 | # Determine which page to fetch next |
| 459 | if (randomizePageOrder) { |
| 460 | # In randomized mode, get the page from our randomized list using the page_index |
| 461 | # Make sure we don't exceed the array bounds |
| 462 | if (page_index > length(pages)) { |
| 463 | break # No more pages to fetch in randomized mode |
| 464 | } |
| 465 | current_offset_page <- pages[page_index] |
| 466 | # For display purposes in randomized mode, show which page out of the total we're fetching |
| 467 | display_page_number <- page_index |
| 468 | } else { |
| 469 | # In sequential mode, use the current_page_number to calculate the offset |
| 470 | current_offset_page <- (current_page_number - 1) |
| 471 | display_page_number <- current_page_number |
| 472 | } |
| 473 | |
| 474 | # Calculate the actual offset in tokens |
| 475 | currentOffset <- current_offset_page * maxResultsPerPage |
| 476 | |
| 477 | # Build the query with the appropriate count and offset |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 478 | 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] | 479 | res <- apiCall(kqo@korapConnection, query) |
| 480 | if (length(res$matches) == 0) { |
| 481 | break |
| 482 | } |
| 483 | |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 484 | if ("fields" %in% colnames(res$matches) && (is.na(use_korap_api) || as.numeric(use_korap_api) >= 1.0)) { |
Marc Kupietz | 16ccf11 | 2025-01-26 13:25:27 +0100 | [diff] [blame] | 485 | log_info(verbose, "Using fields API: ") |
Marc Kupietz | 05a6079 | 2024-12-07 16:23:31 +0100 | [diff] [blame] | 486 | currentMatches <- res$matches$fields %>% |
| 487 | purrr::map(~ mutate(.x, value = repair_data_strcuture(value))) %>% |
| 488 | tibble::enframe() %>% |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 489 | tidyr::unnest(cols = value) %>% |
| 490 | tidyr::pivot_wider(names_from = key, id_cols = name, names_repair = "unique") %>% |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 491 | dplyr::select(-name) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 492 | if ("snippet" %in% colnames(res$matches)) { |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 493 | currentMatches$snippet <- res$matches$snippet |
| 494 | } |
Marc Kupietz | 3cd2c6c | 2025-01-08 20:35:39 +0100 | [diff] [blame] | 495 | if ("tokens" %in% colnames(res$matches)) { |
| 496 | currentMatches$tokens <- res$matches$tokens |
| 497 | } |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 498 | } else { |
| 499 | currentMatches <- res$matches |
| 500 | } |
| 501 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 502 | for (field in kqo@fields) { |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 503 | if (!field %in% colnames(currentMatches)) { |
| 504 | currentMatches[, field] <- NA |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 505 | } |
| 506 | } |
Marc Kupietz | f488112 | 2024-12-17 14:55:39 +0100 | [diff] [blame] | 507 | currentMatches <- currentMatches %>% |
| 508 | select(kqo@fields) %>% |
| 509 | mutate( |
Marc Kupietz | 0447da0 | 2025-01-08 20:51:09 +0100 | [diff] [blame] | 510 | tmp_positions = gsub(".*-p(\\d+)-(\\d+).*", "\\1 \\2", res$matches$matchID), |
Marc Kupietz | f488112 | 2024-12-17 14:55:39 +0100 | [diff] [blame] | 511 | matchStart = as.integer(stringr::word(tmp_positions, 1)), |
| 512 | matchEnd = as.integer(stringr::word(tmp_positions, 2)) - 1 |
| 513 | ) %>% |
| 514 | select(-tmp_positions) |
| 515 | |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 516 | if (!is.list(collectedMatches)) { |
| 517 | collectedMatches <- currentMatches |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 518 | } else { |
Marc Kupietz | 2078bde | 2023-08-27 16:46:15 +0200 | [diff] [blame] | 519 | collectedMatches <- bind_rows(collectedMatches, currentMatches) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 520 | } |
Marc Kupietz | ae9b617 | 2025-05-02 15:50:01 +0200 | [diff] [blame] | 521 | |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 522 | # Get the actual items per page from the API response |
| 523 | # We now consistently use maxResultsPerPage instead |
Marc Kupietz | acbaab0 | 2025-05-01 10:56:35 +0200 | [diff] [blame] | 524 | |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 525 | # Calculate total pages consistently using fixed maxResultsPerPage |
| 526 | # This ensures consistent page counting across the function |
| 527 | total_pages <- ceiling(kqo@totalResults / maxResultsPerPage) |
| 528 | |
| 529 | # Calculate the total pages based on what we've already fetched plus what we'll fetch |
| 530 | # This ensures the correct denominator is displayed for subsequent fetchNext calls |
| 531 | |
| 532 | # Calculate the total number of pages for the entire result set |
| 533 | # This calculation is kept for reference and for showing in parentheses |
Marc Kupietz | 669114b | 2025-05-02 22:02:20 +0200 | [diff] [blame] | 534 | |
Marc Kupietz | ae9b617 | 2025-05-02 15:50:01 +0200 | [diff] [blame] | 535 | # Estimate remaining time |
| 536 | time_per_page <- NA |
| 537 | eta_str <- "N/A" |
| 538 | completion_time_str <- "N/A" |
Marc Kupietz | acbaab0 | 2025-05-01 10:56:35 +0200 | [diff] [blame] | 539 | |
Marc Kupietz | ae9b617 | 2025-05-02 15:50:01 +0200 | [diff] [blame] | 540 | if (!is.null(res$meta$benchmark) && is.character(res$meta$benchmark)) { |
| 541 | # benchmark looks like "0.123s" |
| 542 | time_per_page <- suppressWarnings(as.numeric(sub("s", "", res$meta$benchmark))) |
| 543 | if (!is.na(time_per_page)) { |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 544 | # First determine our current global position for ETA calculation |
| 545 | current_global_position <- if (randomizePageOrder) { |
| 546 | # In randomized mode, this is how many pages we've processed so far in this batch |
| 547 | page_index - 1 # -1 because we're calculating remaining |
| 548 | } else { |
| 549 | page_count_start + (current_page_number - 1) - 1 # -1 because we're calculating remaining |
| 550 | } |
| 551 | |
| 552 | # Calculate remaining pages based on maxFetch if specified |
| 553 | if (!is.na(maxFetch) && maxFetch < kqo@totalResults) { |
| 554 | # We need to fetch up to maxFetch results |
| 555 | remaining_items_to_fetch <- maxFetch - nrow(collectedMatches) |
| 556 | remaining_pages <- ceiling(remaining_items_to_fetch / maxResultsPerPage) |
| 557 | } else { |
| 558 | # We need to fetch all results - account for our actual global position |
| 559 | # For randomized order, calculate remaining pages based on the randomized list or maxFetch |
| 560 | if (randomizePageOrder) { |
| 561 | if (exists("pages") && length(pages) > 0) { |
| 562 | remaining_pages <- length(pages) - page_index |
| 563 | } else if (!is.na(maxFetch)) { |
| 564 | # If pages is not available, use maxFetch to estimate remaining pages |
| 565 | remaining_pages <- ceiling(maxFetch / maxResultsPerPage) - page_index |
| 566 | } else { |
| 567 | # Fallback to a reasonable default |
| 568 | remaining_pages <- 1 |
| 569 | } |
| 570 | } else { |
| 571 | # For sequential order, use the current global position |
| 572 | remaining_pages <- total_pages - current_global_position |
| 573 | } |
| 574 | } |
Marc Kupietz | ae9b617 | 2025-05-02 15:50:01 +0200 | [diff] [blame] | 575 | |
| 576 | estimated_remaining_seconds <- remaining_pages * time_per_page |
| 577 | estimated_completion_time <- Sys.time() + estimated_remaining_seconds |
| 578 | |
| 579 | # Format time nicely |
| 580 | format_duration <- function(seconds) { |
| 581 | if (is.na(seconds) || seconds < 0) { |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 582 | # Instead of "N/A", return "00s" as a fallback |
| 583 | return("00s") |
Marc Kupietz | ae9b617 | 2025-05-02 15:50:01 +0200 | [diff] [blame] | 584 | } |
| 585 | days <- floor(seconds / (24 * 3600)) |
| 586 | seconds <- seconds %% (24 * 3600) |
| 587 | hours <- floor(seconds / 3600) |
| 588 | seconds <- seconds %% 3600 |
| 589 | minutes <- floor(seconds / 60) |
| 590 | seconds <- floor(seconds %% 60) |
| 591 | paste0( |
| 592 | if (days > 0) paste0(days, "d ") else "", |
| 593 | if (hours > 0 || days > 0) paste0(sprintf("%02d", hours), "h ") else "", |
| 594 | if (minutes > 0 || hours > 0 || days > 0) paste0(sprintf("%02d", minutes), "m ") else "", |
| 595 | paste0(sprintf("%02d", seconds), "s") |
| 596 | ) |
| 597 | } |
| 598 | |
| 599 | eta_str <- format_duration(estimated_remaining_seconds) |
| 600 | completion_time_str <- format(estimated_completion_time, "%Y-%m-%d %H:%M:%S") |
Marc Kupietz | acbaab0 | 2025-05-01 10:56:35 +0200 | [diff] [blame] | 601 | } |
Marc Kupietz | acbaab0 | 2025-05-01 10:56:35 +0200 | [diff] [blame] | 602 | } |
| 603 | |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 604 | # Create the page display string with proper formatting |
Marc Kupietz | acbaab0 | 2025-05-01 10:56:35 +0200 | [diff] [blame] | 605 | |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 606 | # For global page tracking, calculate the absolute page number |
| 607 | actual_display_number <- if (randomizePageOrder) { |
| 608 | current_offset_page + 1 # In randomized mode, this is the actual page (0-based + 1) |
| 609 | } else { |
| 610 | # In sequential mode, the absolute page number is the actual offset page + 1 (to make it 1-based) |
| 611 | current_offset_page + 1 |
| 612 | } |
| 613 | |
| 614 | # For subsequent calls to fetchNext, we need to calculate the correct page numbers |
| 615 | # based on the current batch being fetched |
| 616 | |
| 617 | # For each call to fetchNext, we want to show 1/2, 2/2 (not 3/4, 4/4) |
| 618 | # Simply count from 1 within the current batch |
| 619 | |
| 620 | # The relative page number is simply the current position in this batch |
| 621 | if (randomizePageOrder) { |
| 622 | relative_page_number <- page_index # In randomized mode, we start from 1 in each batch |
| 623 | } else { |
| 624 | relative_page_number <- display_page_number - (page_count_start - 1) |
| 625 | } |
| 626 | |
| 627 | # How many pages will we fetch in this batch? |
| 628 | # If maxFetch is specified, calculate based on it |
| 629 | pages_in_this_batch <- if (!is.na(maxFetch)) { |
| 630 | ceiling(maxFetch / maxResultsPerPage) |
| 631 | } else { |
| 632 | # Otherwise fetch all remaining pages |
| 633 | total_pages - page_count_start + 1 |
| 634 | } |
| 635 | |
| 636 | # The total pages to be shown in this batch |
| 637 | batch_total_pages <- pages_in_this_batch |
| 638 | |
| 639 | page_display <- paste0( |
| 640 | "Retrieved page ", |
| 641 | sprintf(paste0("%", nchar(batch_total_pages), "d"), relative_page_number), |
| 642 | "/", |
| 643 | sprintf("%d", batch_total_pages) |
| 644 | ) |
| 645 | |
| 646 | # If randomized, also show which actual page we fetched |
| 647 | if (randomizePageOrder) { |
| 648 | # Determine the maximum width needed for page numbers (based on total pages) |
| 649 | # This ensures consistent alignment |
| 650 | max_page_width <- nchar(as.character(total_pages)) |
| 651 | # Add the actual page number that was fetched (0-based + 1 for display) with proper padding |
Marc Kupietz | 7638ca4 | 2025-05-25 13:18:16 +0200 | [diff] [blame] | 652 | page_display <- paste0( |
| 653 | page_display, |
| 654 | sprintf(" (actual page %*d)", max_page_width, current_offset_page + 1) |
| 655 | ) |
Marc Kupietz | 623d712 | 2025-05-25 12:46:12 +0200 | [diff] [blame] | 656 | } |
| 657 | # Always show the absolute page number and total pages (for clarity) |
| 658 | else { |
| 659 | # Show the absolute page number (out of total possible pages) |
| 660 | page_display <- paste0(page_display, sprintf( |
| 661 | " (page %d of %d total)", |
| 662 | actual_display_number, total_pages |
| 663 | )) |
| 664 | } |
| 665 | |
| 666 | # Add caching or timing information |
| 667 | if (!is.null(res$meta$cached)) { |
| 668 | page_display <- paste0(page_display, " [cached]") |
| 669 | } else { |
| 670 | page_display <- paste0( |
| 671 | page_display, |
| 672 | " in ", |
| 673 | if (!is.na(time_per_page)) sprintf("%4.1f", time_per_page) else "?", |
| 674 | "s. ETA: ", |
| 675 | # Display ETA for both randomized and sequential modes |
| 676 | eta_str, |
| 677 | # Show completion time for both modes |
| 678 | paste0(" (", completion_time_str, ")") |
| 679 | ) |
| 680 | } |
| 681 | |
| 682 | log_info(verbose, paste0(page_display, "\n")) |
| 683 | |
| 684 | # Increment the appropriate counter based on mode |
| 685 | if (randomizePageOrder) { |
| 686 | page_index <- page_index + 1 |
| 687 | } else { |
| 688 | current_page_number <- current_page_number + 1 |
| 689 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 690 | results <- results + res$meta$itemsPerPage |
Marc Kupietz | e8bd49b | 2024-06-28 07:24:44 +0200 | [diff] [blame] | 691 | if (nrow(collectedMatches) >= kqo@totalResults || (!is.na(maxFetch) && results >= maxFetch)) { |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 692 | break |
| 693 | } |
| 694 | } |
Marc Kupietz | 6817095 | 2021-06-30 09:37:21 +0200 | [diff] [blame] | 695 | nextStartIndex <- min(res$meta$startIndex + res$meta$itemsPerPage, kqo@totalResults) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 696 | KorAPQuery( |
| 697 | nextStartIndex = nextStartIndex, |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 698 | korapConnection = kqo@korapConnection, |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 699 | fields = kqo@fields, |
| 700 | requestUrl = kqo@requestUrl, |
| 701 | request = kqo@request, |
Marc Kupietz | 6817095 | 2021-06-30 09:37:21 +0200 | [diff] [blame] | 702 | totalResults = kqo@totalResults, |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 703 | vc = kqo@vc, |
| 704 | webUIRequestUrl = kqo@webUIRequestUrl, |
Marc Kupietz | 6817095 | 2021-06-30 09:37:21 +0200 | [diff] [blame] | 705 | hasMoreMatches = (kqo@totalResults > nextStartIndex), |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 706 | apiResponse = res, |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 707 | collectedMatches = collectedMatches |
| 708 | ) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 709 | }) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 710 | |
| 711 | #' Fetch all results of a KorAP query. |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 712 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 713 | #' **`fetchAll`** fetches all results of a KorAP query. |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame] | 714 | #' |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 715 | #' @examples |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 716 | #' \dontrun{ |
| 717 | #' |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 718 | #' q <- KorAPConnection() %>% |
| 719 | #' corpusQuery("Ameisenplage") %>% |
| 720 | #' fetchAll() |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 721 | #' q@collectedMatches |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 722 | #' } |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 723 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 724 | #' @aliases fetchAll |
| 725 | #' @rdname KorAPQuery-class |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 726 | #' @export |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 727 | setMethod("fetchAll", "KorAPQuery", function(kqo, verbose = kqo@korapConnection@verbose, ...) { |
| 728 | return(fetchNext(kqo, offset = 0, maxFetch = NA, verbose = verbose, ...)) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 729 | }) |
| 730 | |
| 731 | #' Fetches the remaining results of a KorAP query. |
| 732 | #' |
| 733 | #' @examples |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 734 | #' \dontrun{ |
| 735 | #' |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 736 | #' q <- KorAPConnection() %>% |
| 737 | #' corpusQuery("Ameisenplage") %>% |
| 738 | #' fetchRest() |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 739 | #' q@collectedMatches |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 740 | #' } |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 741 | #' |
| 742 | #' @aliases fetchRest |
| 743 | #' @rdname KorAPQuery-class |
| 744 | #' @export |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 745 | setMethod("fetchRest", "KorAPQuery", function(kqo, verbose = kqo@korapConnection@verbose, ...) { |
| 746 | return(fetchNext(kqo, maxFetch = NA, verbose = verbose, ...)) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 747 | }) |
| 748 | |
Marc Kupietz | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 749 | #' Query frequencies of search expressions in virtual corpora |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 750 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 751 | #' **`frequencyQuery`** combines [corpusQuery()], [corpusStats()] and |
Marc Kupietz | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 752 | #' [ci()] to compute a tibble with the absolute and relative frequencies and |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 753 | #' confidence intervals of one ore multiple search terms across one or multiple |
| 754 | #' virtual corpora. |
| 755 | #' |
| 756 | #' @aliases frequencyQuery |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 757 | #' @examples |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 758 | #' \dontrun{ |
| 759 | #' |
Marc Kupietz | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 760 | #' KorAPConnection(verbose = TRUE) |> |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 761 | #' frequencyQuery(c("Mücke", "Schnake"), paste0("pubDate in ", 2000:2003)) |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 762 | #' } |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 763 | #' |
Marc Kupietz | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 764 | # @inheritParams corpusQuery |
Marc Kupietz | 617266d | 2025-02-27 10:43:07 +0100 | [diff] [blame] | 765 | #' @param kco [KorAPConnection()] object (obtained e.g. from `KorAPConnection()` |
Marc Kupietz | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 766 | #' @param query corpus query string(s.) (can be a vector). The query language depends on the `ql` parameter. Either `query` must be provided or `KorAPUrl`. |
| 767 | #' @param vc virtual corpus definition(s) (can be a vector) |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 768 | #' @param conf.level confidence level of the returned confidence interval (passed through [ci()] to [prop.test()]). |
| 769 | #' @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 | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 770 | #' @param ... further arguments passed to or from other methods (see [corpusQuery()]), most notably `expand`, a logical that decides if `query` and `vc` parameters are expanded to all of their combinations. It defaults to `TRUE`, if `query` and `vc` have different lengths, and to `FALSE` otherwise. |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 771 | #' @export |
Marc Kupietz | ad8d2ed | 2025-04-05 15:37:38 +0200 | [diff] [blame] | 772 | #' |
| 773 | #' @return A tibble, with each row containing the following result columns for query and vc combinations: |
| 774 | #' - **query**: the query string used for the frequency analysis. |
| 775 | #' - **totalResults**: absolute frequency of query matches in the vc. |
| 776 | #' - **vc**: virtual corpus used for the query. |
| 777 | #' - **webUIRequestUrl**: URL of the corresponding web UI request with respect to query and vc. |
| 778 | #' - **total**: total number of words in vc. |
| 779 | #' - **f**: relative frequency of query matches in the vc. |
| 780 | #' - **conf.low**: lower bound of the confidence interval for the relative frequency, given `conf.level`. |
| 781 | #' - **conf.high**: upper bound of the confidence interval for the relative frequency, given `conf.level`. |
| 782 | |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 783 | setMethod( |
| 784 | "frequencyQuery", "KorAPConnection", |
Marc Kupietz | 71d6e05 | 2019-11-22 18:42:10 +0100 | [diff] [blame] | 785 | function(kco, query, vc = "", conf.level = 0.95, as.alternatives = FALSE, ...) { |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 786 | (if (as.alternatives) { |
| 787 | corpusQuery(kco, query, vc, metadataOnly = TRUE, as.df = TRUE, ...) |> |
Marc Kupietz | 71d6e05 | 2019-11-22 18:42:10 +0100 | [diff] [blame] | 788 | group_by(vc) %>% |
| 789 | mutate(total = sum(totalResults)) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 790 | } else { |
| 791 | corpusQuery(kco, query, vc, metadataOnly = TRUE, as.df = TRUE, ...) |> |
| 792 | mutate(total = corpusStats(kco, vc = vc, as.df = TRUE)$tokens) |
| 793 | }) %>% |
Marc Kupietz | 0c29cea | 2019-10-09 08:44:36 +0200 | [diff] [blame] | 794 | ci(conf.level = conf.level) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 795 | } |
| 796 | ) |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 797 | |
Marc Kupietz | 38a9d68 | 2024-12-06 16:17:09 +0100 | [diff] [blame] | 798 | #' buildWebUIRequestUrlFromString |
| 799 | #' |
| 800 | #' @rdname KorAPQuery-class |
| 801 | #' @importFrom urltools url_encode |
| 802 | #' @export |
| 803 | buildWebUIRequestUrlFromString <- function(KorAPUrl, |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 804 | query, |
| 805 | vc = "", |
| 806 | ql = "poliqarp") { |
Marc Kupietz | 38a9d68 | 2024-12-06 16:17:09 +0100 | [diff] [blame] | 807 | if ("KorAPConnection" %in% class(KorAPUrl)) { |
| 808 | KorAPUrl <- KorAPUrl@KorAPUrl |
| 809 | } |
| 810 | |
| 811 | request <- |
| 812 | paste0( |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 813 | "?q=", |
Marc Kupietz | 38a9d68 | 2024-12-06 16:17:09 +0100 | [diff] [blame] | 814 | urltools::url_encode(enc2utf8(as.character(query))), |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 815 | ifelse(vc != "", |
| 816 | paste0("&cq=", urltools::url_encode(enc2utf8(vc))), |
| 817 | "" |
| 818 | ), |
| 819 | "&ql=", |
Marc Kupietz | 38a9d68 | 2024-12-06 16:17:09 +0100 | [diff] [blame] | 820 | ql |
| 821 | ) |
| 822 | paste0(KorAPUrl, request) |
| 823 | } |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 824 | |
| 825 | #' buildWebUIRequestUrl |
| 826 | #' |
| 827 | #' @rdname KorAPQuery-class |
Marc Kupietz | f912959 | 2025-01-26 19:17:54 +0100 | [diff] [blame] | 828 | #' @importFrom httr2 url_parse |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 829 | #' @export |
| 830 | buildWebUIRequestUrl <- function(kco, |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 831 | query = if (missing(KorAPUrl)) { |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 832 | stop("At least one of the parameters query and KorAPUrl must be specified.", call. = FALSE) |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 833 | } else { |
| 834 | httr2::url_parse(KorAPUrl)$query$q |
| 835 | }, |
Marc Kupietz | f912959 | 2025-01-26 19:17:54 +0100 | [diff] [blame] | 836 | vc = if (missing(KorAPUrl)) "" else httr2::url_parse(KorAPUrl)$query$cq, |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 837 | KorAPUrl, |
Marc Kupietz | f912959 | 2025-01-26 19:17:54 +0100 | [diff] [blame] | 838 | ql = if (missing(KorAPUrl)) "poliqarp" else httr2::url_parse(KorAPUrl)$query$ql) { |
Marc Kupietz | 38a9d68 | 2024-12-06 16:17:09 +0100 | [diff] [blame] | 839 | buildWebUIRequestUrlFromString(kco@KorAPUrl, query, vc, ql) |
Marc Kupietz | dbd431a | 2021-08-29 12:17:45 +0200 | [diff] [blame] | 840 | } |
| 841 | |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 842 | #' format() |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 843 | #' @rdname KorAPQuery-class |
| 844 | #' @param x KorAPQuery object |
| 845 | #' @param ... further arguments passed to or from other methods |
Marc Kupietz | b73ca0f | 2025-01-28 20:45:01 +0100 | [diff] [blame] | 846 | #' @importFrom urltools param_get url_decode |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 847 | #' @export |
| 848 | format.KorAPQuery <- function(x, ...) { |
| 849 | cat("<KorAPQuery>\n") |
| 850 | q <- x |
Marc Kupietz | d885122 | 2025-05-01 10:57:19 +0200 | [diff] [blame] | 851 | param <- urltools::param_get(q@request) |> lapply(urltools::url_decode) |
Marc Kupietz | b73ca0f | 2025-01-28 20:45:01 +0100 | [diff] [blame] | 852 | cat(" Query: ", param$q, "\n") |
| 853 | if (!is.null(param$cq) && param$cq != "") { |
| 854 | cat(" Virtual corpus: ", param$cq, "\n") |
| 855 | } |
| 856 | if (!is.null(q@collectedMatches)) { |
| 857 | cat("==============================================================================================================", "\n") |
| 858 | print(summary(q@collectedMatches)) |
| 859 | cat("==============================================================================================================", "\n") |
| 860 | } |
| 861 | cat(" Total results: ", q@totalResults, "\n") |
| 862 | cat(" Fetched results: ", q@nextStartIndex, "\n") |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 863 | } |
| 864 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 865 | #' show() |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 866 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 867 | #' @rdname KorAPQuery-class |
| 868 | #' @param object KorAPQuery object |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 869 | #' @export |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 870 | setMethod("show", "KorAPQuery", function(object) { |
| 871 | format(object) |
| 872 | }) |