blob: 02c3f037ba4dfeb766204404af331f0e366e9cc3 [file] [log] [blame]
Marc Kupietzdbd431a2021-08-29 12:17:45 +02001% Generated by roxygen2: do not edit by hand
2% Please edit documentation in R/collocationAnalysis.R
3\name{collocationAnalysis,KorAPConnection-method}
4\alias{collocationAnalysis,KorAPConnection-method}
5\alias{collocationAnalysis}
6\title{Collocation analysis}
7\usage{
8\S4method{collocationAnalysis}{KorAPConnection}(
9 kco,
10 node,
11 vc = "",
12 lemmatizeNodeQuery = FALSE,
13 minOccur = 5,
14 leftContextSize = 5,
15 rightContextSize = 5,
16 topCollocatesLimit = 200,
17 searchHitsSampleLimit = 20000,
18 ignoreCollocateCase = FALSE,
19 withinSpan = ifelse(exactFrequencies, "base/s=s", ""),
20 exactFrequencies = TRUE,
Marc Kupietz6505ccf2021-11-27 17:46:25 +010021 stopwords = append(RKorAPClient::synsemanticStopwords(), node),
Marc Kupietzdbd431a2021-08-29 12:17:45 +020022 seed = 7,
23 expand = length(vc) != length(node),
Marc Kupietz5a336b62021-11-27 17:51:35 +010024 maxRecurse = 0,
Marc Kupietzdadfd912021-12-22 12:48:20 +010025 addExamples = FALSE,
Marc Kupietz419f21f2021-12-07 10:27:30 +010026 thresholdScore = "logDice",
27 threshold = 2,
Marc Kupietz5a336b62021-11-27 17:51:35 +010028 localStopwords = c(),
Marc Kupietz47d0d2b2021-12-19 16:38:52 +010029 collocateFilterRegex = "^[:alnum:]+-?[:alnum:]*$",
Marc Kupietzde679ea2025-10-19 13:14:51 +020030 queryMissingScores = FALSE,
Marc Kupietz130a2a22025-10-18 16:09:23 +020031 missingScoreQuantile = 0.05,
32 vcLabel = NA_character_,
Marc Kupietzdb2fabd2026-04-27 15:01:37 +020033 cacheAs = NULL,
Marc Kupietzdbd431a2021-08-29 12:17:45 +020034 ...
35)
36}
37\arguments{
Marc Kupietz617266d2025-02-27 10:43:07 +010038\item{kco}{\code{\link[=KorAPConnection]{KorAPConnection()}} object (obtained e.g. from \code{KorAPConnection()}}
Marc Kupietzdbd431a2021-08-29 12:17:45 +020039
Marc Kupietz209b52c2026-06-21 12:14:36 +020040\item{node}{target word or query as a single character string}
Marc Kupietzdbd431a2021-08-29 12:17:45 +020041
Marc Kupietz209b52c2026-06-21 12:14:36 +020042\item{vc}{character vector describing the virtual corpus or corpora in which the query should be performed. An empty string (default) means the whole corpus, as far as it is license-wise accessible.}
Marc Kupietzdbd431a2021-08-29 12:17:45 +020043
Marc Kupietz67edcb52021-09-20 21:54:24 +020044\item{lemmatizeNodeQuery}{if TRUE, node query will be lemmatized, i.e. \verb{x -> [tt/l=x]}}
Marc Kupietzdbd431a2021-08-29 12:17:45 +020045
46\item{minOccur}{minimum absolute number of observed co-occurrences to consider a collocate candidate}
47
48\item{leftContextSize}{size of the left context window}
49
50\item{rightContextSize}{size of the right context window}
51
52\item{topCollocatesLimit}{limit analysis to the n most frequent collocates in the search hits sample}
53
54\item{searchHitsSampleLimit}{limit the size of the search hits sample}
55
56\item{ignoreCollocateCase}{logical, set to TRUE if collocate case should be ignored}
57
Marc Kupietz6bd9cad2024-12-18 15:57:26 +010058\item{withinSpan}{KorAP span specification (see \url{https://korap.ids-mannheim.de/doc/ql/poliqarp-plus?embedded=true#spans}) for collocations to be searched within. Defaults to \code{base/s=s}.}
Marc Kupietzdbd431a2021-08-29 12:17:45 +020059
60\item{exactFrequencies}{if FALSE, extrapolate observed co-occurrence frequencies from frequencies in search hits sample, otherwise retrieve exact co-occurrence frequencies}
61
62\item{stopwords}{vector of stopwords not to be considered as collocates}
63
64\item{seed}{seed for random page collecting order}
65
66\item{expand}{if TRUE, \code{node} and \code{vc} parameters are expanded to all of their combinations}
67
Marc Kupietz7d400e02021-12-19 16:39:36 +010068\item{maxRecurse}{apply collocation analysis recursively \code{maxRecurse} times}
69
70\item{addExamples}{If TRUE, examples for instances of collocations will be added in a column \code{example}. This makes a difference in particular if \code{node} is given as a lemma query.}
71
Marc Kupietzdb2fabd2026-04-27 15:01:37 +020072\item{thresholdScore}{association score function (see \code{\link{association-score-functions}}) to use for computing the threshold that is applied for recursive collocation analysis calls (only applied when \code{maxRecurse > 0})}
Marc Kupietz7d400e02021-12-19 16:39:36 +010073
Marc Kupietzdb2fabd2026-04-27 15:01:37 +020074\item{threshold}{minimum value of \code{thresholdScore} function call to apply collocation analysis recursively (only applied when \code{maxRecurse > 0})}
Marc Kupietz7d400e02021-12-19 16:39:36 +010075
76\item{localStopwords}{vector of stopwords that will not be considered as collocates in the current function call, but that will not be passed to recursive calls}
77
Marc Kupietz47d0d2b2021-12-19 16:38:52 +010078\item{collocateFilterRegex}{allow only collocates matching the regular expression}
79
Marc Kupietzde679ea2025-10-19 13:14:51 +020080\item{queryMissingScores}{if TRUE, attempt to retrieve corpus-based association scores for vc/collocate combinations that would otherwise be imputed, by re-querying the KorAP backend without applying the collocate frequency threshold}
81
Marc Kupietz95253342026-08-31 10:18:43 +020082\item{missingScoreQuantile}{lower quantile (evaluated per association measure over the pooled result set) that anchors the adaptive floor used for imputing missing scores between virtual corpora; a robust spread is subtracted from this anchor so the imputed values stay at or below the weakest observed scores. Imputed cells are marked in the \verb{imputed*} columns; see the section on interpreting multi-VC comparisons below}
Marc Kupietz130a2a22025-10-18 16:09:23 +020083
84\item{vcLabel}{optional label override for the current virtual corpus (used internally when named VC collections are expanded)}
85
Marc Kupietzdb2fabd2026-04-27 15:01:37 +020086\item{cacheAs}{path to an RDS file for caching the result. If the file already exists, the cached result is loaded and returned immediately without contacting the server. Otherwise the analysis is run normally and the result is saved to the file before returning. Defaults to \code{NULL} (no caching).}
87
Marc Kupietz67edcb52021-09-20 21:54:24 +020088\item{...}{more arguments will be passed to \code{\link[=collocationScoreQuery]{collocationScoreQuery()}}}
Marc Kupietzdbd431a2021-08-29 12:17:45 +020089}
90\value{
Marc Kupietz130a2a22025-10-18 16:09:23 +020091A tibble where each row represents a candidate collocate for the requested node.
92Columns include (depending on the selected association measures):
93
94\itemize{
95\item \code{node}, \code{collocate}, \code{vc}, \code{label}: identifiers for the query node, collocate, virtual corpus, and optional label.
96\item Frequency and contingency information such as \code{frequency}, \code{O}, \code{O1}, \code{O2}, \code{E}, \code{leftContextSize}, \code{rightContextSize}, and \code{w}.
97\item Association measures (e.g. \code{logDice}, \code{ll}, \code{mi}, ...), one column per requested scorer.
98\item Per-labelled association scores produced by multi-VC comparisons using the pattern \code{<measure>_<label>}.
99\item Ranks per label/measure with the pattern \code{rank_<label>_<measure>} (1 is best) and the corresponding percentile ranks \code{percentile_rank_<label>_<measure>}.
100\item Pairwise contrasts for two-label comparisons, e.g. \code{delta_<measure>}, \code{delta_rank_<measure>}, and \code{delta_percentile_rank_<measure>}.
Marc Kupietz09b1c082026-05-01 14:45:47 +0200101\item Summary columns describing the strongest labels per measure (\code{winner_*}, \code{runner_up_*}, \code{loser_*}, and \code{max_delta_*}), including winner/loser \code{webUIRequestUrl} columns. In multi-VC comparisons, missing per-label concordance URLs are derived from another available row URL for the same \code{node}/\code{collocate} by replacing the \code{cq} parameter with the target label's virtual corpus. Unsuffixed \code{winner_webUIRequestUrl} and \code{loser_webUIRequestUrl} columns are populated only when the score-based URL choices agree.
Marc Kupietzd7bb5cb2026-08-31 10:18:02 +0200102\item \code{imputed_<label>}, \code{n_imputed}, and \code{imputed}: flags marking rows whose scores were not observed for some label but imputed (see \code{missingScoreQuantile}). Filter with \code{dplyr::filter(!imputed)} to keep only collocates attested in every compared virtual corpus.
Marc Kupietz130a2a22025-10-18 16:09:23 +0200103\item Optional helper columns such as \code{query}, \code{example}, or \code{url} when example retrieval is requested.
104}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200105}
106\description{
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200107Performs a collocation analysis for the given node (or query)
108in the given virtual corpus.
109}
110\details{
111The collocation analysis is currently implemented on the client side, as some of the
112functionality is not yet provided by the KorAP backend. Mainly for this reason
113it is very slow (several minutes, up to hours), but on the other hand very flexible.
114You can, for example, perform the analysis in arbitrary virtual corpora, use complex node queries,
115and look for expression-internal collocates using the focus function (see examples and demo).
116
117To increase speed at the cost of accuracy and possible false negatives,
118you can decrease searchHitsSampleLimit and/or topCollocatesLimit and/or set exactFrequencies to FALSE.
119
Marc Kupietze7f0d682025-02-19 10:50:59 +0100120Note that some outdated non-DeReKo back-ends might not yet support returning tokenized matches (warning issued).
121In this case, the client library will fall back to client-side tokenization which might be slightly less accurate.
122This might lead to false negatives and to frequencies that differ from corresponding ones acquired via the web
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200123user interface.
124}
Marc Kupietz95253342026-08-31 10:18:43 +0200125\section{Interpreting multi-VC comparisons}{
126
127
Marc Kupietzba15cff2026-08-31 10:19:56 +0200128\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}}
129
130The comparison columns produced when \code{vc} holds more than one virtual corpus
131are experimental: their names and semantics may still change in a future
132release without a deprecation cycle. Code that has to keep working across
133versions should select the columns it needs explicitly.
134
135They are an exploration aid, not a significance test. When reading them, keep
136three properties in mind.
Marc Kupietz95253342026-08-31 10:18:43 +0200137
138\strong{Imputed scores describe presence/absence, not contrast.} A collocate
139that passes the \code{minOccur} and \code{topCollocatesLimit} thresholds in one virtual
140corpus but not in another has no observed score for the latter. Such cells are
141imputed from a floor derived from the pooled result set (see
142\code{missingScoreQuantile}), so the corresponding \verb{delta_*} and \verb{max_delta_*}
143values measure the distance to that floor rather than an attested difference.
144The \code{imputed}, \code{n_imputed} and \verb{imputed_<label>} columns mark these rows;
145\code{dplyr::filter(!imputed)} restricts the result to collocates attested
146everywhere, and \code{queryMissingScores = TRUE} replaces most imputed cells with
147scores actually retrieved from the backend.
148
149\strong{Imputed values are relative to one analysis.} The floor is computed
150from the scores present in the result at hand. Analysing a node on its own and
151analysing it together with other nodes therefore yield different imputed
152values, and deltas involving imputed cells are not comparable across separate
153calls. Deltas between observed scores are unaffected.
154
155\strong{Winners carry no uncertainty.} Unlike \code{\link[=ci]{ci()}}, which attaches
156confidence intervals to relative frequencies, the \verb{winner_*} / \verb{loser_*}
157columns simply order point estimates. A collocate wins by a hair on six
158occurrences exactly as decisively as one that wins by a wide margin on
159thousands. Consult the observed frequencies (\code{O}, \code{O1}, \code{O2}) and the
160\code{webUIRequestUrl} concordance links before drawing conclusions from a
161small difference.
162
163Note also that \verb{rank_<label>_<measure>} and
164\verb{percentile_rank_<label>_<measure>} are computed within each label, over that
165label's own candidate set. Candidate sets usually differ in size between
166virtual corpora, so rank-based deltas compare positions in populations of
167different sizes.
168}
169
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200170\examples{
Marc Kupietz6ae76052021-09-21 10:34:00 +0200171\dontrun{
172
Marc Kupietzb1dec012025-06-04 17:16:57 +0200173# Find top collocates of "Packung" inside and outside the sports domain.
174KorAPConnection(verbose = TRUE) |>
175 collocationAnalysis("Packung",
176 vc = c("textClass=sport", "textClass!=sport"),
177 leftContextSize = 1, rightContextSize = 1, topCollocatesLimit = 20
178 ) |>
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200179 dplyr::filter(logDice >= 5)
180}
181
Marc Kupietz6ae76052021-09-21 10:34:00 +0200182\dontrun{
183
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200184# Identify the most prominent light verb construction with "in ... setzen".
185# Note that, currently, the use of focus function disallows exactFrequencies.
Marc Kupietz463bb162025-03-26 10:23:33 +0100186KorAPConnection(verbose = TRUE) |>
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200187 collocationAnalysis("focus(in [tt/p=NN] {[tt/l=setzen]})",
Marc Kupietzb1dec012025-06-04 17:16:57 +0200188 leftContextSize = 1, rightContextSize = 0, exactFrequencies = FALSE, topCollocatesLimit = 20
189 )
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200190}
191
192}
193\seealso{
Marc Kupietz209b52c2026-06-21 12:14:36 +0200194Other collocation analysis functions:
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200195\code{\link{association-score-functions}},
196\code{\link{collocationScoreQuery,KorAPConnection-method}},
Marc Kupietz209b52c2026-06-21 12:14:36 +0200197\code{\link[=synsemanticStopwords]{synsemanticStopwords()}}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200198}
199\concept{collocation analysis functions}