blob: 914558bb84c101af9edd494fbb15d26d1a0a636f [file] [log] [blame]
Marc Kupietzdbd431a2021-08-29 12:17:45 +02001test_that("collocationScoreQuery works", {
Marc Kupietz83d0af32022-02-24 12:49:28 +01002 skip_if_offline()
Marc Kupietz617266d2025-02-27 10:43:07 +01003 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = TRUE)
Marc Kupietz7de5f322025-06-04 17:17:22 +02004 df <- collocationScoreQuery(kco, "Ameisenplage", "heimgesucht", leftContextSize = 0, rightContextSize = 1)
Marc Kupietzdbd431a2021-08-29 12:17:45 +02005 expect_gt(df$logDice, 1)
6 expect_equal(df$ll, ll(df$O1, df$O2, df$O, df$N, df$E, df$w))
7 expect_equal(df$pmi, pmi(df$O1, df$O2, df$O, df$N, df$E, df$w))
8 expect_equal(df$mi2, mi2(df$O1, df$O2, df$O, df$N, df$E, df$w))
9 expect_equal(df$mi3, mi3(df$O1, df$O2, df$O, df$N, df$E, df$w))
10 expect_equal(df$logDice, logDice(df$O1, df$O2, df$O, df$N, df$E, df$w))
11})
12
Marc Kupietz581a29b2021-09-04 20:51:04 +020013
14test_that("collocationAnalysis works and warns about missing token", {
Marc Kupietz83d0af32022-02-24 12:49:28 +010015 skip_if_offline()
Marc Kupietz617266d2025-02-27 10:43:07 +010016 kco <- KorAPConnection(
Marc Kupietz7de5f322025-06-04 17:17:22 +020017 accessToken = NULL,
18 verbose = TRUE
19 )
20 expect_warning(
21 df <-
22 collocationAnalysis(
23 kco,
24 "focus([tt/p=ADJA] {Newstickeritis})",
25 leftContextSize = 1,
26 rightContextSize = 0,
27 ),
28 "access token"
29 )
Marc Kupietzdbd431a2021-08-29 12:17:45 +020030 expect_gt(df$O, df$E)
Marc Kupietzf9129592025-01-26 19:17:54 +010031 expect_gt(df$logDice, -1)
Marc Kupietzdbd431a2021-08-29 12:17:45 +020032})
33
34test_that("collocationAnalysis on unaccounted strings does not error out", {
Marc Kupietz83d0af32022-02-24 12:49:28 +010035 skip_if_offline()
Marc Kupietz617266d2025-02-27 10:43:07 +010036 kco <- KorAPConnection(accessToken = NULL, verbose = TRUE)
Marc Kupietz581a29b2021-09-04 20:51:04 +020037 expect_warning(
Marc Kupietz7de5f322025-06-04 17:17:22 +020038 df <- collocationAnalysis(kco, "XXXXXXXXAmeisenplage", vc = c("corpusSigle=/WDD17/", "corpusSigle=/WUD17/"), maxRecurse = 2),
Marc Kupietz581a29b2021-09-04 20:51:04 +020039 "access token"
40 )
Marc Kupietzdbd431a2021-08-29 12:17:45 +020041 testthat::expect_equal(nrow(df), 0)
42})
Marc Kupietzd6314b62021-12-22 12:49:09 +010043
Marc Kupietz7de5f322025-06-04 17:17:22 +020044# test_that("removeWithinSpanWorks", {
Marc Kupietz76dee312025-04-06 16:24:47 +020045# expect_equal(
46# removeWithinSpan("contains(<base/s=s>, (machen []{0,1} aufmerksam | aufmerksam []{0,1} machen))", "base/s=s"),
47# "(machen []{0,1} aufmerksam | aufmerksam []{0,1} machen)")
Marc Kupietz7de5f322025-06-04 17:17:22 +020048# })
Marc Kupietzdbdbb1f2025-02-19 10:33:06 +010049
50
51test_that("mergeDuplicateCollocatesWorksAsExpected", {
Marc Kupietz5057f502025-04-06 16:55:57 +020052 ldf <- tibble::tibble(
Marc Kupietzdbdbb1f2025-02-19 10:33:06 +010053 node = c("focus(in [tt/p=NN] {[tt/l=nehmen]})"),
54 collocate = c("Anspruch"),
55 label = c(""),
56 vc = c(""),
57 query = c("Anspruch focus(in [tt/p=NN] {[tt/l=nehmen]})"),
58 webUIRequestUrl = c(
59 "https://korap.ids-mannheim.de/?q=Anspruch%20focus%28in%20%5btt%2fp%3dNN%5d%20%7b%5btt%2fl%3dnehmen%5d%7d%29&ql=poliqarp"
60 ),
61 w = c(1),
62 leftContextSize = c(1),
63 rightContextSize = c(0),
64 N = c(23578528381.5),
65 O = c(0.5),
66 O1 = c(1168410.5),
67 O2 = c(1296870.5),
68 E = c(64.2651265093014),
69 pmi = c(11.9173498777957),
70 mi2 = c(29.8406639214616),
71 mi3 = c(47.7639779651274),
72 logDice = c(11.6899933757298),
73 ll = c(3717716.74208791)
74 )
Marc Kupietz5057f502025-04-06 16:55:57 +020075 rdf <- tibble::tibble(
Marc Kupietzdbdbb1f2025-02-19 10:33:06 +010076 node = c("focus({[tt/l=nehmen] in} [tt/p=NN])"),
77 collocate = c("Anspruch"),
78 label = c(""),
79 vc = c(""),
80 query = c("focus({[tt/l=nehmen] in} [tt/p=NN]) Anspruch"),
81 webUIRequestUrl = c(
82 "https://korap.ids-mannheim.de/?q=focus%28%7b%5btt%2fl%3dnehmen%5d%20in%7d%20%5btt%2fp%3dNN%5d%29%20Anspruch&ql=poliqarp"
83 ),
84 w = c(1),
85 leftContextSize = c(0),
86 rightContextSize = c(1),
87 N = c(23578528381.5),
88 O = c(0.5),
89 O1 = c(17077.5),
90 O2 = c(1296870.5),
91 E = c(0.939299756346416),
92 pmi = c(7.99469408391783),
93 mi2 = c(15.8990457079122),
94 mi3 = c(23.8033973319065),
95 logDice = c(2.57887487309409),
96 ll = c(2181.35986032019)
97 )
98 merged <- mergeDuplicateCollocates(ldf, rdf, smoothingConstant = 0.5)
99 expect_equal(merged$O, 0.5)
100 expect_equal(merged$O1, 1185487.5)
101 expect_equal(merged$O2, 1296870.5)
102 expect_equal(merged$query, "Anspruch focus(in [tt/p=NN] {[tt/l=nehmen]}) | focus({[tt/l=nehmen] in} [tt/p=NN]) Anspruch")
103})
Marc Kupietz7de5f322025-06-04 17:17:22 +0200104
Marc Kupietz5e35d7a2025-10-17 21:21:22 +0200105test_that("add_multi_vc_comparisons adds favorite columns", {
106 sample_result <- tibble::tibble(
107 node = c("n", "n"),
108 collocate = c("c", "c"),
109 vc = c("vc1", "vc2"),
110 label = c("A", "B"),
111 N = c(100, 100),
112 O = c(10, 20),
113 O1 = c(50, 50),
114 O2 = c(30, 30),
115 E = c(5, 5),
116 w = c(2, 2),
117 leftContextSize = c(1, 1),
118 rightContextSize = c(1, 1),
119 frequency = c(10, 20),
120 logDice = c(5, 7),
121 pmi = c(2, 3)
122 )
123
124 enriched <- RKorAPClient:::add_multi_vc_comparisons(sample_result, "logDice", 0.9)
125
126 expect_true(all(c(
127 "winner_logDice",
128 "winner_logDice_value",
129 "runner_up_logDice",
130 "runner_up_logDice_value"
131 ) %in% colnames(enriched)))
132
133 expect_true(all(enriched$winner_logDice == "B"))
134 expect_true(all(enriched$runner_up_logDice == "A"))
135 expect_true(all(enriched$winner_logDice_value >= enriched$runner_up_logDice_value))
136})
137
Marc Kupietzb2862d42025-10-18 10:17:49 +0200138test_that("add_multi_vc_comparisons handles more than two labels", {
139 sample_result <- tibble::tibble(
140 node = rep("n", 3),
141 collocate = rep("c", 3),
142 vc = c("vc1", "vc2", "vc3"),
143 label = c("A", "B", "C"),
144 N = rep(100, 3),
145 O = c(10, 30, 5),
146 O1 = rep(50, 3),
147 O2 = rep(30, 3),
148 E = rep(5, 3),
149 w = rep(2, 3),
150 leftContextSize = rep(1, 3),
151 rightContextSize = rep(1, 3),
152 frequency = c(10, 30, 5),
153 logDice = c(5, 8, 4),
154 pmi = c(2, 3, 1)
155 )
156
157 enriched <- RKorAPClient:::add_multi_vc_comparisons(sample_result, "logDice", 0.9)
158 expect_equal(enriched$winner_logDice[1], "B")
159 expect_equal(enriched$winner_logDice_value[1], 8)
160 expect_equal(enriched$runner_up_logDice[1], "A")
161 expect_equal(enriched$runner_up_logDice_value[1], 5)
162 expect_equal(enriched$loser_logDice[1], "C")
163 expect_equal(enriched$loser_logDice_value[1], 4)
164 expect_equal(enriched$max_delta_logDice[1], 4)
165})
166
Marc Kupietz7de5f322025-06-04 17:17:22 +0200167# New tests for improved coverage of collocationAnalysis.R helper functions
168
169test_that("synsemanticStopwords returns German stopwords", {
170 stopwords <- synsemanticStopwords()
171 expect_true(is.character(stopwords))
172 expect_true(length(stopwords) > 50)
173 expect_true("der" %in% stopwords)
174 expect_true("die" %in% stopwords)
175 expect_true("und" %in% stopwords)
176 expect_true("mit" %in% stopwords)
177})
178
179test_that("removeWithinSpan removes span constraints correctly", {
180 # Test basic span removal
181 query1 <- "contains(<base/s=s>, (machen []{0,1} aufmerksam | aufmerksam []{0,1} machen))"
182 result1 <- RKorAPClient:::removeWithinSpan(query1, "base/s=s")
183 expect_equal(result1, "(machen []{0,1} aufmerksam | aufmerksam []{0,1} machen)")
184
185 # Test with different span
186 query2 <- "contains(<p/s=s>, (test query))"
187 result2 <- RKorAPClient:::removeWithinSpan(query2, "p/s=s")
188 expect_equal(result2, "(test query)")
189
190 # Test with empty span - should return original query
191 query3 <- "simple query"
192 result3 <- RKorAPClient:::removeWithinSpan(query3, "")
193 expect_equal(result3, query3)
194
195 # Test with non-matching span
196 query4 <- "contains(<base/s=s>, test)"
197 result4 <- RKorAPClient:::removeWithinSpan(query4, "other/span")
198 expect_equal(result4, query4)
199})
200
201test_that("matches2FreqTable handles empty matches", {
202 empty_matches <- data.frame()
203 result <- RKorAPClient:::matches2FreqTable(empty_matches, index = 0)
204
205 expect_true(is.data.frame(result))
206 expect_equal(nrow(result), 0)
207})
208
209test_that("matches2FreqTable processes single match correctly", {
210 # Create mock matches data
211 mock_matches <- data.frame(
212 tokens = I(list(list(
213 left = c("der", "große"),
214 match = "Test",
215 right = c("ist", "wichtig")
216 ))),
217 stringsAsFactors = FALSE
218 )
219
220 result <- RKorAPClient:::matches2FreqTable(
221 mock_matches,
222 index = 1,
223 leftContextSize = 2,
224 rightContextSize = 2,
225 stopwords = c("der", "ist") # Provide stopwords to avoid empty join
226 )
227
228 expect_true(is.data.frame(result))
229})
230
231test_that("snippet2FreqTable handles empty snippet", {
232 result <- RKorAPClient:::snippet2FreqTable(character(0))
233
234 expect_true(is.data.frame(result))
235 expect_equal(nrow(result), 0)
236})
237
238test_that("snippet2FreqTable processes single snippet correctly", {
239 snippet <- '<span class="context-left">der große </span><span class="match"><mark>Test</mark></span><span class="context-right"> ist wichtig</span>'
240
241 result <- RKorAPClient:::snippet2FreqTable(
242 snippet,
243 leftContextSize = 2,
244 rightContextSize = 2,
245 stopwords = c("der"), # Provide stopwords to avoid empty join
246 verbose = FALSE
247 )
248
249 expect_true(is.data.frame(result))
250})
251
252# Removed hanging findExample tests as they cause infinite wait
253# These tests make API calls that don't complete properly
254
255# Removed hanging collocatesQuery tests as they cause infinite wait
256# These tests were causing the test suite to hang and not terminate
257
258test_that("collocationAnalysis handles exactFrequencies parameter", {
259 skip_if_offline()
260 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
261
262 expect_warning(
263 result <- collocationAnalysis(
264 kco,
265 "Test",
266 exactFrequencies = TRUE,
267 searchHitsSampleLimit = 5,
268 topCollocatesLimit = 5
269 ),
270 "access token"
271 )
272 expect_true(is.data.frame(result))
273})
274
275test_that("collocationAnalysis handles withinSpan parameter", {
276 skip_if_offline()
277 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
278
279 expect_warning(
280 result <- collocationAnalysis(
281 kco,
282 "Test",
283 withinSpan = "base/s=s",
284 exactFrequencies = TRUE,
285 searchHitsSampleLimit = 5,
286 topCollocatesLimit = 5
287 ),
288 "access token"
289 )
290 expect_true(is.data.frame(result))
291})
292
293test_that("collocationAnalysis handles expand parameter", {
294 skip_if_offline()
295 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
296
297 expect_warning(
298 result <- collocationAnalysis(
299 kco,
300 c("Test", "der"),
301 expand = TRUE,
302 searchHitsSampleLimit = 2,
303 topCollocatesLimit = 2
304 ),
305 "access token"
306 )
307 expect_true(is.data.frame(result))
308})
309
Marc Kupietze34a8be2025-10-17 20:13:42 +0200310test_that("collocationAnalysis honors named vc labels", {
311 skip_if_offline()
312 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
313
314 named_vc <- c(
315 Western = "textType=/.*Western.*/ & pubDate in 2012",
316 Erotic = "textType=/.*(Erotik|Gay).*/ & pubDate in 2012",
317 Historic = "textType=/.*Historisch.*/ & pubDate in 2012"
318 )
319
320 expect_warning(
321 result <- collocationAnalysis(
322 kco,
323 "[tt/l=treffen]",
324 vc = named_vc,
325 searchHitsSampleLimit = 2,
326 topCollocatesLimit = 2
327 ),
328 "access token"
329 )
330
331 if (nrow(result) > 0) {
332 expect_true("label" %in% colnames(result))
333 expect_setequal(unique(result$label), names(named_vc))
334 }
335})
336
Marc Kupietz7de5f322025-06-04 17:17:22 +0200337test_that("collocationAnalysis handles stopwords parameter", {
338 skip_if_offline()
339 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
340
341 expect_warning(
342 result <- collocationAnalysis(
343 kco,
344 "Test",
345 stopwords = c("der", "die", "und"),
346 searchHitsSampleLimit = 5,
347 topCollocatesLimit = 5
348 ),
349 "access token"
350 )
351 expect_true(is.data.frame(result))
352})
353
354test_that("collocationAnalysis handles lemmatizeNodeQuery parameter", {
355 skip_if_offline()
356 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
357
358 expect_warning(
359 result <- collocationAnalysis(
360 kco,
361 "laufen",
362 lemmatizeNodeQuery = TRUE,
363 searchHitsSampleLimit = 5,
364 topCollocatesLimit = 5
365 ),
366 "access token"
367 )
368 expect_true(is.data.frame(result))
369})
370
371test_that("collocationAnalysis handles addExamples parameter", {
372 skip_if_offline()
373 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
374
375 expect_warning(
376 result <- collocationAnalysis(
377 kco,
378 "Test",
379 addExamples = TRUE,
380 searchHitsSampleLimit = 3,
381 topCollocatesLimit = 3
382 ),
383 "access token"
384 )
385 expect_true(is.data.frame(result))
386 if (nrow(result) > 0) {
387 expect_true("example" %in% colnames(result))
388 }
389})
390
391test_that("collocationAnalysis handles maxRecurse parameter", {
392 skip_if_offline()
393 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
394
395 expect_warning(
396 result <- collocationAnalysis(
397 kco,
398 "Test",
399 maxRecurse = 1,
400 searchHitsSampleLimit = 2,
401 topCollocatesLimit = 2
402 ),
403 "access token"
404 )
405 expect_true(is.data.frame(result))
406})