blob: 0705a2b877a8f2bb586d2f912071fc0bf17aa3ce [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
Marc Kupietz9894a372025-10-18 14:51:29 +0200124 enriched <- RKorAPClient:::add_multi_vc_comparisons(sample_result, "logDice")
Marc Kupietz5e35d7a2025-10-17 21:21:22 +0200125
126 expect_true(all(c(
127 "winner_logDice",
128 "winner_logDice_value",
129 "runner_up_logDice",
Marc Kupietz28a29842025-10-18 12:25:09 +0200130 "runner_up_logDice_value",
131 "max_delta_logDice",
132 "winner_rank_logDice",
133 "winner_rank_logDice_value",
134 "runner_up_rank_logDice",
135 "runner_up_rank_logDice_value",
136 "loser_rank_logDice",
137 "loser_rank_logDice_value",
138 "max_delta_rank_logDice",
139 "winner_rank_pmi",
140 "winner_rank_pmi_value",
141 "runner_up_rank_pmi",
142 "runner_up_rank_pmi_value",
143 "loser_rank_pmi",
144 "loser_rank_pmi_value",
145 "max_delta_rank_pmi",
146 "rank_A_logDice",
147 "rank_B_logDice",
148 "rank_A_pmi",
149 "rank_B_pmi",
150 "delta_rank_logDice",
151 "delta_rank_pmi"
Marc Kupietz5e35d7a2025-10-17 21:21:22 +0200152 ) %in% colnames(enriched)))
153
154 expect_true(all(enriched$winner_logDice == "B"))
155 expect_true(all(enriched$runner_up_logDice == "A"))
156 expect_true(all(enriched$winner_logDice_value >= enriched$runner_up_logDice_value))
157})
158
Marc Kupietzb2862d42025-10-18 10:17:49 +0200159test_that("add_multi_vc_comparisons handles more than two labels", {
160 sample_result <- tibble::tibble(
161 node = rep("n", 3),
162 collocate = rep("c", 3),
163 vc = c("vc1", "vc2", "vc3"),
164 label = c("A", "B", "C"),
165 N = rep(100, 3),
166 O = c(10, 30, 5),
167 O1 = rep(50, 3),
168 O2 = rep(30, 3),
169 E = rep(5, 3),
170 w = rep(2, 3),
171 leftContextSize = rep(1, 3),
172 rightContextSize = rep(1, 3),
173 frequency = c(10, 30, 5),
174 logDice = c(5, 8, 4),
175 pmi = c(2, 3, 1)
176 )
177
Marc Kupietz9894a372025-10-18 14:51:29 +0200178 enriched <- RKorAPClient:::add_multi_vc_comparisons(sample_result, "logDice")
Marc Kupietzb2862d42025-10-18 10:17:49 +0200179 expect_equal(enriched$winner_logDice[1], "B")
180 expect_equal(enriched$winner_logDice_value[1], 8)
181 expect_equal(enriched$runner_up_logDice[1], "A")
182 expect_equal(enriched$runner_up_logDice_value[1], 5)
183 expect_equal(enriched$loser_logDice[1], "C")
184 expect_equal(enriched$loser_logDice_value[1], 4)
185 expect_equal(enriched$max_delta_logDice[1], 4)
186})
187
Marc Kupietz28a29842025-10-18 12:25:09 +0200188test_that("add_multi_vc_comparisons computes rank deltas", {
189 base_tbl <- tidyr::expand_grid(
190 label = c("A", "B", "C"),
191 collocate = c("c1", "c2", "c3")
192 ) |>
193 dplyr::mutate(
194 node = "n",
195 vc = paste0("vc", label),
196 N = 100,
197 O = 10,
198 O1 = 50,
199 O2 = 40,
200 E = 5,
201 w = 2,
202 leftContextSize = 1,
203 rightContextSize = 1,
204 frequency = 10,
205 logDice = dplyr::case_when(
206 label == "A" & collocate == "c1" ~ 9,
207 label == "A" & collocate == "c2" ~ 6,
208 label == "A" & collocate == "c3" ~ 3,
209 label == "B" & collocate == "c1" ~ 7,
210 label == "B" & collocate == "c2" ~ 9,
211 label == "B" & collocate == "c3" ~ 5,
212 label == "C" & collocate == "c1" ~ 4,
213 label == "C" & collocate == "c2" ~ 6,
214 label == "C" & collocate == "c3" ~ 8,
215 TRUE ~ 0
216 )
217 )
218
Marc Kupietz9894a372025-10-18 14:51:29 +0200219 enriched <- RKorAPClient:::add_multi_vc_comparisons(base_tbl, "logDice")
Marc Kupietz28a29842025-10-18 12:25:09 +0200220 target_row <- enriched |>
221 dplyr::filter(collocate == "c1") |>
222 dplyr::slice_head(n = 1)
223
224 expect_equal(target_row$rank_A_logDice, 1)
225 expect_equal(target_row$rank_B_logDice, 2)
226 expect_equal(target_row$rank_C_logDice, 3)
227 expect_equal(target_row$winner_rank_logDice, "A")
228 expect_equal(target_row$winner_rank_logDice_value, 1)
229 expect_equal(target_row$runner_up_rank_logDice, "B")
230 expect_equal(target_row$runner_up_rank_logDice_value, 2)
231 expect_equal(target_row$loser_rank_logDice, "C")
232 expect_equal(target_row$loser_rank_logDice_value, 3)
233 expect_equal(target_row$max_delta_rank_logDice, 2)
234})
235
236test_that("add_multi_vc_comparisons imputes missing ranks for max delta", {
237 sample_result <- tibble::tibble(
238 node = c("n", "n"),
239 collocate = c("c", "c"),
240 vc = c("vc1", "vc2"),
241 label = c("A", "B"),
242 N = c(100, 100),
243 O = c(10, 10),
244 O1 = c(50, 50),
245 O2 = c(30, 30),
246 E = c(5, 5),
247 w = c(2, 2),
248 leftContextSize = c(1, 1),
249 rightContextSize = c(1, 1),
250 frequency = c(10, 10),
251 logDice = c(5, NA)
252 )
253
Marc Kupietz9894a372025-10-18 14:51:29 +0200254 enriched <- RKorAPClient:::add_multi_vc_comparisons(sample_result, "logDice")
Marc Kupietz28a29842025-10-18 12:25:09 +0200255
256 expect_equal(enriched$rank_A_logDice[1], 1)
257 expect_true(is.na(enriched$rank_B_logDice[1]))
258 expect_equal(enriched$winner_rank_logDice[1], "A")
259 expect_equal(enriched$loser_rank_logDice[1], "B")
260 expect_equal(enriched$loser_rank_logDice_value[1], 2)
261 expect_equal(enriched$max_delta_rank_logDice[1], 1)
262})
263
Marc Kupietz9894a372025-10-18 14:51:29 +0200264test_that("adaptive missing score imputation respects measure-specific scales", {
265 sample_result <- tibble::tibble(
266 node = c("n", "n", "n"),
267 collocate = c("c", "c", "c"),
268 vc = c("vc1", "vc2", "vc3"),
269 label = c("A", "B", "C"),
270 N = c(100, 100, 100),
271 O = c(12, 9, 7),
272 O1 = c(60, 40, 30),
273 O2 = c(33, 22, 18),
274 E = c(6, 6, 6),
275 w = c(2, 2, 2),
276 leftContextSize = c(1, 1, 1),
277 rightContextSize = c(1, 1, 1),
278 frequency = c(15, 11, 9),
279 logDice = c(-0.31, NA, -0.12),
280 pmi = c(-1.65, NA, -0.48),
281 ll = c(12.4, NA, 7.9)
282 )
283
284 enriched <- RKorAPClient:::add_multi_vc_comparisons(
285 sample_result,
286 "logDice",
287 missingScoreQuantile = 0.05
288 )
289
290 row_a <- dplyr::filter(enriched, label == "A") |> dplyr::slice_head(n = 1)
291
292 expect_false(is.na(row_a$logDice_B))
293 expect_false(is.na(row_a$pmi_B))
294 expect_false(is.na(row_a$ll_B))
295
296 expect_lt(row_a$logDice_B, min(sample_result$logDice, na.rm = TRUE))
297 expect_lt(row_a$pmi_B, min(sample_result$pmi, na.rm = TRUE))
298 expect_lte(row_a$ll_B, min(sample_result$ll, na.rm = TRUE))
299
300 expect_gt(row_a$max_delta_logDice, 0)
301 expect_gt(row_a$winner_logDice_value - row_a$loser_logDice_value, 0)
302})
303
Marc Kupietz7de5f322025-06-04 17:17:22 +0200304# New tests for improved coverage of collocationAnalysis.R helper functions
305
306test_that("synsemanticStopwords returns German stopwords", {
307 stopwords <- synsemanticStopwords()
308 expect_true(is.character(stopwords))
309 expect_true(length(stopwords) > 50)
310 expect_true("der" %in% stopwords)
311 expect_true("die" %in% stopwords)
312 expect_true("und" %in% stopwords)
313 expect_true("mit" %in% stopwords)
314})
315
316test_that("removeWithinSpan removes span constraints correctly", {
317 # Test basic span removal
318 query1 <- "contains(<base/s=s>, (machen []{0,1} aufmerksam | aufmerksam []{0,1} machen))"
319 result1 <- RKorAPClient:::removeWithinSpan(query1, "base/s=s")
320 expect_equal(result1, "(machen []{0,1} aufmerksam | aufmerksam []{0,1} machen)")
321
322 # Test with different span
323 query2 <- "contains(<p/s=s>, (test query))"
324 result2 <- RKorAPClient:::removeWithinSpan(query2, "p/s=s")
325 expect_equal(result2, "(test query)")
326
327 # Test with empty span - should return original query
328 query3 <- "simple query"
329 result3 <- RKorAPClient:::removeWithinSpan(query3, "")
330 expect_equal(result3, query3)
331
332 # Test with non-matching span
333 query4 <- "contains(<base/s=s>, test)"
334 result4 <- RKorAPClient:::removeWithinSpan(query4, "other/span")
335 expect_equal(result4, query4)
336})
337
338test_that("matches2FreqTable handles empty matches", {
339 empty_matches <- data.frame()
340 result <- RKorAPClient:::matches2FreqTable(empty_matches, index = 0)
341
342 expect_true(is.data.frame(result))
343 expect_equal(nrow(result), 0)
344})
345
346test_that("matches2FreqTable processes single match correctly", {
347 # Create mock matches data
348 mock_matches <- data.frame(
349 tokens = I(list(list(
350 left = c("der", "große"),
351 match = "Test",
352 right = c("ist", "wichtig")
353 ))),
354 stringsAsFactors = FALSE
355 )
356
357 result <- RKorAPClient:::matches2FreqTable(
358 mock_matches,
359 index = 1,
360 leftContextSize = 2,
361 rightContextSize = 2,
362 stopwords = c("der", "ist") # Provide stopwords to avoid empty join
363 )
364
365 expect_true(is.data.frame(result))
366})
367
368test_that("snippet2FreqTable handles empty snippet", {
369 result <- RKorAPClient:::snippet2FreqTable(character(0))
370
371 expect_true(is.data.frame(result))
372 expect_equal(nrow(result), 0)
373})
374
375test_that("snippet2FreqTable processes single snippet correctly", {
376 snippet <- '<span class="context-left">der große </span><span class="match"><mark>Test</mark></span><span class="context-right"> ist wichtig</span>'
377
378 result <- RKorAPClient:::snippet2FreqTable(
379 snippet,
380 leftContextSize = 2,
381 rightContextSize = 2,
382 stopwords = c("der"), # Provide stopwords to avoid empty join
383 verbose = FALSE
384 )
385
386 expect_true(is.data.frame(result))
387})
388
389# Removed hanging findExample tests as they cause infinite wait
390# These tests make API calls that don't complete properly
391
392# Removed hanging collocatesQuery tests as they cause infinite wait
393# These tests were causing the test suite to hang and not terminate
394
395test_that("collocationAnalysis handles exactFrequencies parameter", {
396 skip_if_offline()
397 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
398
399 expect_warning(
400 result <- collocationAnalysis(
401 kco,
402 "Test",
403 exactFrequencies = TRUE,
404 searchHitsSampleLimit = 5,
405 topCollocatesLimit = 5
406 ),
407 "access token"
408 )
409 expect_true(is.data.frame(result))
410})
411
412test_that("collocationAnalysis handles withinSpan parameter", {
413 skip_if_offline()
414 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
415
416 expect_warning(
417 result <- collocationAnalysis(
418 kco,
419 "Test",
420 withinSpan = "base/s=s",
421 exactFrequencies = TRUE,
422 searchHitsSampleLimit = 5,
423 topCollocatesLimit = 5
424 ),
425 "access token"
426 )
427 expect_true(is.data.frame(result))
428})
429
430test_that("collocationAnalysis handles expand parameter", {
431 skip_if_offline()
432 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
433
434 expect_warning(
435 result <- collocationAnalysis(
436 kco,
437 c("Test", "der"),
438 expand = TRUE,
439 searchHitsSampleLimit = 2,
440 topCollocatesLimit = 2
441 ),
442 "access token"
443 )
444 expect_true(is.data.frame(result))
445})
446
Marc Kupietze34a8be2025-10-17 20:13:42 +0200447test_that("collocationAnalysis honors named vc labels", {
448 skip_if_offline()
449 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
450
451 named_vc <- c(
452 Western = "textType=/.*Western.*/ & pubDate in 2012",
453 Erotic = "textType=/.*(Erotik|Gay).*/ & pubDate in 2012",
454 Historic = "textType=/.*Historisch.*/ & pubDate in 2012"
455 )
456
457 expect_warning(
458 result <- collocationAnalysis(
459 kco,
460 "[tt/l=treffen]",
461 vc = named_vc,
462 searchHitsSampleLimit = 2,
463 topCollocatesLimit = 2
464 ),
465 "access token"
466 )
467
468 if (nrow(result) > 0) {
469 expect_true("label" %in% colnames(result))
470 expect_setequal(unique(result$label), names(named_vc))
471 }
472})
473
Marc Kupietz7de5f322025-06-04 17:17:22 +0200474test_that("collocationAnalysis handles stopwords parameter", {
475 skip_if_offline()
476 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
477
478 expect_warning(
479 result <- collocationAnalysis(
480 kco,
481 "Test",
482 stopwords = c("der", "die", "und"),
483 searchHitsSampleLimit = 5,
484 topCollocatesLimit = 5
485 ),
486 "access token"
487 )
488 expect_true(is.data.frame(result))
489})
490
491test_that("collocationAnalysis handles lemmatizeNodeQuery parameter", {
492 skip_if_offline()
493 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
494
495 expect_warning(
496 result <- collocationAnalysis(
497 kco,
498 "laufen",
499 lemmatizeNodeQuery = TRUE,
500 searchHitsSampleLimit = 5,
501 topCollocatesLimit = 5
502 ),
503 "access token"
504 )
505 expect_true(is.data.frame(result))
506})
507
508test_that("collocationAnalysis handles addExamples parameter", {
509 skip_if_offline()
510 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
511
512 expect_warning(
513 result <- collocationAnalysis(
514 kco,
515 "Test",
516 addExamples = TRUE,
517 searchHitsSampleLimit = 3,
518 topCollocatesLimit = 3
519 ),
520 "access token"
521 )
522 expect_true(is.data.frame(result))
523 if (nrow(result) > 0) {
524 expect_true("example" %in% colnames(result))
525 }
526})
527
528test_that("collocationAnalysis handles maxRecurse parameter", {
529 skip_if_offline()
530 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
531
532 expect_warning(
533 result <- collocationAnalysis(
534 kco,
535 "Test",
536 maxRecurse = 1,
537 searchHitsSampleLimit = 2,
538 topCollocatesLimit = 2
539 ),
540 "access token"
541 )
542 expect_true(is.data.frame(result))
543})