blob: bd86cdfbfdb432f50185ea712e5d89bbb835ed4a [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 Kupietz7de5f322025-06-04 17:17:22 +0200138# New tests for improved coverage of collocationAnalysis.R helper functions
139
140test_that("synsemanticStopwords returns German stopwords", {
141 stopwords <- synsemanticStopwords()
142 expect_true(is.character(stopwords))
143 expect_true(length(stopwords) > 50)
144 expect_true("der" %in% stopwords)
145 expect_true("die" %in% stopwords)
146 expect_true("und" %in% stopwords)
147 expect_true("mit" %in% stopwords)
148})
149
150test_that("removeWithinSpan removes span constraints correctly", {
151 # Test basic span removal
152 query1 <- "contains(<base/s=s>, (machen []{0,1} aufmerksam | aufmerksam []{0,1} machen))"
153 result1 <- RKorAPClient:::removeWithinSpan(query1, "base/s=s")
154 expect_equal(result1, "(machen []{0,1} aufmerksam | aufmerksam []{0,1} machen)")
155
156 # Test with different span
157 query2 <- "contains(<p/s=s>, (test query))"
158 result2 <- RKorAPClient:::removeWithinSpan(query2, "p/s=s")
159 expect_equal(result2, "(test query)")
160
161 # Test with empty span - should return original query
162 query3 <- "simple query"
163 result3 <- RKorAPClient:::removeWithinSpan(query3, "")
164 expect_equal(result3, query3)
165
166 # Test with non-matching span
167 query4 <- "contains(<base/s=s>, test)"
168 result4 <- RKorAPClient:::removeWithinSpan(query4, "other/span")
169 expect_equal(result4, query4)
170})
171
172test_that("matches2FreqTable handles empty matches", {
173 empty_matches <- data.frame()
174 result <- RKorAPClient:::matches2FreqTable(empty_matches, index = 0)
175
176 expect_true(is.data.frame(result))
177 expect_equal(nrow(result), 0)
178})
179
180test_that("matches2FreqTable processes single match correctly", {
181 # Create mock matches data
182 mock_matches <- data.frame(
183 tokens = I(list(list(
184 left = c("der", "große"),
185 match = "Test",
186 right = c("ist", "wichtig")
187 ))),
188 stringsAsFactors = FALSE
189 )
190
191 result <- RKorAPClient:::matches2FreqTable(
192 mock_matches,
193 index = 1,
194 leftContextSize = 2,
195 rightContextSize = 2,
196 stopwords = c("der", "ist") # Provide stopwords to avoid empty join
197 )
198
199 expect_true(is.data.frame(result))
200})
201
202test_that("snippet2FreqTable handles empty snippet", {
203 result <- RKorAPClient:::snippet2FreqTable(character(0))
204
205 expect_true(is.data.frame(result))
206 expect_equal(nrow(result), 0)
207})
208
209test_that("snippet2FreqTable processes single snippet correctly", {
210 snippet <- '<span class="context-left">der große </span><span class="match"><mark>Test</mark></span><span class="context-right"> ist wichtig</span>'
211
212 result <- RKorAPClient:::snippet2FreqTable(
213 snippet,
214 leftContextSize = 2,
215 rightContextSize = 2,
216 stopwords = c("der"), # Provide stopwords to avoid empty join
217 verbose = FALSE
218 )
219
220 expect_true(is.data.frame(result))
221})
222
223# Removed hanging findExample tests as they cause infinite wait
224# These tests make API calls that don't complete properly
225
226# Removed hanging collocatesQuery tests as they cause infinite wait
227# These tests were causing the test suite to hang and not terminate
228
229test_that("collocationAnalysis handles exactFrequencies parameter", {
230 skip_if_offline()
231 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
232
233 expect_warning(
234 result <- collocationAnalysis(
235 kco,
236 "Test",
237 exactFrequencies = TRUE,
238 searchHitsSampleLimit = 5,
239 topCollocatesLimit = 5
240 ),
241 "access token"
242 )
243 expect_true(is.data.frame(result))
244})
245
246test_that("collocationAnalysis handles withinSpan parameter", {
247 skip_if_offline()
248 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
249
250 expect_warning(
251 result <- collocationAnalysis(
252 kco,
253 "Test",
254 withinSpan = "base/s=s",
255 exactFrequencies = TRUE,
256 searchHitsSampleLimit = 5,
257 topCollocatesLimit = 5
258 ),
259 "access token"
260 )
261 expect_true(is.data.frame(result))
262})
263
264test_that("collocationAnalysis handles expand parameter", {
265 skip_if_offline()
266 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
267
268 expect_warning(
269 result <- collocationAnalysis(
270 kco,
271 c("Test", "der"),
272 expand = TRUE,
273 searchHitsSampleLimit = 2,
274 topCollocatesLimit = 2
275 ),
276 "access token"
277 )
278 expect_true(is.data.frame(result))
279})
280
Marc Kupietze34a8be2025-10-17 20:13:42 +0200281test_that("collocationAnalysis honors named vc labels", {
282 skip_if_offline()
283 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
284
285 named_vc <- c(
286 Western = "textType=/.*Western.*/ & pubDate in 2012",
287 Erotic = "textType=/.*(Erotik|Gay).*/ & pubDate in 2012",
288 Historic = "textType=/.*Historisch.*/ & pubDate in 2012"
289 )
290
291 expect_warning(
292 result <- collocationAnalysis(
293 kco,
294 "[tt/l=treffen]",
295 vc = named_vc,
296 searchHitsSampleLimit = 2,
297 topCollocatesLimit = 2
298 ),
299 "access token"
300 )
301
302 if (nrow(result) > 0) {
303 expect_true("label" %in% colnames(result))
304 expect_setequal(unique(result$label), names(named_vc))
305 }
306})
307
Marc Kupietz7de5f322025-06-04 17:17:22 +0200308test_that("collocationAnalysis handles stopwords parameter", {
309 skip_if_offline()
310 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
311
312 expect_warning(
313 result <- collocationAnalysis(
314 kco,
315 "Test",
316 stopwords = c("der", "die", "und"),
317 searchHitsSampleLimit = 5,
318 topCollocatesLimit = 5
319 ),
320 "access token"
321 )
322 expect_true(is.data.frame(result))
323})
324
325test_that("collocationAnalysis handles lemmatizeNodeQuery parameter", {
326 skip_if_offline()
327 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
328
329 expect_warning(
330 result <- collocationAnalysis(
331 kco,
332 "laufen",
333 lemmatizeNodeQuery = TRUE,
334 searchHitsSampleLimit = 5,
335 topCollocatesLimit = 5
336 ),
337 "access token"
338 )
339 expect_true(is.data.frame(result))
340})
341
342test_that("collocationAnalysis handles addExamples parameter", {
343 skip_if_offline()
344 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
345
346 expect_warning(
347 result <- collocationAnalysis(
348 kco,
349 "Test",
350 addExamples = TRUE,
351 searchHitsSampleLimit = 3,
352 topCollocatesLimit = 3
353 ),
354 "access token"
355 )
356 expect_true(is.data.frame(result))
357 if (nrow(result) > 0) {
358 expect_true("example" %in% colnames(result))
359 }
360})
361
362test_that("collocationAnalysis handles maxRecurse parameter", {
363 skip_if_offline()
364 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, verbose = FALSE)
365
366 expect_warning(
367 result <- collocationAnalysis(
368 kco,
369 "Test",
370 maxRecurse = 1,
371 searchHitsSampleLimit = 2,
372 topCollocatesLimit = 2
373 ),
374 "access token"
375 )
376 expect_true(is.data.frame(result))
377})