Label virtual corpora by the names they were given
Passing c(before = ..., since = ...) said what the two corpora are to be
called, and only collocationAnalysis() listened. frequencyQuery() dropped
the names in the expand_grid() it builds its queries from and returned no
label at all; corpusStats() let them become row names, which the first
bind_rows() throws away; collocationScoreQuery() ignored them and derived
a label from the corpus definitions instead, so that the pair above came
out as "1990 & pubDat..." and "2010".
All three now prefer the names, falling back to queryStringToLabel()
where a vector is named only in part, as collocationAnalysis() has been
doing. A vector without names is left alone: no label column appears
where there is nothing to put in it.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Change-Id: I72c7c8d5f14b649df853cae94a4d90c0be19d1f4
diff --git a/tests/testthat/test-vc-labels.R b/tests/testthat/test-vc-labels.R
new file mode 100644
index 0000000..a614ec6
--- /dev/null
+++ b/tests/testthat/test-vc-labels.R
@@ -0,0 +1,63 @@
+test_that("vcLabels prefers the names a vector was given", {
+ vcLabels <- RKorAPClient:::vcLabels
+
+ expect_equal(
+ vcLabels(c(before = "pubDate until 2009", since = "pubDate since 2010")),
+ c("before", "since")
+ )
+ # nothing to prefer, so nothing is invented
+ expect_null(vcLabels(c("pubDate until 2009", "pubDate since 2010")))
+ # a partly named vector is filled in from the definitions
+ expect_equal(
+ vcLabels(c(before = "pubDate until 2009", "pubDate since 2010")),
+ c("before", "since 2010")
+ )
+})
+
+test_that("vcLabelsOrGuess always gives a label", {
+ expect_equal(
+ RKorAPClient:::vcLabelsOrGuess(c("pubDate until 2009", "pubDate since 2010")),
+ RKorAPClient:::queryStringToLabel(c("pubDate until 2009", "pubDate since 2010"))
+ )
+})
+
+test_that("the query functions label virtual corpora by their names", {
+ skip_if_offline()
+ kco <- KorAPConnection(accessToken = NULL, verbose = FALSE)
+ vcs <- c(before = "pubDate until 2009", since = "pubDate since 2010")
+
+ expect_equal(frequencyQuery(kco, "Ameisenplage", vcs)$label, c("before", "since"))
+ expect_equal(corpusStats(kco, vc = vcs, as.df = TRUE)$label, c("before", "since"))
+ expect_equal(
+ collocationScoreQuery(kco, "Grund", "triftiger", vc = vcs)$label,
+ c("before", "since")
+ )
+})
+
+test_that("an unnamed vector leaves the result as it was", {
+ skip_if_offline()
+ kco <- KorAPConnection(accessToken = NULL, verbose = FALSE)
+ vcs <- c("pubDate until 2009", "pubDate since 2010")
+
+ # no column appears where there is nothing to put in it
+ expect_false("label" %in% names(frequencyQuery(kco, "Ameisenplage", vcs)))
+ expect_false("label" %in% names(corpusStats(kco, vc = vcs, as.df = TRUE)))
+ # collocationScoreQuery has always labelled, and keeps deriving one
+ expect_equal(
+ collocationScoreQuery(kco, "Grund", "triftiger", vc = vcs)$label,
+ RKorAPClient:::queryStringToLabel(vcs)
+ )
+})
+
+test_that("corpusStats does not hide the labels in row names", {
+ skip_if_offline()
+ kco <- KorAPConnection(accessToken = NULL, verbose = FALSE)
+ stats <- corpusStats(
+ kco,
+ vc = c(before = "pubDate until 2009", since = "pubDate since 2010"),
+ as.df = TRUE
+ )
+ # row names are lost by the first bind_rows(), a column is not
+ expect_equal(rownames(stats), c("1", "2"))
+ expect_true("label" %in% names(stats))
+})