blob: 52a2a6610ec3cff25dd063a63ea160a44d562733 [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",
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
178 enriched <- RKorAPClient:::add_multi_vc_comparisons(sample_result, "logDice", 0.9)
179 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
219 enriched <- RKorAPClient:::add_multi_vc_comparisons(base_tbl, "logDice", 0.9)
220 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
254 enriched <- RKorAPClient:::add_multi_vc_comparisons(sample_result, "logDice", 0.9)
255
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 Kupietz7de5f322025-06-04 17:17:22 +0200264# New tests for improved coverage of collocationAnalysis.R helper functions
265
266test_that("synsemanticStopwords returns German stopwords", {
267 stopwords <- synsemanticStopwords()
268 expect_true(is.character(stopwords))
269 expect_true(length(stopwords) > 50)
270 expect_true("der" %in% stopwords)
271 expect_true("die" %in% stopwords)
272 expect_true("und" %in% stopwords)
273 expect_true("mit" %in% stopwords)
274})
275
276test_that("removeWithinSpan removes span constraints correctly", {
277 # Test basic span removal
278 query1 <- "contains(<base/s=s>, (machen []{0,1} aufmerksam | aufmerksam []{0,1} machen))"
279 result1 <- RKorAPClient:::removeWithinSpan(query1, "base/s=s")
280 expect_equal(result1, "(machen []{0,1} aufmerksam | aufmerksam []{0,1} machen)")
281
282 # Test with different span
283 query2 <- "contains(<p/s=s>, (test query))"
284 result2 <- RKorAPClient:::removeWithinSpan(query2, "p/s=s")
285 expect_equal(result2, "(test query)")
286
287 # Test with empty span - should return original query
288 query3 <- "simple query"
289 result3 <- RKorAPClient:::removeWithinSpan(query3, "")
290 expect_equal(result3, query3)
291
292 # Test with non-matching span
293 query4 <- "contains(<base/s=s>, test)"
294 result4 <- RKorAPClient:::removeWithinSpan(query4, "other/span")
295 expect_equal(result4, query4)
296})
297
298test_that("matches2FreqTable handles empty matches", {
299 empty_matches <- data.frame()
300 result <- RKorAPClient:::matches2FreqTable(empty_matches, index = 0)
301
302 expect_true(is.data.frame(result))
303 expect_equal(nrow(result), 0)
304})
305
306test_that("matches2FreqTable processes single match correctly", {
307 # Create mock matches data
308 mock_matches <- data.frame(
309 tokens = I(list(list(
310 left = c("der", "große"),
311 match = "Test",
312 right = c("ist", "wichtig")
313 ))),
314 stringsAsFactors = FALSE
315 )
316
317 result <- RKorAPClient:::matches2FreqTable(
318 mock_matches,
319 index = 1,
320 leftContextSize = 2,
321 rightContextSize = 2,
322 stopwords = c("der", "ist") # Provide stopwords to avoid empty join
323 )
324
325 expect_true(is.data.frame(result))
326})
327
328test_that("snippet2FreqTable handles empty snippet", {
329 result <- RKorAPClient:::snippet2FreqTable(character(0))
330
331 expect_true(is.data.frame(result))
332 expect_equal(nrow(result), 0)
333})
334
335test_that("snippet2FreqTable processes single snippet correctly", {
336 snippet <- '<span class="context-left">der große </span><span class="match"><mark>Test</mark></span><span class="context-right"> ist wichtig</span>'
337
338 result <- RKorAPClient:::snippet2FreqTable(
339 snippet,
340 leftContextSize = 2,
341 rightContextSize = 2,
342 stopwords = c("der"), # Provide stopwords to avoid empty join
343 verbose = FALSE
344 )
345
346 expect_true(is.data.frame(result))
347})
348
349# Removed hanging findExample tests as they cause infinite wait
350# These tests make API calls that don't complete properly
351
352# Removed hanging collocatesQuery tests as they cause infinite wait
353# These tests were causing the test suite to hang and not terminate
354
355test_that("collocationAnalysis handles exactFrequencies parameter", {
356 skip_if_offline()
357 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
358
359 expect_warning(
360 result <- collocationAnalysis(
361 kco,
362 "Test",
363 exactFrequencies = TRUE,
364 searchHitsSampleLimit = 5,
365 topCollocatesLimit = 5
366 ),
367 "access token"
368 )
369 expect_true(is.data.frame(result))
370})
371
372test_that("collocationAnalysis handles withinSpan parameter", {
373 skip_if_offline()
374 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
375
376 expect_warning(
377 result <- collocationAnalysis(
378 kco,
379 "Test",
380 withinSpan = "base/s=s",
381 exactFrequencies = TRUE,
382 searchHitsSampleLimit = 5,
383 topCollocatesLimit = 5
384 ),
385 "access token"
386 )
387 expect_true(is.data.frame(result))
388})
389
390test_that("collocationAnalysis handles expand parameter", {
391 skip_if_offline()
392 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
393
394 expect_warning(
395 result <- collocationAnalysis(
396 kco,
397 c("Test", "der"),
398 expand = TRUE,
399 searchHitsSampleLimit = 2,
400 topCollocatesLimit = 2
401 ),
402 "access token"
403 )
404 expect_true(is.data.frame(result))
405})
406
Marc Kupietze34a8be2025-10-17 20:13:42 +0200407test_that("collocationAnalysis honors named vc labels", {
408 skip_if_offline()
409 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
410
411 named_vc <- c(
412 Western = "textType=/.*Western.*/ & pubDate in 2012",
413 Erotic = "textType=/.*(Erotik|Gay).*/ & pubDate in 2012",
414 Historic = "textType=/.*Historisch.*/ & pubDate in 2012"
415 )
416
417 expect_warning(
418 result <- collocationAnalysis(
419 kco,
420 "[tt/l=treffen]",
421 vc = named_vc,
422 searchHitsSampleLimit = 2,
423 topCollocatesLimit = 2
424 ),
425 "access token"
426 )
427
428 if (nrow(result) > 0) {
429 expect_true("label" %in% colnames(result))
430 expect_setequal(unique(result$label), names(named_vc))
431 }
432})
433
Marc Kupietz7de5f322025-06-04 17:17:22 +0200434test_that("collocationAnalysis handles stopwords parameter", {
435 skip_if_offline()
436 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
437
438 expect_warning(
439 result <- collocationAnalysis(
440 kco,
441 "Test",
442 stopwords = c("der", "die", "und"),
443 searchHitsSampleLimit = 5,
444 topCollocatesLimit = 5
445 ),
446 "access token"
447 )
448 expect_true(is.data.frame(result))
449})
450
451test_that("collocationAnalysis handles lemmatizeNodeQuery parameter", {
452 skip_if_offline()
453 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
454
455 expect_warning(
456 result <- collocationAnalysis(
457 kco,
458 "laufen",
459 lemmatizeNodeQuery = TRUE,
460 searchHitsSampleLimit = 5,
461 topCollocatesLimit = 5
462 ),
463 "access token"
464 )
465 expect_true(is.data.frame(result))
466})
467
468test_that("collocationAnalysis handles addExamples parameter", {
469 skip_if_offline()
470 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
471
472 expect_warning(
473 result <- collocationAnalysis(
474 kco,
475 "Test",
476 addExamples = TRUE,
477 searchHitsSampleLimit = 3,
478 topCollocatesLimit = 3
479 ),
480 "access token"
481 )
482 expect_true(is.data.frame(result))
483 if (nrow(result) > 0) {
484 expect_true("example" %in% colnames(result))
485 }
486})
487
488test_that("collocationAnalysis handles maxRecurse parameter", {
489 skip_if_offline()
490 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
491
492 expect_warning(
493 result <- collocationAnalysis(
494 kco,
495 "Test",
496 maxRecurse = 1,
497 searchHitsSampleLimit = 2,
498 topCollocatesLimit = 2
499 ),
500 "access token"
501 )
502 expect_true(is.data.frame(result))
503})