blob: 13f0337ac03f51a5b127cefcb39d08438b8b59d0 [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 Kupietzdbd431a2021-08-29 12:17:45 +020018\alias{buildWebUIRequestUrl}
Marc Kupietze95108e2019-09-18 13:23:58 +020019\alias{format.KorAPQuery}
20\alias{show,KorAPQuery-method}
21\title{Class KorAPQuery}
22\usage{
Marc Kupietz76685f52019-11-25 17:46:06 +010023\S4method{initialize}{KorAPQuery}(
24 .Object,
25 korapConnection = NULL,
26 request = NULL,
27 vc = "",
28 totalResults = 0,
29 nextStartIndex = 0,
30 fields = c("corpusSigle", "textSigle", "pubDate", "pubPlace", "availability",
31 "textClass", "snippet"),
32 requestUrl = "",
33 webUIRequestUrl = "",
34 apiResponse = NULL,
35 hasMoreMatches = FALSE,
36 collectedMatches = NULL
37)
Marc Kupietze95108e2019-09-18 13:23:58 +020038
Marc Kupietzdbd431a2021-08-29 12:17:45 +020039\S4method{corpusQuery}{KorAPConnection}(
40 kco,
41 query = if (missing(KorAPUrl))
42 stop("At least one of the parameters query and KorAPUrl must be specified.", call. =
43 FALSE) else httr::parse_url(KorAPUrl)$query$q,
44 vc = if (missing(KorAPUrl)) "" else httr::parse_url(KorAPUrl)$query$cq,
45 KorAPUrl,
46 metadataOnly = TRUE,
47 ql = if (missing(KorAPUrl)) "poliqarp" else httr::parse_url(KorAPUrl)$query$ql,
48 fields = c("corpusSigle", "textSigle", "pubDate", "pubPlace", "availability",
49 "textClass", "snippet"),
50 accessRewriteFatal = TRUE,
51 verbose = kco@verbose,
52 expand = length(vc) != length(query),
53 as.df = FALSE
54)
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 Kupietz76685f52019-11-25 17:46:06 +010068\S4method{frequencyQuery}{KorAPConnection}(
69 kco,
70 query,
71 vc = "",
72 conf.level = 0.95,
73 as.alternatives = FALSE,
74 ...
75)
Marc Kupietz3f575282019-10-04 14:46:04 +020076
Marc Kupietzdbd431a2021-08-29 12:17:45 +020077buildWebUIRequestUrl(
78 kco,
79 query = if (missing(KorAPUrl))
80 stop("At least one of the parameters query and KorAPUrl must be specified.", call. =
81 FALSE) else httr::parse_url(KorAPUrl)$query$q,
82 vc = if (missing(KorAPUrl)) "" else httr::parse_url(KorAPUrl)$query$cq,
83 KorAPUrl,
84 metadataOnly = TRUE,
85 ql = if (missing(KorAPUrl)) "poliqarp" else httr::parse_url(KorAPUrl)$query$ql,
86 fields = c("corpusSigle", "textSigle", "pubDate", "pubPlace", "availability",
87 "textClass", "snippet"),
88 accessRewriteFatal = TRUE
89)
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
126\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, unless the connection is authorized (currently not possible).}
127
128\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.}
129
130\item{accessRewriteFatal}{abort if query or given vc had to be rewritten due to insufficient rights (not yet implemented).}
131
132\item{verbose}{print progress information if true}
133
134\item{expand}{logical that decides if \code{query} and \code{vc} parameters are expanded to all of their combinations}
135
136\item{as.df}{return result as data frame instead of as S4 object?}
137
Marc Kupietz67edcb52021-09-20 21:54:24 +0200138\item{kqo}{object obtained from \code{\link[=corpusQuery]{corpusQuery()}}}
Marc Kupietze95108e2019-09-18 13:23:58 +0200139
140\item{offset}{start offset for query results to fetch}
141
142\item{maxFetch}{maximum number of query results to fetch}
143
Marc Kupietz67edcb52021-09-20 21:54:24 +0200144\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 +0200145
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200146\item{...}{further arguments passed to or from other methods}
Marc Kupietze95108e2019-09-18 13:23:58 +0200147
Marc Kupietz67edcb52021-09-20 21:54:24 +0200148\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 +0200149
Marc Kupietz71d6e052019-11-22 18:42:10 +0100150\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.}
151
Marc Kupietz3f575282019-10-04 14:46:04 +0200152\item{x}{KorAPQuery object}
153
Marc Kupietze95108e2019-09-18 13:23:58 +0200154\item{object}{KorAPQuery object}
155}
156\value{
Marc Kupietz67edcb52021-09-20 21:54:24 +0200157Depending 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()}}).
158A corresponding URL to be used within a web browser is contained in \verb{@webUIRequestUrl}
159Please 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 +0100160
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200161The \code{kqo} input object with updated slots \code{collectedMatches}, \code{apiResponse}, \code{nextStartIndex}, \code{hasMoreMatches}
Marc Kupietze95108e2019-09-18 13:23:58 +0200162}
163\description{
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100164This class provides methods to perform different kinds of queries on the KorAP API server.
Marc Kupietz67edcb52021-09-20 21:54:24 +0200165\code{KorAPQuery} objects, which are typically created by the \code{\link[=corpusQuery]{corpusQuery()}} method,
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100166represent the current state of a query to a KorAP server.
Marc Kupietz3f575282019-10-04 14:46:04 +0200167
Marc Kupietz67edcb52021-09-20 21:54:24 +0200168\strong{\code{corpusQuery}} performs a corpus query via a connection to a KorAP-API-server
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200169
Marc Kupietz67edcb52021-09-20 21:54:24 +0200170\strong{\code{fetchNext}} fetches the next bunch of results of a KorAP query.
Marc Kupietz3f575282019-10-04 14:46:04 +0200171
Marc Kupietz67edcb52021-09-20 21:54:24 +0200172\strong{\code{fetchAll}} fetches all results of a KorAP query.
Marc Kupietza6e4ee62021-03-05 09:00:15 +0100173
Marc Kupietz67edcb52021-09-20 21:54:24 +0200174\strong{\code{frequencyQuery}} combines \code{\link[=corpusQuery]{corpusQuery()}}, \code{\link[=corpusStats]{corpusStats()}} and
175\code{\link[=ci]{ci()}} to compute a table with the relative frequencies and
Marc Kupietz3f575282019-10-04 14:46:04 +0200176confidence intervals of one ore multiple search terms across one or multiple
177virtual corpora.
Marc Kupietze95108e2019-09-18 13:23:58 +0200178}
179\examples{
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200180# Fetch metadata of every query hit for "Ameisenplage" and show a summary
181\donttest{
182new("KorAPConnection") \%>\% corpusQuery("Ameisenplage") \%>\% fetchAll()
183}
184
185# Use the copy of a KorAP-web-frontend URL for an API query of "Ameise" in a virtual corpus
186# and show the number of query hits (but don't fetch them).
187
188new("KorAPConnection", verbose = TRUE) \%>\%
189 corpusQuery(KorAPUrl =
190 "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp")
191
192# Plot the time/frequency curve of "Ameisenplage"
193\donttest{
194new("KorAPConnection", verbose=TRUE) \%>\%
195 { . ->> kco } \%>\%
196 corpusQuery("Ameisenplage") \%>\%
197 fetchAll() \%>\%
198 slot("collectedMatches") \%>\%
199 mutate(year = lubridate::year(pubDate)) \%>\%
200 dplyr::select(year) \%>\%
201 group_by(year) \%>\%
202 summarise(Count = dplyr::n()) \%>\%
203 mutate(Freq = mapply(function(f, y)
204 f / corpusStats(kco, paste("pubDate in", y))@tokens, Count, year)) \%>\%
205 dplyr::select(-Count) \%>\%
206 complete(year = min(year):max(year), fill = list(Freq = 0)) \%>\%
207 plot(type = "l")
208}
Marc Kupietz657d8e72020-02-25 18:31:50 +0100209\donttest{q <- new("KorAPConnection") \%>\% corpusQuery("Ameisenplage") \%>\% fetchNext()
Marc Kupietze95108e2019-09-18 13:23:58 +0200210q@collectedMatches
Marc Kupietz657d8e72020-02-25 18:31:50 +0100211}
Marc Kupietze95108e2019-09-18 13:23:58 +0200212
Marc Kupietz05b22772020-02-18 21:58:42 +0100213\donttest{
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200214q <- new("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 Kupietz05b22772020-02-18 21:58:42 +0100218\donttest{
219q <- new("KorAPConnection") \%>\% corpusQuery("Ameisenplage") \%>\% fetchRest()
220q@collectedMatches
221}
222
223\donttest{
Marc Kupietz3f575282019-10-04 14:46:04 +0200224new("KorAPConnection", verbose = TRUE) \%>\%
225 frequencyQuery(c("Mücke", "Schnake"), paste0("pubDate in ", 2000:2003))
Marc Kupietz05b22772020-02-18 21:58:42 +0100226}
Marc Kupietz3f575282019-10-04 14:46:04 +0200227
Marc Kupietze95108e2019-09-18 13:23:58 +0200228}
229\references{
230\url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200231
232\url{https://ids-pub.bsz-bw.de/frontdoor/index/index/docId/9026}
233}
234\seealso{
Marc Kupietz67edcb52021-09-20 21:54:24 +0200235\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 +0200236}