blob: e7875f48dccc9f28ee3a6cb639bd01f1c751a411 [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 Kupietz4cbb5472025-10-19 12:15:25 +020082\item{missingScoreQuantile}{lower quantile (evaluated per association measure) 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 below the weakest observed scores}
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}
125\examples{
Marc Kupietz6ae76052021-09-21 10:34:00 +0200126\dontrun{
127
Marc Kupietzb1dec012025-06-04 17:16:57 +0200128# Find top collocates of "Packung" inside and outside the sports domain.
129KorAPConnection(verbose = TRUE) |>
130 collocationAnalysis("Packung",
131 vc = c("textClass=sport", "textClass!=sport"),
132 leftContextSize = 1, rightContextSize = 1, topCollocatesLimit = 20
133 ) |>
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200134 dplyr::filter(logDice >= 5)
135}
136
Marc Kupietz6ae76052021-09-21 10:34:00 +0200137\dontrun{
138
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200139# Identify the most prominent light verb construction with "in ... setzen".
140# Note that, currently, the use of focus function disallows exactFrequencies.
Marc Kupietz463bb162025-03-26 10:23:33 +0100141KorAPConnection(verbose = TRUE) |>
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200142 collocationAnalysis("focus(in [tt/p=NN] {[tt/l=setzen]})",
Marc Kupietzb1dec012025-06-04 17:16:57 +0200143 leftContextSize = 1, rightContextSize = 0, exactFrequencies = FALSE, topCollocatesLimit = 20
144 )
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200145}
146
147}
148\seealso{
Marc Kupietz209b52c2026-06-21 12:14:36 +0200149Other collocation analysis functions:
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200150\code{\link{association-score-functions}},
151\code{\link{collocationScoreQuery,KorAPConnection-method}},
Marc Kupietz209b52c2026-06-21 12:14:36 +0200152\code{\link[=synsemanticStopwords]{synsemanticStopwords()}}
Marc Kupietzdbd431a2021-08-29 12:17:45 +0200153}
154\concept{collocation analysis functions}