Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 1 | #' Class KorAPQuery |
| 2 | #' |
| 3 | #' \code{KorAPQuery} objetcs represent the current state of a query to a KorAP server. |
| 4 | #' New \code{KorAPQuery} objects are typically created by the \code{\link{corpusQuery}} method. |
| 5 | #' |
| 6 | #' @include KorAPConnection.R |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 7 | #' @import httr |
| 8 | #' |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 9 | #' @include RKorAPClient.R |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 10 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 11 | #' @export |
| 12 | KorAPQuery <- setClass("KorAPQuery", slots = c( |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 13 | "korapConnection", |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 14 | "request", |
| 15 | "vc", |
| 16 | "totalResults", |
| 17 | "nextStartIndex", |
| 18 | "fields", |
| 19 | "requestUrl", |
| 20 | "webUIRequestUrl", |
| 21 | "apiResponse", |
| 22 | "collectedMatches", |
| 23 | "hasMoreMatches" |
| 24 | )) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 25 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 26 | #' Method initialize |
| 27 | #' |
| 28 | #' @rdname KorAPQuery-class |
| 29 | #' @param .Object … |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 30 | #' @param korapConnection KorAPConnection object |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 31 | #' @param request query part of the request URL |
| 32 | #' @param vc definition of a virtual corpus |
| 33 | #' @param totalResults number of hits the query has yielded |
| 34 | #' @param nextStartIndex at what index to start the next fetch of query results |
| 35 | #' @param fields what data / metadata fields should be collected |
| 36 | #' @param requestUrl complete URL of the API request |
| 37 | #' @param webUIRequestUrl URL of a web frontend request corresponding to the API request |
| 38 | #' @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] | 39 | #' @param hasMoreMatches logical that signals if more query results can be fetched |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 40 | #' @param collectedMatches matches already fetched from the KorAP-API-server |
Marc Kupietz | 97a1bca | 2019-10-04 22:52:09 +0200 | [diff] [blame] | 41 | #' |
| 42 | #' @importFrom tibble tibble |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 43 | #' @export |
| 44 | setMethod("initialize", "KorAPQuery", |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 45 | 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] | 46 | "availability", "textClass", "snippet"), |
| 47 | requestUrl="", webUIRequestUrl = "", apiResponse = NULL, hasMoreMatches= FALSE, collectedMatches = NULL) { |
| 48 | .Object <- callNextMethod() |
Marc Kupietz | b897218 | 2019-09-20 21:33:46 +0200 | [diff] [blame] | 49 | .Object@korapConnection = korapConnection |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 50 | .Object@request = request |
| 51 | .Object@vc = vc |
| 52 | .Object@totalResults = totalResults |
| 53 | .Object@nextStartIndex = nextStartIndex |
| 54 | .Object@fields = fields |
| 55 | .Object@requestUrl = requestUrl |
| 56 | .Object@webUIRequestUrl = webUIRequestUrl |
| 57 | .Object@apiResponse = apiResponse |
| 58 | .Object@hasMoreMatches = hasMoreMatches |
| 59 | .Object@collectedMatches = collectedMatches |
| 60 | .Object |
| 61 | }) |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 62 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 63 | setGeneric("corpusQuery", function(kco, ...) standardGeneric("corpusQuery") ) |
| 64 | setGeneric("fetchAll", function(kqo, ...) standardGeneric("fetchAll") ) |
| 65 | setGeneric("fetchNext", function(kqo, ...) standardGeneric("fetchNext") ) |
| 66 | setGeneric("fetchRest", function(kqo, ...) standardGeneric("fetchRest") ) |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 67 | setGeneric("frequencyQuery", function(kco, ...) standardGeneric("frequencyQuery") ) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 68 | |
| 69 | maxResultsPerPage <- 50 |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 70 | |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 71 | QueryParameterFromUrl <- function(url, parameter) { |
| 72 | regex <- paste0(".*[?&]", parameter, "=([^&]*).*") |
| 73 | if (grepl(regex, url)) { |
| 74 | return(gsub(regex, '\\1', url, perl = TRUE)) |
| 75 | } else { |
| 76 | return("") |
| 77 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 80 | ## quiets concerns of R CMD check re: the .'s that appear in pipelines |
| 81 | if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 82 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 83 | #' Method corpusQuery |
| 84 | #' |
| 85 | #' Perform a corpus query via a connection to a KorAP-API-server. |
| 86 | #' |
| 87 | #' @param kco \code{\link{KorAPConnection}} object (obtained e.g. from \code{new("KorAPConnection")} |
Akron | 5e13546 | 2019-09-27 16:31:38 +0200 | [diff] [blame] | 88 | #' @param query string that contains the corpus query. The query language depends on the \code{ql} parameter. Either \code{query} must be provided or \code{KorAPUrl}. |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 89 | #' @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. |
| 90 | #' @param KorAPUrl instead of providing the query and vc string parameters, you can also simply copy a KorAP query URL from your browser and use it here (and in \code{KorAPConnection}) to provide all necessary information for the query. |
Marc Kupietz | 7776dec | 2019-09-27 16:59:02 +0200 | [diff] [blame] | 91 | #' @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 | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 92 | #' @param ql string to choose the query language (see \href{https://github.com/KorAP/Kustvakt/wiki/Service:-Search-GET#user-content-parameters}{section on Query Parameters} in the Kustvakt-Wiki for possible values. |
Akron | 5e13546 | 2019-09-27 16:31:38 +0200 | [diff] [blame] | 93 | #' @param fields (meta)data fields that will be fetched for every match. |
| 94 | #' @param accessRewriteFatal abort if query or given vc had to be rewritten due to insufficent rights (not yet implemented). |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 95 | #' @param verbose print some info |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 96 | #' @param as.df return result as data frame instead of as S4 object? |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 97 | #' @param expand logical that deicdes if \code{query} and \code{vc} parameters are expanded to all of their combinations |
| 98 | #' @return Depending on the \code{as.df} parameter, a table or a \code{\link{KorAPQuery}} object that, among other information, contains the total number of results in \code{@totalResults}. The resulting object can be used to fetch all query results (with \code{\link{fetchAll}}) or the next page of results (with \code{\link{fetchNext}}). |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 99 | #' A corresponding URL to be used within a web browser is contained in \code{@webUIRequestUrl} |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 100 | #' Please make sure to check \code{$collection$rewrites} to see if any unforseen access rewrites of the query's virtual corpus had to be performed. |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 101 | #' |
| 102 | #' @examples |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 103 | #' # Fetch metadata of every query hit for "Ameisenplage" and show a summary |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 104 | #' new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll() |
Marc Kupietz | 3c531f6 | 2019-09-13 12:17:24 +0200 | [diff] [blame] | 105 | #' |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 106 | #' # Use the copy of a KorAP-web-frontend URL for an API query of "Ameise" in a virtual corpus |
| 107 | #' # and show the number of query hits (but don't fetch them). |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 108 | #' |
| 109 | #' new("KorAPConnection", verbose = TRUE) %>% |
| 110 | #' corpusQuery(KorAPUrl = |
| 111 | #' "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] | 112 | #' |
Marc Kupietz | 603491f | 2019-09-18 14:01:02 +0200 | [diff] [blame] | 113 | #' # Plot the time/frequency curve of "Ameisenplage" |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 114 | #' \donttest{ |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 115 | #' new("KorAPConnection", verbose=TRUE) %>% |
| 116 | #' { . ->> kco } %>% |
| 117 | #' corpusQuery("Ameisenplage") %>% |
| 118 | #' fetchAll() %>% |
| 119 | #' slot("collectedMatches") %>% |
| 120 | #' mutate(year = lubridate::year(pubDate)) %>% |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 121 | #' dplyr::select(year) %>% |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 122 | #' group_by(year) %>% |
| 123 | #' summarise(Count = n()) %>% |
| 124 | #' mutate(Freq = mapply(function(f, y) |
| 125 | #' f / corpusStats(kco, paste("pubDate in", y))@tokens, Count, year)) %>% |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 126 | #' dplyr::select(-Count) %>% |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 127 | #' complete(year = min(year):max(year), fill = list(Freq = 0)) %>% |
| 128 | #' plot(type = "l") |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 129 | #' } |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 130 | #' @seealso \code{\link{KorAPConnection}}, \code{\link{fetchNext}}, \code{\link{fetchRest}}, \code{\link{fetchAll}}, \code{\link{corpusStats}} |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 131 | #' |
| 132 | #' @references |
| 133 | #' \url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026} |
| 134 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 135 | #' @aliases corpusQuery |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 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) { |
| 161 | |
| 162 | grid <- { |
| 163 | if (expand) |
| 164 | expand_grid(query=query, vc=vc) else tibble(query=query, vc=vc) } |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 165 | return( |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 166 | do.call(rbind, |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 167 | Map(function(q, cq) corpusQuery(kco, query=q, vc=cq, ql=ql, |
| 168 | verbose=verbose, as.df = TRUE), grid$query, grid$vc)) %>% |
| 169 | remove_rownames() |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 170 | ) |
| 171 | } else { |
| 172 | contentFields <- c("snippet") |
| 173 | if (metadataOnly) { |
| 174 | fields <- fields[!fields %in% contentFields] |
| 175 | } |
| 176 | request <- |
| 177 | paste0('?q=', |
| 178 | URLencode(query, reserved = TRUE), |
| 179 | if (vc != '') paste0('&cq=', URLencode(vc, reserved = TRUE)) else '', '&ql=', ql) |
| 180 | webUIRequestUrl <- paste0(kco@KorAPUrl, request) |
| 181 | requestUrl <- paste0( |
| 182 | kco@apiUrl, |
| 183 | 'search', |
| 184 | request, |
| 185 | '&fields=', |
| 186 | paste(fields, collapse = ","), |
| 187 | if (metadataOnly) '&access-rewrite-disabled=true' else '' |
| 188 | ) |
| 189 | log.info(verbose, "Searching \"", query, "\" in \"", vc, "\"", sep = |
| 190 | "") |
| 191 | res = apiCall(kco, paste0(requestUrl, '&count=0')) |
Marc Kupietz | f5769b6 | 2019-12-13 09:19:45 +0100 | [diff] [blame] | 192 | if(!is.null(res$meta$cached)) |
| 193 | log.info(verbose, " [cached]\n") |
| 194 | else |
| 195 | log.info(verbose, " took ", res$meta$benchmark, "\n", sep = "") |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 196 | if (as.df) |
| 197 | data.frame( |
| 198 | query = query, |
| 199 | totalResults = res$meta$totalResults, |
| 200 | vc = vc, |
| 201 | webUIRequestUrl = webUIRequestUrl, |
| 202 | stringsAsFactors = FALSE |
| 203 | ) |
| 204 | else |
| 205 | KorAPQuery( |
| 206 | korapConnection = kco, |
| 207 | nextStartIndex = 0, |
| 208 | fields = fields, |
| 209 | requestUrl = requestUrl, |
| 210 | request = request, |
| 211 | totalResults = res$meta$totalResults, |
| 212 | vc = vc, |
| 213 | apiResponse = res, |
| 214 | webUIRequestUrl = webUIRequestUrl, |
| 215 | hasMoreMatches = (res$meta$totalResults > 0), |
| 216 | ) |
| 217 | } |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 218 | }) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 219 | |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 220 | #' Fetch the next bunch of results of a KorAP query. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 221 | #' |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 222 | #' \bold{\code{fetchNext}} fetches the next bunch of results of a KorAP query. |
| 223 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 224 | #' @param kqo object obtained from \code{\link{corpusQuery}} |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 225 | #' @param offset start offset for query results to fetch |
| 226 | #' @param maxFetch maximum number of query results to fetch |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 227 | #' @param verbose print progress information if true |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 228 | #' @return The \code{kqo} input object with updated slots \code{collectedMatches}, \code{apiResponse}, \code{nextStartIndex}, \code{hasMoreMatches} |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 229 | #' |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 230 | #' @examples |
| 231 | #' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchNext() |
| 232 | #' q@collectedMatches |
| 233 | #' |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 234 | #' @references |
| 235 | #' \url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026} |
| 236 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 237 | #' @aliases fetchNext |
| 238 | #' @rdname KorAPQuery-class |
Marc Kupietz | 97a1bca | 2019-10-04 22:52:09 +0200 | [diff] [blame] | 239 | #' @importFrom purrr map_dfr |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 240 | #' @importFrom dplyr rowwise bind_rows select |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 241 | #' @export |
Marc Kupietz | f6f7131 | 2019-09-23 18:35:27 +0200 | [diff] [blame] | 242 | setMethod("fetchNext", "KorAPQuery", function(kqo, offset = kqo@nextStartIndex, maxFetch = maxResultsPerPage, verbose = kqo@korapConnection@verbose) { |
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 | |
| 247 | page <- 1 |
| 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 | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 252 | repeat { |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 253 | res <- apiCall(kqo@korapConnection, paste0(kqo@requestUrl, '&count=', min(if (!is.na(maxFetch)) maxFetch - results else maxResultsPerPage, maxResultsPerPage) ,'&offset=', offset + results)) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 254 | if (res$meta$totalResults == 0) { return(kqo) } |
| 255 | for (field in kqo@fields) { |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 256 | if (!field %in% colnames(res$matches)) { |
| 257 | res$matches[, field] <- NA |
| 258 | } |
| 259 | } |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 260 | currentMatches <- |
| 261 | kqo@fields %>% |
| 262 | map_dfr( ~tibble(!!.x := logical() ) ) %>% |
| 263 | bind_rows(res$matches) %>% |
Marc Kupietz | 19e2ebd | 2019-10-07 11:45:30 +0200 | [diff] [blame] | 264 | dplyr::select(kqo@fields) |
Marc Kupietz | 36d12d9 | 2019-09-27 18:13:27 +0200 | [diff] [blame] | 265 | if ("pubDate" %in% kqo@fields) { |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 266 | currentMatches$pubDate <- currentMatches$pubDate %>% as.Date(format = "%Y-%m-%d") |
| 267 | factorCols <- currentMatches %>% select(-pubDate) %>% colnames() |
Marc Kupietz | 36d12d9 | 2019-09-27 18:13:27 +0200 | [diff] [blame] | 268 | } else { |
| 269 | factorCols <- colnames(currentMatches) |
| 270 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 271 | currentMatches[factorCols] <- lapply(currentMatches[factorCols], factor) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 272 | if (!is.list(collectedMatches)) { |
| 273 | collectedMatches <- currentMatches |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 274 | } else { |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 275 | collectedMatches <- rbind(collectedMatches, currentMatches) |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 276 | } |
Marc Kupietz | c2c59bd | 2019-08-30 16:50:49 +0200 | [diff] [blame] | 277 | if (verbose) { |
Marc Kupietz | f6f7131 | 2019-09-23 18:35:27 +0200 | [diff] [blame] | 278 | cat(paste0("Retrieved page ", page, "/", ceiling((res$meta$totalResults) / res$meta$itemsPerPage), ' in ', res$meta$benchmark, '\n')) |
Marc Kupietz | c2c59bd | 2019-08-30 16:50:49 +0200 | [diff] [blame] | 279 | } |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 280 | page <- page + 1 |
| 281 | results <- results + res$meta$itemsPerPage |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 282 | if (offset + results >= res$meta$totalResults || (!is.na(maxFetch) && results >= maxFetch)) { |
Marc Kupietz | 5bbc9db | 2019-08-30 16:30:45 +0200 | [diff] [blame] | 283 | break |
| 284 | } |
| 285 | } |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 286 | nextStartIndex <- min(res$meta$startIndex + res$meta$itemsPerPage, res$meta$totalResults) |
| 287 | KorAPQuery(nextStartIndex = nextStartIndex, |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 288 | korapConnection = kqo@korapConnection, |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 289 | fields = kqo@fields, |
| 290 | requestUrl = kqo@requestUrl, |
| 291 | request = kqo@request, |
| 292 | totalResults = res$meta$totalResults, |
| 293 | vc = kqo@vc, |
| 294 | webUIRequestUrl = kqo@webUIRequestUrl, |
| 295 | hasMoreMatches = (res$meta$totalResults > nextStartIndex), |
| 296 | apiResponse = res, |
| 297 | collectedMatches = collectedMatches) |
| 298 | }) |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 299 | |
| 300 | #' Fetch all results of a KorAP query. |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 301 | #' |
| 302 | #' @examples |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 303 | #' \donttest{ |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 304 | #' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll() |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 305 | #' q@collectedMatches |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 306 | #' } |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 307 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 308 | #' @aliases fetchAll |
| 309 | #' @rdname KorAPQuery-class |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 310 | #' @export |
Marc Kupietz | f6f7131 | 2019-09-23 18:35:27 +0200 | [diff] [blame] | 311 | setMethod("fetchAll", "KorAPQuery", function(kqo, verbose = kqo@korapConnection@verbose) { |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 312 | return(fetchNext(kqo, offset = 0, maxFetch = NA, verbose = verbose)) |
| 313 | }) |
| 314 | |
| 315 | #' Fetches the remaining results of a KorAP query. |
| 316 | #' |
| 317 | #' @examples |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 318 | #' \donttest{ |
| 319 | #' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchRest() |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 320 | #' q@collectedMatches |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 321 | #' } |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 322 | #' |
| 323 | #' @aliases fetchRest |
| 324 | #' @rdname KorAPQuery-class |
| 325 | #' @export |
Marc Kupietz | f6f7131 | 2019-09-23 18:35:27 +0200 | [diff] [blame] | 326 | setMethod("fetchRest", "KorAPQuery", function(kqo, verbose = kqo@korapConnection@verbose) { |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 327 | return(fetchNext(kqo, maxFetch = NA, verbose = verbose)) |
| 328 | }) |
| 329 | |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 330 | #' Query relative frequency of search term(s) |
| 331 | #' |
| 332 | #' \bold{\code{frequencyQuery}} combines \code{\link{corpusQuery}}, \code{\link{corpusStats}} and |
| 333 | #' \code{\link{ci}} to compute a table with the relative frequencies and |
| 334 | #' confidence intervals of one ore multiple search terms across one or multiple |
| 335 | #' virtual corpora. |
| 336 | #' |
| 337 | #' @aliases frequencyQuery |
| 338 | #' @rdname KorAPQuery-class |
| 339 | #' @examples |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 340 | #' \donttest{ |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 341 | #' new("KorAPConnection", verbose = TRUE) %>% |
| 342 | #' frequencyQuery(c("Mücke", "Schnake"), paste0("pubDate in ", 2000:2003)) |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame^] | 343 | #' } |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 344 | #' |
| 345 | #' @param kco \code{\link{KorAPConnection}} object (obtained e.g. from \code{new("KorAPConnection")} |
| 346 | #' @param query string that contains the corpus query. The query language depends on the \code{ql} parameter. Either \code{query} must be provided or \code{KorAPUrl}. |
Marc Kupietz | 0c29cea | 2019-10-09 08:44:36 +0200 | [diff] [blame] | 347 | #' @param conf.level confidence level of the returned confidence interval (passed throgh \code{\link{ci}} to \code{\link{prop.test}}). |
Marc Kupietz | 71d6e05 | 2019-11-22 18:42:10 +0100 | [diff] [blame] | 348 | #' @param as.alternatives LOGICAL that specifies if the query terms should be treated as alternatives. If \code{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] | 349 | #' @export |
| 350 | setMethod("frequencyQuery", "KorAPConnection", |
Marc Kupietz | 71d6e05 | 2019-11-22 18:42:10 +0100 | [diff] [blame] | 351 | function(kco, query, vc = "", conf.level = 0.95, as.alternatives = FALSE, ...) { |
| 352 | (if (as.alternatives) { |
| 353 | corpusQuery(kco, query, vc, metadataOnly = TRUE, as.df = TRUE, ...) %>% |
| 354 | group_by(vc) %>% |
| 355 | mutate(total = sum(totalResults)) |
| 356 | } else { |
| 357 | corpusQuery(kco, query, vc, metadataOnly = TRUE, as.df = TRUE, ...) %>% |
| 358 | mutate(total = corpusStats(kco, vc=vc, as.df=TRUE)$tokens) |
| 359 | } ) %>% |
Marc Kupietz | 0c29cea | 2019-10-09 08:44:36 +0200 | [diff] [blame] | 360 | ci(conf.level = conf.level) |
Marc Kupietz | 3f57528 | 2019-10-04 14:46:04 +0200 | [diff] [blame] | 361 | }) |
| 362 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 363 | #´ format() |
| 364 | #' @rdname KorAPQuery-class |
| 365 | #' @param x KorAPQuery object |
| 366 | #' @param ... further arguments passed to or from other methods |
| 367 | #' @export |
| 368 | format.KorAPQuery <- function(x, ...) { |
| 369 | cat("<KorAPQuery>\n") |
| 370 | q <- x |
| 371 | aurl = parse_url(q@request) |
| 372 | cat(" Query: ", aurl$query$q, "\n") |
| 373 | if (!is.null(aurl$query$vc) && aurl$query$vc != "") { |
| 374 | cat("Virtual corpus: ", aurl$query$vc, "\n") |
| 375 | } |
| 376 | if (!is.null(q@collectedMatches)) { |
| 377 | cat("==============================================================================================================", "\n") |
| 378 | print(summary(q@collectedMatches)) |
| 379 | cat("==============================================================================================================", "\n") |
| 380 | } |
| 381 | cat(" Total results: ", q@totalResults, "\n") |
| 382 | cat(" Fetched results: ", q@nextStartIndex, "\n") |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 385 | #' show() |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 386 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 387 | #' @rdname KorAPQuery-class |
| 388 | #' @param object KorAPQuery object |
Marc Kupietz | 62da2b5 | 2019-09-12 17:43:34 +0200 | [diff] [blame] | 389 | #' @export |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 390 | setMethod("show", "KorAPQuery", function(object) { |
| 391 | format(object) |
| 392 | }) |