blob: d0d16aef834fa4710c60141a8f4c14dc3517c5cf [file] [log] [blame]
#!/usr/bin/env Rscript
#
# Visualize frequencies of alternative query terms in relation to other variables
# specified in virtual corpus definitions.
#
library(RKorAPClient)
library(vcd)
library(tibble)
library(dplyr)
library(PTXQC)
queryStringToLabel <- function(data) {
leftCommon = lcpCount(data)
while (leftCommon > 0 && grepl("[[:alpha:]]", substring(data[1], leftCommon, leftCommon))) {
leftCommon <- leftCommon - 1
}
rightCommon = lcsCount(data)
while (rightCommon > 0 && grepl("[[:alpha:]]", substring(data[1], rightCommon, rightCommon))) {
rightCommon <- rightCommon - 1
}
substring(data, leftCommon + 1, nchar(data) - rightCommon)
}
mosaicplot <- function(query, vc, kco = new("KorAPConnection", verbose = TRUE)) {
frequencyQuery(
query = query,
vc = vc,
kco,
expand = TRUE,
as.alternatives = TRUE
) %>%
mutate(alternative = queryStringToLabel(query), condition = queryStringToLabel(vc)) %>%
{ . ->> queryResult } %>%
{ xtabs(totalResults ~ condition + alternative, .)} %>%
vcd::mosaic(shade = TRUE) # , labeling = labeling_border(rot_labels = c(45,0,0,0), just_labels = c("left", "center", "center", "right")))
df
}
queryResult <- mosaicplot(c("[marmot/m=mood:subj]", "[marmot/m=mood:ind]"), c("textDomain=Wirtschaft", "textDomain=Kultur", "textDomain=Sport"))
#mosaicplot(c("Asylbewerber", "Asylwerber"), c("pubPlaceKey=DE", "pubPlaceKey=AT"))
#mosaicplot(c("wegen dem [tt/p=NN]", "wegen des [tt/p=NN]"), c("pubPlaceKey=DE", "pubPlaceKey=AT"))
#mosaicplot(c("Samstag", "Sonnabend"), c("pubPlace=Hamburg", "pubPlace=Berlin"))
#mosaicplot(c("Tomaten", "Paradeiser"), c("pubPlaceKey=DE", "pubPlaceKey=AT"))
#mosaicplot(c("Samstag", "Sonnabend"), c("pubPlace=Hamburg", "pubPlace=Berlin", 'pubPlaceKey=AT'))