blob: db5ad6f6ea1a7a8bd5b131648e4e47c5658f89ef [file] [log] [blame]
Marc Kupietze95108e2019-09-18 13:23:58 +02001#' Class KorAPQuery
2#'
Marc Kupietza6e4ee62021-03-05 09:00:15 +01003#' This class provides methods to perform different kinds of queries on the KorAP API server.
Marc Kupietz67edcb52021-09-20 21:54:24 +02004#' `KorAPQuery` objects, which are typically created by the [corpusQuery()] method,
Marc Kupietza6e4ee62021-03-05 09:00:15 +01005#' represent the current state of a query to a KorAP server.
Marc Kupietze95108e2019-09-18 13:23:58 +02006#'
7#' @include KorAPConnection.R
Marc Kupietzf9129592025-01-26 19:17:54 +01008#' @import httr2
Marc Kupietze95108e2019-09-18 13:23:58 +02009#'
Marc Kupietza6e4ee62021-03-05 09:00:15 +010010#' @include RKorAPClient-package.R
Marc Kupietz5bbc9db2019-08-30 16:30:45 +020011
Marc Kupietze95108e2019-09-18 13:23:58 +020012#' @export
13KorAPQuery <- setClass("KorAPQuery", slots = c(
Marc Kupietzb8972182019-09-20 21:33:46 +020014 "korapConnection",
Marc Kupietze95108e2019-09-18 13:23:58 +020015 "request",
16 "vc",
17 "totalResults",
18 "nextStartIndex",
19 "fields",
20 "requestUrl",
21 "webUIRequestUrl",
22 "apiResponse",
23 "collectedMatches",
24 "hasMoreMatches"
25))
Marc Kupietz5bbc9db2019-08-30 16:30:45 +020026
Marc Kupietze95108e2019-09-18 13:23:58 +020027#' Method initialize
28#'
29#' @rdname KorAPQuery-class
30#' @param .Object …
Marc Kupietzb8972182019-09-20 21:33:46 +020031#' @param korapConnection KorAPConnection object
Marc Kupietze95108e2019-09-18 13:23:58 +020032#' @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 Kupietz7776dec2019-09-27 16:59:02 +020040#' @param hasMoreMatches logical that signals if more query results can be fetched
Marc Kupietze95108e2019-09-18 13:23:58 +020041#' @param collectedMatches matches already fetched from the KorAP-API-server
Marc Kupietz97a1bca2019-10-04 22:52:09 +020042#'
43#' @importFrom tibble tibble
Marc Kupietze95108e2019-09-18 13:23:58 +020044#' @export
45setMethod("initialize", "KorAPQuery",
Marc Kupietzb8972182019-09-20 21:33:46 +020046 function(.Object, korapConnection = NULL, request = NULL, vc="", totalResults=0, nextStartIndex=0, fields=c("corpusSigle", "textSigle", "pubDate", "pubPlace",
Marc Kupietz2078bde2023-08-27 16:46:15 +020047 "availability", "textClass", "snippet", "tokens"),
Marc Kupietze95108e2019-09-18 13:23:58 +020048 requestUrl="", webUIRequestUrl = "", apiResponse = NULL, hasMoreMatches= FALSE, collectedMatches = NULL) {
49 .Object <- callNextMethod()
Marc Kupietzb8972182019-09-20 21:33:46 +020050 .Object@korapConnection = korapConnection
Marc Kupietze95108e2019-09-18 13:23:58 +020051 .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 Kupietz632cbd42019-09-06 16:04:51 +020063
Marc Kupietze95108e2019-09-18 13:23:58 +020064setGeneric("corpusQuery", function(kco, ...) standardGeneric("corpusQuery") )
65setGeneric("fetchAll", function(kqo, ...) standardGeneric("fetchAll") )
66setGeneric("fetchNext", function(kqo, ...) standardGeneric("fetchNext") )
67setGeneric("fetchRest", function(kqo, ...) standardGeneric("fetchRest") )
Marc Kupietz3f575282019-10-04 14:46:04 +020068setGeneric("frequencyQuery", function(kco, ...) standardGeneric("frequencyQuery") )
Marc Kupietze95108e2019-09-18 13:23:58 +020069
70maxResultsPerPage <- 50
Marc Kupietz62da2b52019-09-12 17:43:34 +020071
Marc Kupietz4de53ec2019-10-04 09:12:00 +020072## quiets concerns of R CMD check re: the .'s that appear in pipelines
73if(getRversion() >= "2.15.1") utils::globalVariables(c("."))
Marc Kupietz632cbd42019-09-06 16:04:51 +020074
Marc Kupietzdbd431a2021-08-29 12:17:45 +020075#' Corpus query
76#'
Marc Kupietz67edcb52021-09-20 21:54:24 +020077#' **`corpusQuery`** performs a corpus query via a connection to a KorAP-API-server
Marc Kupietze95108e2019-09-18 13:23:58 +020078#'
Marc Kupietzdbd431a2021-08-29 12:17:45 +020079#' @rdname KorAPQuery-class
80#' @aliases corpusQuery
81#'
82#' @importFrom urltools url_encode
83#' @importFrom purrr pmap
84#' @importFrom dplyr bind_rows
85#'
Marc Kupietz67edcb52021-09-20 21:54:24 +020086#' @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 Kupietz632cbd42019-09-06 16:04:51 +020088#' @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 Kupietz67edcb52021-09-20 21:54:24 +020089#' @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 Kupietz132f0052023-04-16 14:23:05 +020090#' @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.
91#' If you want your corpus queries to return not only metadata, but also KWICS, you need to authorize
92#' your RKorAPClient application as explained in the
93#' [authorization section](https://github.com/KorAP/RKorAPClient#authorization)
94#' of the RKorAPClient Readme on GitHub and set the `metadataOnly` parameter to
95#' `FALSE`.
Marc Kupietz67edcb52021-09-20 21:54:24 +020096#' @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.
Akron5e135462019-09-27 16:31:38 +020097#' @param fields (meta)data fields that will be fetched for every match.
Marc Kupietz43a6ade2020-02-18 17:01:44 +010098#' @param accessRewriteFatal abort if query or given vc had to be rewritten due to insufficient rights (not yet implemented).
Marc Kupietz25aebc32019-09-16 18:40:50 +020099#' @param verbose print some info
Marc Kupietz4de53ec2019-10-04 09:12:00 +0200100#' @param as.df return result as data frame instead of as S4 object?
Marc Kupietz67edcb52021-09-20 21:54:24 +0200101#' @param expand logical that decides if `query` and `vc` parameters are expanded to all of their combinations
Marc Kupietzd9b2fd72023-04-17 19:08:50 +0200102#' @param context string that specifies the size of the left and the right context returned in `snippet`
103#' (provided that `metadataOnly` is set to `false` and that the necessary access right are met).
104#' 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).
105#' If the parameter is not set, the default context size secification of the KorAP server instance will be used.
106#' Note that you cannot overrule the maximum context size set in the KorAP server instance,
107#' as this is typically legally motivated.
Marc Kupietz67edcb52021-09-20 21:54:24 +0200108#' @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()]).
109#' A corresponding URL to be used within a web browser is contained in `@webUIRequestUrl`
110#' 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 Kupietz632cbd42019-09-06 16:04:51 +0200111#'
112#' @examples
Marc Kupietz6ae76052021-09-21 10:34:00 +0200113#' \dontrun{
114#'
Marc Kupietz603491f2019-09-18 14:01:02 +0200115#' # Fetch metadata of every query hit for "Ameisenplage" and show a summary
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200116#' new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll()
Marc Kupietz657d8e72020-02-25 18:31:50 +0100117#' }
Marc Kupietz3c531f62019-09-13 12:17:24 +0200118#'
Marc Kupietz6ae76052021-09-21 10:34:00 +0200119#' \dontrun{
120#'
Marc Kupietz603491f2019-09-18 14:01:02 +0200121#' # Use the copy of a KorAP-web-frontend URL for an API query of "Ameise" in a virtual corpus
122#' # and show the number of query hits (but don't fetch them).
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200123#'
124#' new("KorAPConnection", verbose = TRUE) %>%
125#' corpusQuery(KorAPUrl =
126#' "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp")
Marc Kupietz6ae76052021-09-21 10:34:00 +0200127#' }
128#'
129#' \dontrun{
Marc Kupietz3c531f62019-09-13 12:17:24 +0200130#'
Marc Kupietz603491f2019-09-18 14:01:02 +0200131#' # Plot the time/frequency curve of "Ameisenplage"
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200132#' new("KorAPConnection", verbose=TRUE) %>%
133#' { . ->> kco } %>%
134#' corpusQuery("Ameisenplage") %>%
135#' fetchAll() %>%
136#' slot("collectedMatches") %>%
137#' mutate(year = lubridate::year(pubDate)) %>%
Marc Kupietz19e2ebd2019-10-07 11:45:30 +0200138#' dplyr::select(year) %>%
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200139#' group_by(year) %>%
Marc Kupietzcb3c59e2020-06-02 10:10:43 +0200140#' summarise(Count = dplyr::n()) %>%
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200141#' mutate(Freq = mapply(function(f, y)
142#' f / corpusStats(kco, paste("pubDate in", y))@tokens, Count, year)) %>%
Marc Kupietz19e2ebd2019-10-07 11:45:30 +0200143#' dplyr::select(-Count) %>%
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200144#' complete(year = min(year):max(year), fill = list(Freq = 0)) %>%
145#' plot(type = "l")
Marc Kupietz05b22772020-02-18 21:58:42 +0100146#' }
Marc Kupietz67edcb52021-09-20 21:54:24 +0200147#' @seealso [KorAPConnection()], [fetchNext()], [fetchRest()], [fetchAll()], [corpusStats()]
Marc Kupietz632cbd42019-09-06 16:04:51 +0200148#'
149#' @references
Marc Kupietz67edcb52021-09-20 21:54:24 +0200150#' <https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026>
Marc Kupietz632cbd42019-09-06 16:04:51 +0200151#'
152#' @export
Marc Kupietze95108e2019-09-18 13:23:58 +0200153setMethod("corpusQuery", "KorAPConnection",
Marc Kupietza96537f2019-11-09 23:07:44 +0100154 function(kco,
155 query = if (missing(KorAPUrl))
156 stop("At least one of the parameters query and KorAPUrl must be specified.", call. = FALSE)
157 else
Marc Kupietzf9129592025-01-26 19:17:54 +0100158 httr2::url_parse(KorAPUrl)$query$q,
159 vc = if (missing(KorAPUrl)) "" else httr2::url_parse(KorAPUrl)$query$cq,
Marc Kupietza96537f2019-11-09 23:07:44 +0100160 KorAPUrl,
161 metadataOnly = TRUE,
Marc Kupietzf9129592025-01-26 19:17:54 +0100162 ql = if (missing(KorAPUrl)) "poliqarp" else httr2::url_parse(KorAPUrl)$query$ql,
Marc Kupietza96537f2019-11-09 23:07:44 +0100163 fields = c(
164 "corpusSigle",
165 "textSigle",
166 "pubDate",
167 "pubPlace",
168 "availability",
169 "textClass",
Marc Kupietz2078bde2023-08-27 16:46:15 +0200170 "snippet",
171 "tokens"
Marc Kupietza96537f2019-11-09 23:07:44 +0100172 ),
173 accessRewriteFatal = TRUE,
174 verbose = kco@verbose,
175 expand = length(vc) != length(query),
Marc Kupietzd9b2fd72023-04-17 19:08:50 +0200176 as.df = FALSE,
177 context = NULL) {
Marc Kupietza96537f2019-11-09 23:07:44 +0100178 if (length(query) > 1 || length(vc) > 1) {
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200179 grid <- if (expand) expand_grid(query=query, vc=vc) else tibble(query=query, vc=vc)
180 purrr::pmap(grid, function(query, vc, ...)
181 corpusQuery(kco, query=query, vc=vc, ql=ql, verbose=verbose, as.df = TRUE)) %>%
182 bind_rows()
183 } else {
Marc Kupietz2078bde2023-08-27 16:46:15 +0200184 contentFields <- c("snippet", "tokens")
Marc Kupietza96537f2019-11-09 23:07:44 +0100185 if (metadataOnly) {
186 fields <- fields[!fields %in% contentFields]
187 }
Marc Kupietz80dc6432025-02-07 16:57:40 +0100188 if (!"textSigle" %in% fields) {
189 fields <- c(fields, "textSigle")
190 }
Marc Kupietza96537f2019-11-09 23:07:44 +0100191 request <-
192 paste0('?q=',
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200193 url_encode(enc2utf8(query)),
Marc Kupietzd9b2fd72023-04-17 19:08:50 +0200194 ifelse (!metadataOnly && ! is.null(context) && context != '', paste0('&context=', url_encode(enc2utf8(context))), ''),
Marc Kupietz2078bde2023-08-27 16:46:15 +0200195 ifelse (vc != '', paste0('&cq=', url_encode(enc2utf8(vc))), ''),
196 ifelse (!metadataOnly, '&show-tokens=true', ''),
197 '&ql=', ql)
Marc Kupietza96537f2019-11-09 23:07:44 +0100198 webUIRequestUrl <- paste0(kco@KorAPUrl, request)
199 requestUrl <- paste0(
200 kco@apiUrl,
201 'search',
202 request,
203 '&fields=',
204 paste(fields, collapse = ","),
205 if (metadataOnly) '&access-rewrite-disabled=true' else ''
206 )
Marc Kupietz16ccf112025-01-26 13:25:27 +0100207 log_info(verbose, "\rSearching \"", query, "\" in \"", vc, "\"", sep =
Marc Kupietza96537f2019-11-09 23:07:44 +0100208 "")
209 res = apiCall(kco, paste0(requestUrl, '&count=0'))
Marc Kupietza4675722022-02-23 23:55:15 +0100210 if (is.null(res)) {
Marc Kupietza4675722022-02-23 23:55:15 +0100211 message("API call failed.")
212 totalResults <- 0
213 } else {
Marc Kupietz41d4e352024-03-11 21:48:55 +0100214 totalResults <-as.integer(res$meta$totalResults)
Marc Kupietza47d1502023-04-18 15:26:47 +0200215 log_info(verbose, ": ", totalResults, " hits")
Marc Kupietza4675722022-02-23 23:55:15 +0100216 if(!is.null(res$meta$cached))
Marc Kupietza47d1502023-04-18 15:26:47 +0200217 log_info(verbose, " [cached]\n")
Marc Kupietza4675722022-02-23 23:55:15 +0100218 else
Marc Kupietz16ccf112025-01-26 13:25:27 +0100219 if(! is.null(res$meta$benchmark))
220 log_info(verbose, ", took ", res$meta$benchmark, "\n", sep = "")
221 else
222 log_info(verbose, "\n")
Marc Kupietza4675722022-02-23 23:55:15 +0100223 }
Marc Kupietza96537f2019-11-09 23:07:44 +0100224 if (as.df)
225 data.frame(
226 query = query,
Marc Kupietza4675722022-02-23 23:55:15 +0100227 totalResults = totalResults,
Marc Kupietza96537f2019-11-09 23:07:44 +0100228 vc = vc,
229 webUIRequestUrl = webUIRequestUrl,
230 stringsAsFactors = FALSE
231 )
232 else
233 KorAPQuery(
234 korapConnection = kco,
235 nextStartIndex = 0,
236 fields = fields,
237 requestUrl = requestUrl,
238 request = request,
Marc Kupietza4675722022-02-23 23:55:15 +0100239 totalResults = totalResults,
Marc Kupietza96537f2019-11-09 23:07:44 +0100240 vc = vc,
241 apiResponse = res,
242 webUIRequestUrl = webUIRequestUrl,
Marc Kupietza4675722022-02-23 23:55:15 +0100243 hasMoreMatches = (totalResults > 0),
Marc Kupietza96537f2019-11-09 23:07:44 +0100244 )
245 }
Marc Kupietz4de53ec2019-10-04 09:12:00 +0200246 })
Marc Kupietz5bbc9db2019-08-30 16:30:45 +0200247
Marc Kupietz05a60792024-12-07 16:23:31 +0100248#' @importFrom purrr map
249repair_data_strcuture <- function(x) {
250 if (is.list(x))
251 as.character (purrr::map(x, ~ if (length(.x) > 1) {
252 paste(.x, collapse = " ")
253 } else {
254 .x
255 }))
256 else
257 ifelse(is.na(x), "", x)
258}
259
Marc Kupietz62da2b52019-09-12 17:43:34 +0200260#' Fetch the next bunch of results of a KorAP query.
Marc Kupietze95108e2019-09-18 13:23:58 +0200261#'
Marc Kupietz67edcb52021-09-20 21:54:24 +0200262#' **`fetchNext`** fetches the next bunch of results of a KorAP query.
Marc Kupietz3f575282019-10-04 14:46:04 +0200263#'
Marc Kupietz67edcb52021-09-20 21:54:24 +0200264#' @param kqo object obtained from [corpusQuery()]
Marc Kupietz62da2b52019-09-12 17:43:34 +0200265#' @param offset start offset for query results to fetch
266#' @param maxFetch maximum number of query results to fetch
Marc Kupietz25aebc32019-09-16 18:40:50 +0200267#' @param verbose print progress information if true
Marc Kupietz67edcb52021-09-20 21:54:24 +0200268#' @param randomizePageOrder fetch result pages in pseudo random order if true. Use [set.seed()] to set seed for reproducible results.
269#' @return The `kqo` input object with updated slots `collectedMatches`, `apiResponse`, `nextStartIndex`, `hasMoreMatches`
Marc Kupietz62da2b52019-09-12 17:43:34 +0200270#'
Marc Kupietz05b22772020-02-18 21:58:42 +0100271#' @examples
Marc Kupietz6ae76052021-09-21 10:34:00 +0200272#' \dontrun{
273#'
274#' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchNext()
Marc Kupietz05b22772020-02-18 21:58:42 +0100275#' q@collectedMatches
Marc Kupietz657d8e72020-02-25 18:31:50 +0100276#' }
Marc Kupietz05b22772020-02-18 21:58:42 +0100277#'
Marc Kupietz62da2b52019-09-12 17:43:34 +0200278#' @references
Marc Kupietz67edcb52021-09-20 21:54:24 +0200279#' <https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026>
Marc Kupietz62da2b52019-09-12 17:43:34 +0200280#'
Marc Kupietze95108e2019-09-18 13:23:58 +0200281#' @aliases fetchNext
282#' @rdname KorAPQuery-class
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200283#' @importFrom dplyr rowwise mutate bind_rows select summarise n select
Marc Kupietzf4881122024-12-17 14:55:39 +0100284#' @importFrom tibble enframe add_column
285#' @importFrom stringr word
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200286#' @importFrom tidyr unnest unchop pivot_wider
287#' @importFrom purrr map
Marc Kupietz632cbd42019-09-06 16:04:51 +0200288#' @export
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200289setMethod("fetchNext", "KorAPQuery", function(kqo,
290 offset = kqo@nextStartIndex,
291 maxFetch = maxResultsPerPage,
292 verbose = kqo@korapConnection@verbose,
293 randomizePageOrder = FALSE) {
Marc Kupietza7a8f1b2024-12-18 15:56:19 +0100294 # https://stackoverflow.com/questions/8096313/no-visible-binding-for-global-variable-note-in-r-cmd-check
295 results <- key <- name <- pubDate <- tmp_positions <- 0
296
Marc Kupietze95108e2019-09-18 13:23:58 +0200297 if (kqo@totalResults == 0 || offset >= kqo@totalResults) {
298 return(kqo)
Marc Kupietz62da2b52019-09-12 17:43:34 +0200299 }
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200300 use_korap_api <- Sys.getenv("USE_KORAP_API", unset = NA)
Marc Kupietz705488d2021-06-30 18:26:36 +0200301 page <- kqo@nextStartIndex / maxResultsPerPage + 1
Marc Kupietze95108e2019-09-18 13:23:58 +0200302 collectedMatches <- kqo@collectedMatches
Marc Kupietz62da2b52019-09-12 17:43:34 +0200303
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200304 if (randomizePageOrder) {
305 pages <- head(sample.int(ceiling(kqo@totalResults / maxResultsPerPage)), maxFetch) - 1
306 }
307
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200308 if(is.null(collectedMatches)) {
309 collectedMatches <- data.frame()
310 }
Marc Kupietz5bbc9db2019-08-30 16:30:45 +0200311 repeat {
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200312 page = nrow(collectedMatches) %/% maxResultsPerPage + 1
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200313 currentOffset = ifelse(randomizePageOrder, pages[page], page - 1) * maxResultsPerPage
314 query <- paste0(kqo@requestUrl, '&count=', min(if (!is.na(maxFetch)) maxFetch - results else maxResultsPerPage, maxResultsPerPage) ,'&offset=', currentOffset, '&cutoff=true')
Marc Kupietz68170952021-06-30 09:37:21 +0200315 res <- apiCall(kqo@korapConnection, query)
316 if (length(res$matches) == 0) {
317 break
318 }
319
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200320 if ("fields" %in% colnames(res$matches) && (is.na(use_korap_api) || as.numeric(use_korap_api) >= 1.0)) {
Marc Kupietz16ccf112025-01-26 13:25:27 +0100321 log_info(verbose, "Using fields API: ")
Marc Kupietz05a60792024-12-07 16:23:31 +0100322 currentMatches <- res$matches$fields %>%
323 purrr::map(~ mutate(.x, value = repair_data_strcuture(value))) %>%
324 tibble::enframe() %>%
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200325 tidyr::unnest(cols = value) %>%
326 tidyr::pivot_wider(names_from = key, id_cols = name, names_repair = "unique") %>%
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200327 dplyr::select(-name)
328 if("snippet" %in% colnames(res$matches)) {
329 currentMatches$snippet <- res$matches$snippet
330 }
Marc Kupietz3cd2c6c2025-01-08 20:35:39 +0100331 if ("tokens" %in% colnames(res$matches)) {
332 currentMatches$tokens <- res$matches$tokens
333 }
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200334 } else {
335 currentMatches <- res$matches
336 }
337
Marc Kupietze95108e2019-09-18 13:23:58 +0200338 for (field in kqo@fields) {
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200339 if (!field %in% colnames(currentMatches)) {
340 currentMatches[, field] <- NA
Marc Kupietz5bbc9db2019-08-30 16:30:45 +0200341 }
342 }
Marc Kupietzf4881122024-12-17 14:55:39 +0100343 currentMatches <- currentMatches %>%
344 select(kqo@fields) %>%
345 mutate(
Marc Kupietz0447da02025-01-08 20:51:09 +0100346 tmp_positions = gsub(".*-p(\\d+)-(\\d+).*", "\\1 \\2", res$matches$matchID),
Marc Kupietzf4881122024-12-17 14:55:39 +0100347 matchStart = as.integer(stringr::word(tmp_positions, 1)),
348 matchEnd = as.integer(stringr::word(tmp_positions, 2)) - 1
349 ) %>%
350 select(-tmp_positions)
351
Marc Kupietz62da2b52019-09-12 17:43:34 +0200352 if (!is.list(collectedMatches)) {
353 collectedMatches <- currentMatches
Marc Kupietz5bbc9db2019-08-30 16:30:45 +0200354 } else {
Marc Kupietz2078bde2023-08-27 16:46:15 +0200355 collectedMatches <- bind_rows(collectedMatches, currentMatches)
Marc Kupietz5bbc9db2019-08-30 16:30:45 +0200356 }
Marc Kupietz16ccf112025-01-26 13:25:27 +0100357 log_info(verbose, paste0(
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200358 "Retrieved page ",
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200359 ceiling(nrow(collectedMatches) / res$meta$itemsPerPage),
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200360 "/",
361 if (!is.na(maxFetch) && maxFetch < kqo@totalResults)
362 sprintf("%d (%d)", ceiling(maxFetch / res$meta$itemsPerPage), ceiling(kqo@totalResults / res$meta$itemsPerPage))
363 else
364 sprintf("%d", ceiling(kqo@totalResults / res$meta$itemsPerPage)),
365 ' in ',
366 res$meta$benchmark,
367 '\n'
368 ))
Marc Kupietz16ccf112025-01-26 13:25:27 +0100369
Marc Kupietz5bbc9db2019-08-30 16:30:45 +0200370 page <- page + 1
371 results <- results + res$meta$itemsPerPage
Marc Kupietze8bd49b2024-06-28 07:24:44 +0200372 if (nrow(collectedMatches) >= kqo@totalResults || (!is.na(maxFetch) && results >= maxFetch)) {
Marc Kupietz5bbc9db2019-08-30 16:30:45 +0200373 break
374 }
375 }
Marc Kupietz68170952021-06-30 09:37:21 +0200376 nextStartIndex <- min(res$meta$startIndex + res$meta$itemsPerPage, kqo@totalResults)
Marc Kupietze95108e2019-09-18 13:23:58 +0200377 KorAPQuery(nextStartIndex = nextStartIndex,
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200378 korapConnection = kqo@korapConnection,
Marc Kupietze95108e2019-09-18 13:23:58 +0200379 fields = kqo@fields,
380 requestUrl = kqo@requestUrl,
381 request = kqo@request,
Marc Kupietz68170952021-06-30 09:37:21 +0200382 totalResults = kqo@totalResults,
Marc Kupietze95108e2019-09-18 13:23:58 +0200383 vc = kqo@vc,
384 webUIRequestUrl = kqo@webUIRequestUrl,
Marc Kupietz68170952021-06-30 09:37:21 +0200385 hasMoreMatches = (kqo@totalResults > nextStartIndex),
Marc Kupietze95108e2019-09-18 13:23:58 +0200386 apiResponse = res,
387 collectedMatches = collectedMatches)
388})
Marc Kupietz62da2b52019-09-12 17:43:34 +0200389
390#' Fetch all results of a KorAP query.
Marc Kupietz62da2b52019-09-12 17:43:34 +0200391#'
Marc Kupietz67edcb52021-09-20 21:54:24 +0200392#' **`fetchAll`** fetches all results of a KorAP query.
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100393#'
Marc Kupietz62da2b52019-09-12 17:43:34 +0200394#' @examples
Marc Kupietz6ae76052021-09-21 10:34:00 +0200395#' \dontrun{
396#'
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200397#' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll()
Marc Kupietze95108e2019-09-18 13:23:58 +0200398#' q@collectedMatches
Marc Kupietz05b22772020-02-18 21:58:42 +0100399#' }
Marc Kupietz62da2b52019-09-12 17:43:34 +0200400#'
Marc Kupietze95108e2019-09-18 13:23:58 +0200401#' @aliases fetchAll
402#' @rdname KorAPQuery-class
Marc Kupietz62da2b52019-09-12 17:43:34 +0200403#' @export
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200404setMethod("fetchAll", "KorAPQuery", function(kqo, verbose = kqo@korapConnection@verbose, ...) {
405 return(fetchNext(kqo, offset = 0, maxFetch = NA, verbose = verbose, ...))
Marc Kupietze95108e2019-09-18 13:23:58 +0200406})
407
408#' Fetches the remaining results of a KorAP query.
409#'
410#' @examples
Marc Kupietz6ae76052021-09-21 10:34:00 +0200411#' \dontrun{
412#'
Marc Kupietz05b22772020-02-18 21:58:42 +0100413#' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchRest()
Marc Kupietze95108e2019-09-18 13:23:58 +0200414#' q@collectedMatches
Marc Kupietz05b22772020-02-18 21:58:42 +0100415#' }
Marc Kupietze95108e2019-09-18 13:23:58 +0200416#'
417#' @aliases fetchRest
418#' @rdname KorAPQuery-class
419#' @export
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200420setMethod("fetchRest", "KorAPQuery", function(kqo, verbose = kqo@korapConnection@verbose, ...) {
421 return(fetchNext(kqo, maxFetch = NA, verbose = verbose, ...))
Marc Kupietze95108e2019-09-18 13:23:58 +0200422})
423
Marc Kupietz3f575282019-10-04 14:46:04 +0200424#' Query relative frequency of search term(s)
425#'
Marc Kupietz67edcb52021-09-20 21:54:24 +0200426#' **`frequencyQuery`** combines [corpusQuery()], [corpusStats()] and
427#' [ci()] to compute a table with the relative frequencies and
Marc Kupietz3f575282019-10-04 14:46:04 +0200428#' confidence intervals of one ore multiple search terms across one or multiple
429#' virtual corpora.
430#'
431#' @aliases frequencyQuery
432#' @rdname KorAPQuery-class
433#' @examples
Marc Kupietz6ae76052021-09-21 10:34:00 +0200434#' \dontrun{
435#'
Marc Kupietz3f575282019-10-04 14:46:04 +0200436#' new("KorAPConnection", verbose = TRUE) %>%
437#' frequencyQuery(c("Mücke", "Schnake"), paste0("pubDate in ", 2000:2003))
Marc Kupietz05b22772020-02-18 21:58:42 +0100438#' }
Marc Kupietz3f575282019-10-04 14:46:04 +0200439#'
Marc Kupietz67edcb52021-09-20 21:54:24 +0200440#' @param kco [KorAPConnection()] object (obtained e.g. from `new("KorAPConnection")`
441#' @param query string that contains the corpus query. The query language depends on the `ql` parameter. Either `query` must be provided or `KorAPUrl`.
442#' @param conf.level confidence level of the returned confidence interval (passed through [ci()] to [prop.test()]).
443#' @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 Kupietz3f575282019-10-04 14:46:04 +0200444#' @export
445setMethod("frequencyQuery", "KorAPConnection",
Marc Kupietz71d6e052019-11-22 18:42:10 +0100446 function(kco, query, vc = "", conf.level = 0.95, as.alternatives = FALSE, ...) {
447 (if (as.alternatives) {
448 corpusQuery(kco, query, vc, metadataOnly = TRUE, as.df = TRUE, ...) %>%
449 group_by(vc) %>%
450 mutate(total = sum(totalResults))
451 } else {
452 corpusQuery(kco, query, vc, metadataOnly = TRUE, as.df = TRUE, ...) %>%
453 mutate(total = corpusStats(kco, vc=vc, as.df=TRUE)$tokens)
454 } ) %>%
Marc Kupietz0c29cea2019-10-09 08:44:36 +0200455 ci(conf.level = conf.level)
Marc Kupietz3f575282019-10-04 14:46:04 +0200456})
457
Marc Kupietz38a9d682024-12-06 16:17:09 +0100458#' buildWebUIRequestUrlFromString
459#'
460#' @rdname KorAPQuery-class
461#' @importFrom urltools url_encode
462#' @export
463buildWebUIRequestUrlFromString <- function(KorAPUrl,
464 query,
465 vc = "",
466 ql = "poliqarp"
467) {
468 if ("KorAPConnection" %in% class(KorAPUrl)) {
469 KorAPUrl <- KorAPUrl@KorAPUrl
470 }
471
472 request <-
473 paste0(
474 '?q=',
475 urltools::url_encode(enc2utf8(as.character(query))),
476 ifelse(vc != '',
477 paste0('&cq=', urltools::url_encode(enc2utf8(vc))),
478 ''),
479 '&ql=',
480 ql
481 )
482 paste0(KorAPUrl, request)
483}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200484
485#' buildWebUIRequestUrl
486#'
487#' @rdname KorAPQuery-class
Marc Kupietzf9129592025-01-26 19:17:54 +0100488#' @importFrom httr2 url_parse
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200489#' @export
490buildWebUIRequestUrl <- function(kco,
491 query = if (missing(KorAPUrl))
492 stop("At least one of the parameters query and KorAPUrl must be specified.", call. = FALSE)
493 else
Marc Kupietzf9129592025-01-26 19:17:54 +0100494 httr2::url_parse(KorAPUrl)$query$q,
495 vc = if (missing(KorAPUrl)) "" else httr2::url_parse(KorAPUrl)$query$cq,
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200496 KorAPUrl,
Marc Kupietzf9129592025-01-26 19:17:54 +0100497 ql = if (missing(KorAPUrl)) "poliqarp" else httr2::url_parse(KorAPUrl)$query$ql) {
Marc Kupietz38a9d682024-12-06 16:17:09 +0100498
499 buildWebUIRequestUrlFromString(kco@KorAPUrl, query, vc, ql)
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200500}
501
Marc Kupietze95108e2019-09-18 13:23:58 +0200502#´ format()
503#' @rdname KorAPQuery-class
504#' @param x KorAPQuery object
505#' @param ... further arguments passed to or from other methods
Marc Kupietzb73ca0f2025-01-28 20:45:01 +0100506#' @importFrom urltools param_get url_decode
Marc Kupietze95108e2019-09-18 13:23:58 +0200507#' @export
508format.KorAPQuery <- function(x, ...) {
509 cat("<KorAPQuery>\n")
510 q <- x
Marc Kupietzb73ca0f2025-01-28 20:45:01 +0100511 param = urltools::param_get(q@request) |> lapply(urltools::url_decode)
512 cat(" Query: ", param$q, "\n")
513 if (!is.null(param$cq) && param$cq != "") {
514 cat(" Virtual corpus: ", param$cq, "\n")
515 }
516 if (!is.null(q@collectedMatches)) {
517 cat("==============================================================================================================", "\n")
518 print(summary(q@collectedMatches))
519 cat("==============================================================================================================", "\n")
520 }
521 cat(" Total results: ", q@totalResults, "\n")
522 cat(" Fetched results: ", q@nextStartIndex, "\n")
Marc Kupietz62da2b52019-09-12 17:43:34 +0200523}
524
Marc Kupietze95108e2019-09-18 13:23:58 +0200525#' show()
Marc Kupietz62da2b52019-09-12 17:43:34 +0200526#'
Marc Kupietze95108e2019-09-18 13:23:58 +0200527#' @rdname KorAPQuery-class
528#' @param object KorAPQuery object
Marc Kupietz62da2b52019-09-12 17:43:34 +0200529#' @export
Marc Kupietze95108e2019-09-18 13:23:58 +0200530setMethod("show", "KorAPQuery", function(object) {
531 format(object)
532})
Marc Kupietz006b47c2021-01-13 17:00:59 +0100533