blob: 1c862cc3ce0cbb090fb402cea4dbdfe2ca326883 [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 Kupietz3f575282019-10-04 14:46:04 +020016\alias{frequencyQuery,KorAPConnection-method}
17\alias{frequencyQuery}
Marc Kupietz38a9d682024-12-06 16:17:09 +010018\alias{buildWebUIRequestUrlFromString}
Marc Kupietzdbd431a2021-08-29 12:17:45 +020019\alias{buildWebUIRequestUrl}
Marc Kupietze95108e2019-09-18 13:23:58 +020020\alias{format.KorAPQuery}
21\alias{show,KorAPQuery-method}
22\title{Class KorAPQuery}
23\usage{
Marc Kupietz76685f52019-11-25 17:46:06 +010024\S4method{initialize}{KorAPQuery}(
25 .Object,
26 korapConnection = NULL,
27 request = NULL,
28 vc = "",
29 totalResults = 0,
30 nextStartIndex = 0,
31 fields = c("corpusSigle", "textSigle", "pubDate", "pubPlace", "availability",
Marc Kupietz2078bde2023-08-27 16:46:15 +020032 "textClass", "snippet", "tokens"),
Marc Kupietz76685f52019-11-25 17:46:06 +010033 requestUrl = "",
34 webUIRequestUrl = "",
35 apiResponse = NULL,
36 hasMoreMatches = FALSE,
37 collectedMatches = NULL
38)
Marc Kupietze95108e2019-09-18 13:23:58 +020039
Marc Kupietzdbd431a2021-08-29 12:17:45 +020040\S4method{corpusQuery}{KorAPConnection}(
41 kco,
42 query = if (missing(KorAPUrl))
43 stop("At least one of the parameters query and KorAPUrl must be specified.", call. =
44 FALSE) else httr::parse_url(KorAPUrl)$query$q,
45 vc = if (missing(KorAPUrl)) "" else httr::parse_url(KorAPUrl)$query$cq,
46 KorAPUrl,
47 metadataOnly = TRUE,
48 ql = if (missing(KorAPUrl)) "poliqarp" else httr::parse_url(KorAPUrl)$query$ql,
49 fields = c("corpusSigle", "textSigle", "pubDate", "pubPlace", "availability",
Marc Kupietz2078bde2023-08-27 16:46:15 +020050 "textClass", "snippet", "tokens"),
Marc Kupietzdbd431a2021-08-29 12:17:45 +020051 accessRewriteFatal = TRUE,
52 verbose = kco@verbose,
53 expand = length(vc) != length(query),
Marc Kupietzd9b2fd72023-04-17 19:08:50 +020054 as.df = FALSE,
55 context = NULL
Marc Kupietzdbd431a2021-08-29 12:17:45 +020056)
57
Marc Kupietz76685f52019-11-25 17:46:06 +010058\S4method{fetchNext}{KorAPQuery}(
59 kqo,
60 offset = kqo@nextStartIndex,
61 maxFetch = maxResultsPerPage,
Marc Kupietzdbd431a2021-08-29 12:17:45 +020062 verbose = kqo@korapConnection@verbose,
63 randomizePageOrder = FALSE
Marc Kupietz76685f52019-11-25 17:46:06 +010064)
Marc Kupietze95108e2019-09-18 13:23:58 +020065
Marc Kupietzdbd431a2021-08-29 12:17:45 +020066\S4method{fetchAll}{KorAPQuery}(kqo, verbose = kqo@korapConnection@verbose, ...)
Marc Kupietze95108e2019-09-18 13:23:58 +020067
Marc Kupietzdbd431a2021-08-29 12:17:45 +020068\S4method{fetchRest}{KorAPQuery}(kqo, verbose = kqo@korapConnection@verbose, ...)
Marc Kupietze95108e2019-09-18 13:23:58 +020069
Marc Kupietz76685f52019-11-25 17:46:06 +010070\S4method{frequencyQuery}{KorAPConnection}(
71 kco,
72 query,
73 vc = "",
74 conf.level = 0.95,
75 as.alternatives = FALSE,
76 ...
77)
Marc Kupietz3f575282019-10-04 14:46:04 +020078
Marc Kupietz38a9d682024-12-06 16:17:09 +010079buildWebUIRequestUrlFromString(KorAPUrl, query, vc = "", ql = "poliqarp")
80
Marc Kupietzdbd431a2021-08-29 12:17:45 +020081buildWebUIRequestUrl(
82 kco,
83 query = if (missing(KorAPUrl))
84 stop("At least one of the parameters query and KorAPUrl must be specified.", call. =
85 FALSE) else httr::parse_url(KorAPUrl)$query$q,
86 vc = if (missing(KorAPUrl)) "" else httr::parse_url(KorAPUrl)$query$cq,
87 KorAPUrl,
Marc Kupietz38a9d682024-12-06 16:17:09 +010088 ql = if (missing(KorAPUrl)) "poliqarp" else httr::parse_url(KorAPUrl)$query$ql
Marc Kupietzdbd431a2021-08-29 12:17:45 +020089)
90
Marc Kupietze95108e2019-09-18 13:23:58 +020091\method{format}{KorAPQuery}(x, ...)
92
93\S4method{show}{KorAPQuery}(object)
94}
95\arguments{
96\item{.Object}{}
97
Marc Kupietzb8972182019-09-20 21:33:46 +020098\item{korapConnection}{KorAPConnection object}
99
Marc Kupietze95108e2019-09-18 13:23:58 +0200100\item{request}{query part of the request URL}
101
Marc Kupietze2038322021-03-04 18:24:02 +0100102\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 +0200103
104\item{totalResults}{number of hits the query has yielded}
105
106\item{nextStartIndex}{at what index to start the next fetch of query results}
107
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200108\item{fields}{(meta)data fields that will be fetched for every match.}
Marc Kupietze95108e2019-09-18 13:23:58 +0200109
110\item{requestUrl}{complete URL of the API request}
111
112\item{webUIRequestUrl}{URL of a web frontend request corresponding to the API request}
113
114\item{apiResponse}{data-frame representation of the JSON response of the API request}
115
Marc Kupietz7776dec2019-09-27 16:59:02 +0200116\item{hasMoreMatches}{logical that signals if more query results can be fetched}
Marc Kupietze95108e2019-09-18 13:23:58 +0200117
118\item{collectedMatches}{matches already fetched from the KorAP-API-server}
119
Marc Kupietz67edcb52021-09-20 21:54:24 +0200120\item{kco}{\code{\link[=KorAPConnection]{KorAPConnection()}} object (obtained e.g. from \code{new("KorAPConnection")}}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200121
122\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}.}
123
124\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.}
125
Marc Kupietz132f0052023-04-16 14:23:05 +0200126\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.
127If you want your corpus queries to return not only metadata, but also KWICS, you need to authorize
128your RKorAPClient application as explained in the
129\href{https://github.com/KorAP/RKorAPClient#authorization}{authorization section}
130of the RKorAPClient Readme on GitHub and set the \code{metadataOnly} parameter to
131\code{FALSE}.}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200132
133\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.}
134
135\item{accessRewriteFatal}{abort if query or given vc had to be rewritten due to insufficient rights (not yet implemented).}
136
137\item{verbose}{print progress information if true}
138
139\item{expand}{logical that decides if \code{query} and \code{vc} parameters are expanded to all of their combinations}
140
141\item{as.df}{return result as data frame instead of as S4 object?}
142
Marc Kupietzd9b2fd72023-04-17 19:08:50 +0200143\item{context}{string that specifies the size of the left and the right context returned in \code{snippet}
144(provided that \code{metadataOnly} is set to \code{false} and that the necessary access right are met).
145The 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}.
146If the parameter is not set, the default context size secification of the KorAP server instance will be used.
147Note that you cannot overrule the maximum context size set in the KorAP server instance,
148as this is typically legally motivated.}
149
Marc Kupietz67edcb52021-09-20 21:54:24 +0200150\item{kqo}{object obtained from \code{\link[=corpusQuery]{corpusQuery()}}}
Marc Kupietze95108e2019-09-18 13:23:58 +0200151
152\item{offset}{start offset for query results to fetch}
153
154\item{maxFetch}{maximum number of query results to fetch}
155
Marc Kupietz67edcb52021-09-20 21:54:24 +0200156\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 +0200157
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200158\item{...}{further arguments passed to or from other methods}
Marc Kupietze95108e2019-09-18 13:23:58 +0200159
Marc Kupietz67edcb52021-09-20 21:54:24 +0200160\item{conf.level}{confidence level of the returned confidence interval (passed through \code{\link[=ci]{ci()}} to \code{\link[=prop.test]{prop.test()}}).}
Marc Kupietz0c29cea2019-10-09 08:44:36 +0200161
Marc Kupietz71d6e052019-11-22 18:42:10 +0100162\item{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.}
163
Marc Kupietz3f575282019-10-04 14:46:04 +0200164\item{x}{KorAPQuery object}
165
Marc Kupietze95108e2019-09-18 13:23:58 +0200166\item{object}{KorAPQuery object}
167}
168\value{
Marc Kupietz67edcb52021-09-20 21:54:24 +0200169Depending on the \code{as.df} parameter, a table 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()}}).
170A corresponding URL to be used within a web browser is contained in \verb{@webUIRequestUrl}
171Please 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 +0100172
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200173The \code{kqo} input object with updated slots \code{collectedMatches}, \code{apiResponse}, \code{nextStartIndex}, \code{hasMoreMatches}
Marc Kupietze95108e2019-09-18 13:23:58 +0200174}
175\description{
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100176This class provides methods to perform different kinds of queries on the KorAP API server.
Marc Kupietz67edcb52021-09-20 21:54:24 +0200177\code{KorAPQuery} objects, which are typically created by the \code{\link[=corpusQuery]{corpusQuery()}} method,
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100178represent the current state of a query to a KorAP server.
Marc Kupietz3f575282019-10-04 14:46:04 +0200179
Marc Kupietz67edcb52021-09-20 21:54:24 +0200180\strong{\code{corpusQuery}} performs a corpus query via a connection to a KorAP-API-server
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200181
Marc Kupietz67edcb52021-09-20 21:54:24 +0200182\strong{\code{fetchNext}} fetches the next bunch of results of a KorAP query.
Marc Kupietz3f575282019-10-04 14:46:04 +0200183
Marc Kupietz67edcb52021-09-20 21:54:24 +0200184\strong{\code{fetchAll}} fetches all results of a KorAP query.
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100185
Marc Kupietz67edcb52021-09-20 21:54:24 +0200186\strong{\code{frequencyQuery}} combines \code{\link[=corpusQuery]{corpusQuery()}}, \code{\link[=corpusStats]{corpusStats()}} and
187\code{\link[=ci]{ci()}} to compute a table with the relative frequencies and
Marc Kupietz3f575282019-10-04 14:46:04 +0200188confidence intervals of one ore multiple search terms across one or multiple
189virtual corpora.
Marc Kupietze95108e2019-09-18 13:23:58 +0200190}
191\examples{
Marc Kupietz6ae76052021-09-21 10:34:00 +0200192\dontrun{
193
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200194# Fetch metadata of every query hit for "Ameisenplage" and show a summary
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200195new("KorAPConnection") \%>\% corpusQuery("Ameisenplage") \%>\% fetchAll()
196}
197
Marc Kupietz6ae76052021-09-21 10:34:00 +0200198\dontrun{
199
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200200# Use the copy of a KorAP-web-frontend URL for an API query of "Ameise" in a virtual corpus
201# and show the number of query hits (but don't fetch them).
202
203new("KorAPConnection", verbose = TRUE) \%>\%
204 corpusQuery(KorAPUrl =
205 "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp")
Marc Kupietz6ae76052021-09-21 10:34:00 +0200206}
207
208\dontrun{
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200209
210# Plot the time/frequency curve of "Ameisenplage"
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200211new("KorAPConnection", verbose=TRUE) \%>\%
212 { . ->> kco } \%>\%
213 corpusQuery("Ameisenplage") \%>\%
214 fetchAll() \%>\%
215 slot("collectedMatches") \%>\%
216 mutate(year = lubridate::year(pubDate)) \%>\%
217 dplyr::select(year) \%>\%
218 group_by(year) \%>\%
219 summarise(Count = dplyr::n()) \%>\%
220 mutate(Freq = mapply(function(f, y)
221 f / corpusStats(kco, paste("pubDate in", y))@tokens, Count, year)) \%>\%
222 dplyr::select(-Count) \%>\%
223 complete(year = min(year):max(year), fill = list(Freq = 0)) \%>\%
224 plot(type = "l")
225}
Marc Kupietz6ae76052021-09-21 10:34:00 +0200226\dontrun{
227
228q <- new("KorAPConnection") \%>\% corpusQuery("Ameisenplage") \%>\% fetchNext()
Marc Kupietze95108e2019-09-18 13:23:58 +0200229q@collectedMatches
Marc Kupietz657d8e72020-02-25 18:31:50 +0100230}
Marc Kupietze95108e2019-09-18 13:23:58 +0200231
Marc Kupietz6ae76052021-09-21 10:34:00 +0200232\dontrun{
233
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200234q <- new("KorAPConnection") \%>\% corpusQuery("Ameisenplage") \%>\% fetchAll()
Marc Kupietze95108e2019-09-18 13:23:58 +0200235q@collectedMatches
Marc Kupietz05b22772020-02-18 21:58:42 +0100236}
Marc Kupietze95108e2019-09-18 13:23:58 +0200237
Marc Kupietz6ae76052021-09-21 10:34:00 +0200238\dontrun{
239
Marc Kupietz05b22772020-02-18 21:58:42 +0100240q <- new("KorAPConnection") \%>\% corpusQuery("Ameisenplage") \%>\% fetchRest()
241q@collectedMatches
242}
243
Marc Kupietz6ae76052021-09-21 10:34:00 +0200244\dontrun{
245
Marc Kupietz3f575282019-10-04 14:46:04 +0200246new("KorAPConnection", verbose = TRUE) \%>\%
247 frequencyQuery(c("Mücke", "Schnake"), paste0("pubDate in ", 2000:2003))
Marc Kupietz05b22772020-02-18 21:58:42 +0100248}
Marc Kupietz3f575282019-10-04 14:46:04 +0200249
Marc Kupietze95108e2019-09-18 13:23:58 +0200250}
251\references{
252\url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200253
254\url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026}
255}
256\seealso{
Marc Kupietz67edcb52021-09-20 21:54:24 +0200257\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 +0200258}