blob: e6705e345eedd08b32dfe6e003af5c105da04e8c [file] [log] [blame]
Marc Kupietze95108e2019-09-18 13:23:58 +02001% Generated by roxygen2: do not edit by hand
2% Please edit documentation in R/KorAPQuery.R
3\docType{class}
4\name{KorAPQuery-class}
5\alias{KorAPQuery-class}
6\alias{KorAPQuery}
7\alias{initialize,KorAPQuery-method}
Marc Kupietzdbd431a2021-08-29 12:17:45 +02008\alias{corpusQuery,KorAPConnection-method}
9\alias{corpusQuery}
Marc Kupietze95108e2019-09-18 13:23:58 +020010\alias{fetchNext,KorAPQuery-method}
11\alias{fetchNext}
12\alias{fetchAll,KorAPQuery-method}
13\alias{fetchAll}
14\alias{fetchRest,KorAPQuery-method}
15\alias{fetchRest}
Marc Kupietz38a9d682024-12-06 16:17:09 +010016\alias{buildWebUIRequestUrlFromString}
Marc Kupietzdbd431a2021-08-29 12:17:45 +020017\alias{buildWebUIRequestUrl}
Marc Kupietze95108e2019-09-18 13:23:58 +020018\alias{format.KorAPQuery}
19\alias{show,KorAPQuery-method}
20\title{Class KorAPQuery}
21\usage{
Marc Kupietz76685f52019-11-25 17:46:06 +010022\S4method{initialize}{KorAPQuery}(
23 .Object,
24 korapConnection = NULL,
25 request = NULL,
26 vc = "",
27 totalResults = 0,
28 nextStartIndex = 0,
29 fields = c("corpusSigle", "textSigle", "pubDate", "pubPlace", "availability",
Marc Kupietz2078bde2023-08-27 16:46:15 +020030 "textClass", "snippet", "tokens"),
Marc Kupietz76685f52019-11-25 17:46:06 +010031 requestUrl = "",
32 webUIRequestUrl = "",
33 apiResponse = NULL,
34 hasMoreMatches = FALSE,
35 collectedMatches = NULL
36)
Marc Kupietze95108e2019-09-18 13:23:58 +020037
Marc Kupietzdbd431a2021-08-29 12:17:45 +020038\S4method{corpusQuery}{KorAPConnection}(
39 kco,
40 query = if (missing(KorAPUrl))
41 stop("At least one of the parameters query and KorAPUrl must be specified.", call. =
Marc Kupietzf9129592025-01-26 19:17:54 +010042 FALSE) else httr2::url_parse(KorAPUrl)$query$q,
43 vc = if (missing(KorAPUrl)) "" else httr2::url_parse(KorAPUrl)$query$cq,
Marc Kupietzdbd431a2021-08-29 12:17:45 +020044 KorAPUrl,
45 metadataOnly = TRUE,
Marc Kupietzf9129592025-01-26 19:17:54 +010046 ql = if (missing(KorAPUrl)) "poliqarp" else httr2::url_parse(KorAPUrl)$query$ql,
Marc Kupietzdbd431a2021-08-29 12:17:45 +020047 fields = c("corpusSigle", "textSigle", "pubDate", "pubPlace", "availability",
Marc Kupietz2078bde2023-08-27 16:46:15 +020048 "textClass", "snippet", "tokens"),
Marc Kupietzdbd431a2021-08-29 12:17:45 +020049 accessRewriteFatal = TRUE,
50 verbose = kco@verbose,
51 expand = length(vc) != length(query),
Marc Kupietzd9b2fd72023-04-17 19:08:50 +020052 as.df = FALSE,
53 context = NULL
Marc Kupietzdbd431a2021-08-29 12:17:45 +020054)
55
Marc Kupietz76685f52019-11-25 17:46:06 +010056\S4method{fetchNext}{KorAPQuery}(
57 kqo,
58 offset = kqo@nextStartIndex,
59 maxFetch = maxResultsPerPage,
Marc Kupietzdbd431a2021-08-29 12:17:45 +020060 verbose = kqo@korapConnection@verbose,
61 randomizePageOrder = FALSE
Marc Kupietz76685f52019-11-25 17:46:06 +010062)
Marc Kupietze95108e2019-09-18 13:23:58 +020063
Marc Kupietzdbd431a2021-08-29 12:17:45 +020064\S4method{fetchAll}{KorAPQuery}(kqo, verbose = kqo@korapConnection@verbose, ...)
Marc Kupietze95108e2019-09-18 13:23:58 +020065
Marc Kupietzdbd431a2021-08-29 12:17:45 +020066\S4method{fetchRest}{KorAPQuery}(kqo, verbose = kqo@korapConnection@verbose, ...)
Marc Kupietze95108e2019-09-18 13:23:58 +020067
Marc Kupietz38a9d682024-12-06 16:17:09 +010068buildWebUIRequestUrlFromString(KorAPUrl, query, vc = "", ql = "poliqarp")
69
Marc Kupietzdbd431a2021-08-29 12:17:45 +020070buildWebUIRequestUrl(
71 kco,
72 query = if (missing(KorAPUrl))
73 stop("At least one of the parameters query and KorAPUrl must be specified.", call. =
Marc Kupietzf9129592025-01-26 19:17:54 +010074 FALSE) else httr2::url_parse(KorAPUrl)$query$q,
75 vc = if (missing(KorAPUrl)) "" else httr2::url_parse(KorAPUrl)$query$cq,
Marc Kupietzdbd431a2021-08-29 12:17:45 +020076 KorAPUrl,
Marc Kupietzf9129592025-01-26 19:17:54 +010077 ql = if (missing(KorAPUrl)) "poliqarp" else httr2::url_parse(KorAPUrl)$query$ql
Marc Kupietzdbd431a2021-08-29 12:17:45 +020078)
79
Marc Kupietze95108e2019-09-18 13:23:58 +020080\method{format}{KorAPQuery}(x, ...)
81
82\S4method{show}{KorAPQuery}(object)
83}
84\arguments{
85\item{.Object}{}
86
Marc Kupietzb8972182019-09-20 21:33:46 +020087\item{korapConnection}{KorAPConnection object}
88
Marc Kupietze95108e2019-09-18 13:23:58 +020089\item{request}{query part of the request URL}
90
Marc Kupietze2038322021-03-04 18:24:02 +010091\item{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 Kupietze95108e2019-09-18 13:23:58 +020092
93\item{totalResults}{number of hits the query has yielded}
94
95\item{nextStartIndex}{at what index to start the next fetch of query results}
96
Marc Kupietzdbd431a2021-08-29 12:17:45 +020097\item{fields}{(meta)data fields that will be fetched for every match.}
Marc Kupietze95108e2019-09-18 13:23:58 +020098
99\item{requestUrl}{complete URL of the API request}
100
101\item{webUIRequestUrl}{URL of a web frontend request corresponding to the API request}
102
103\item{apiResponse}{data-frame representation of the JSON response of the API request}
104
Marc Kupietz7776dec2019-09-27 16:59:02 +0200105\item{hasMoreMatches}{logical that signals if more query results can be fetched}
Marc Kupietze95108e2019-09-18 13:23:58 +0200106
107\item{collectedMatches}{matches already fetched from the KorAP-API-server}
108
Marc Kupietz617266d2025-02-27 10:43:07 +0100109\item{kco}{\code{\link[=KorAPConnection]{KorAPConnection()}} object (obtained e.g. from \code{KorAPConnection()}}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200110
111\item{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}.}
112
113\item{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.}
114
Marc Kupietz132f0052023-04-16 14:23:05 +0200115\item{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.
116If you want your corpus queries to return not only metadata, but also KWICS, you need to authorize
117your RKorAPClient application as explained in the
118\href{https://github.com/KorAP/RKorAPClient#authorization}{authorization section}
119of the RKorAPClient Readme on GitHub and set the \code{metadataOnly} parameter to
120\code{FALSE}.}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200121
122\item{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.}
123
124\item{accessRewriteFatal}{abort if query or given vc had to be rewritten due to insufficient rights (not yet implemented).}
125
126\item{verbose}{print progress information if true}
127
Marc Kupietzad8d2ed2025-04-05 15:37:38 +0200128\item{expand}{logical that decides if \code{query} and \code{vc} parameters are expanded to all of their combinations. Defaults to \code{TRUE}, iff \code{query} and \code{vc} have different lengths}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200129
130\item{as.df}{return result as data frame instead of as S4 object?}
131
Marc Kupietzd9b2fd72023-04-17 19:08:50 +0200132\item{context}{string that specifies the size of the left and the right context returned in \code{snippet}
133(provided that \code{metadataOnly} is set to \code{false} and that the necessary access right are met).
134The format of the context size specifcation (e.g. \verb{3-token,3-token}) is described in the \href{https://github.com/KorAP/Kustvakt/wiki/Service:-Search-GET}{Service: Search GET documentation of the Kustvakt Wiki}.
135If the parameter is not set, the default context size secification of the KorAP server instance will be used.
136Note that you cannot overrule the maximum context size set in the KorAP server instance,
137as this is typically legally motivated.}
138
Marc Kupietz67edcb52021-09-20 21:54:24 +0200139\item{kqo}{object obtained from \code{\link[=corpusQuery]{corpusQuery()}}}
Marc Kupietze95108e2019-09-18 13:23:58 +0200140
141\item{offset}{start offset for query results to fetch}
142
143\item{maxFetch}{maximum number of query results to fetch}
144
Marc Kupietz67edcb52021-09-20 21:54:24 +0200145\item{randomizePageOrder}{fetch result pages in pseudo random order if true. Use \code{\link[=set.seed]{set.seed()}} to set seed for reproducible results.}
Marc Kupietze95108e2019-09-18 13:23:58 +0200146
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200147\item{...}{further arguments passed to or from other methods}
Marc Kupietze95108e2019-09-18 13:23:58 +0200148
Marc Kupietz3f575282019-10-04 14:46:04 +0200149\item{x}{KorAPQuery object}
150
Marc Kupietze95108e2019-09-18 13:23:58 +0200151\item{object}{KorAPQuery object}
152}
153\value{
Marc Kupietzad8d2ed2025-04-05 15:37:38 +0200154Depending on the \code{as.df} parameter, a tibble or a \code{\link[=KorAPQuery]{KorAPQuery()}} object that, among other information, contains the total number of results in \verb{@totalResults}. The resulting object can be used to fetch all query results (with \code{\link[=fetchAll]{fetchAll()}}) or the next page of results (with \code{\link[=fetchNext]{fetchNext()}}).
Marc Kupietz67edcb52021-09-20 21:54:24 +0200155A corresponding URL to be used within a web browser is contained in \verb{@webUIRequestUrl}
156Please make sure to check \verb{$collection$rewrites} to see if any unforeseen access rewrites of the query's virtual corpus had to be performed.
Marc Kupietze2038322021-03-04 18:24:02 +0100157
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200158The \code{kqo} input object with updated slots \code{collectedMatches}, \code{apiResponse}, \code{nextStartIndex}, \code{hasMoreMatches}
Marc Kupietze95108e2019-09-18 13:23:58 +0200159}
160\description{
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100161This class provides methods to perform different kinds of queries on the KorAP API server.
Marc Kupietz67edcb52021-09-20 21:54:24 +0200162\code{KorAPQuery} objects, which are typically created by the \code{\link[=corpusQuery]{corpusQuery()}} method,
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100163represent the current state of a query to a KorAP server.
Marc Kupietz3f575282019-10-04 14:46:04 +0200164
Marc Kupietz67edcb52021-09-20 21:54:24 +0200165\strong{\code{corpusQuery}} performs a corpus query via a connection to a KorAP-API-server
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200166
Marc Kupietz67edcb52021-09-20 21:54:24 +0200167\strong{\code{fetchNext}} fetches the next bunch of results of a KorAP query.
Marc Kupietz3f575282019-10-04 14:46:04 +0200168
Marc Kupietz67edcb52021-09-20 21:54:24 +0200169\strong{\code{fetchAll}} fetches all results of a KorAP query.
Marc Kupietze95108e2019-09-18 13:23:58 +0200170}
171\examples{
Marc Kupietz6ae76052021-09-21 10:34:00 +0200172\dontrun{
173
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200174# Fetch metadata of every query hit for "Ameisenplage" and show a summary
Marc Kupietz617266d2025-02-27 10:43:07 +0100175KorAPConnection() \%>\% corpusQuery("Ameisenplage") \%>\% fetchAll()
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200176}
177
Marc Kupietz6ae76052021-09-21 10:34:00 +0200178\dontrun{
179
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200180# Use the copy of a KorAP-web-frontend URL for an API query of "Ameise" in a virtual corpus
181# and show the number of query hits (but don't fetch them).
182
Marc Kupietz617266d2025-02-27 10:43:07 +0100183KorAPConnection(verbose = TRUE) \%>\%
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200184 corpusQuery(KorAPUrl =
185 "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp")
Marc Kupietz6ae76052021-09-21 10:34:00 +0200186}
187
188\dontrun{
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200189
190# Plot the time/frequency curve of "Ameisenplage"
Marc Kupietz617266d2025-02-27 10:43:07 +0100191KorAPConnection(verbose=TRUE) \%>\%
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200192 { . ->> kco } \%>\%
193 corpusQuery("Ameisenplage") \%>\%
194 fetchAll() \%>\%
195 slot("collectedMatches") \%>\%
196 mutate(year = lubridate::year(pubDate)) \%>\%
197 dplyr::select(year) \%>\%
198 group_by(year) \%>\%
199 summarise(Count = dplyr::n()) \%>\%
200 mutate(Freq = mapply(function(f, y)
201 f / corpusStats(kco, paste("pubDate in", y))@tokens, Count, year)) \%>\%
202 dplyr::select(-Count) \%>\%
203 complete(year = min(year):max(year), fill = list(Freq = 0)) \%>\%
204 plot(type = "l")
205}
Marc Kupietz6ae76052021-09-21 10:34:00 +0200206\dontrun{
207
Marc Kupietz617266d2025-02-27 10:43:07 +0100208q <- KorAPConnection() \%>\% corpusQuery("Ameisenplage") \%>\% fetchNext()
Marc Kupietze95108e2019-09-18 13:23:58 +0200209q@collectedMatches
Marc Kupietz657d8e72020-02-25 18:31:50 +0100210}
Marc Kupietze95108e2019-09-18 13:23:58 +0200211
Marc Kupietz6ae76052021-09-21 10:34:00 +0200212\dontrun{
213
Marc Kupietz617266d2025-02-27 10:43:07 +0100214q <- KorAPConnection() \%>\% corpusQuery("Ameisenplage") \%>\% fetchAll()
Marc Kupietze95108e2019-09-18 13:23:58 +0200215q@collectedMatches
Marc Kupietz05b22772020-02-18 21:58:42 +0100216}
Marc Kupietze95108e2019-09-18 13:23:58 +0200217
Marc Kupietz6ae76052021-09-21 10:34:00 +0200218\dontrun{
219
Marc Kupietz617266d2025-02-27 10:43:07 +0100220q <- KorAPConnection() \%>\% corpusQuery("Ameisenplage") \%>\% fetchRest()
Marc Kupietz05b22772020-02-18 21:58:42 +0100221q@collectedMatches
222}
223
Marc Kupietze95108e2019-09-18 13:23:58 +0200224}
225\references{
226\url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200227
228\url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026}
229}
230\seealso{
Marc Kupietz67edcb52021-09-20 21:54:24 +0200231\code{\link[=KorAPConnection]{KorAPConnection()}}, \code{\link[=fetchNext]{fetchNext()}}, \code{\link[=fetchRest]{fetchRest()}}, \code{\link[=fetchAll]{fetchAll()}}, \code{\link[=corpusStats]{corpusStats()}}
Marc Kupietze95108e2019-09-18 13:23:58 +0200232}