blob: 32d8d128c96e1ba33b6716b6c4bebe093045c322 [file] [log] [blame]
Marc Kupietz6ddece42023-12-18 17:02:36 +01001setGeneric("textMetadata", function(kco, ...) standardGeneric("textMetadata") )
2
3#' Retrieve metadata for a text, identified by its sigle (id)
4#'
5#' @aliases textMetadata
6#'
7#' @description
8#' Retrieves metadata for a text, identified by its sigle (id) using the corresponding KorAP API
9#' (see [Kustvakt Wiki](https://github.com/KorAP/Kustvakt/wiki/Service:-Metadata-Retrieval)).
10#'
11#'
12#' @param kco [KorAPConnection()] object (obtained e.g. from `new("KorAPConnection")`)
13#' @param textSigle unique text id (concatenation of corpus, document and text ids, separated by `/`, e.g. ) or vector thereof
14#' @param verbose logical. If `TRUE`, additional diagnostics are printed. Defaults to `kco@verbose`.
15#'
Marc Kupietz3687a8c2024-01-24 16:18:36 +010016#' @return Tibble with columns for each metadata property. In case of errors, such as non-existing texts/sigles, the tibble will also contain a column called `errors`.
17#' If there are metadata columns you cannot make sense of, please ignore them. The function simply returns all the metadata it gets from the server.
Marc Kupietz6ddece42023-12-18 17:02:36 +010018#'
19#' @importFrom urltools url_encode
20#' @importFrom dplyr bind_rows relocate mutate
Marc Kupietz2a8ab822024-06-26 21:13:37 +020021#' @importFrom tibble as_tibble
22#' @importFrom tidyr pivot_wider
Marc Kupietz6ddece42023-12-18 17:02:36 +010023#'
24#' @examples
25#' \dontrun{
26#' new("KorAPConnection") %>% textMetadata(c("WUD17/A97/08542", "WUD17/B96/57558", "WUD17/A97/08541"))
27#' }
28#'
29#' @export
30setMethod("textMetadata", "KorAPConnection",
31 function(kco, textSigle, verbose = kco@verbose) {
32 if (length(textSigle) > 1)
33 do.call(bind_rows, Map(function(atomicSigle)
34 textMetadata(kco, atomicSigle), textSigle))
35 else {
36 url <-
37 paste0(kco@apiUrl, 'corpus/',
38 URLencode(enc2utf8(textSigle), reserved = TRUE))
39 log_info(verbose, "Getting metadata for ", textSigle, sep = "")
40 res <- apiCall(kco, url)
41 log_info(verbose, ifelse(is.null(res) || "errors" %in% names(res), " [error]\n", "\n"))
42
43 if(is.null(res)) {
44 res <- tibble(errors="API request failed")
45 } else {
Marc Kupietz05664aa2024-12-07 16:56:50 +010046 if ("document" %in% names(res) & "fields" %in% names(res$document) && length(res$document$fields) > 0) {
Marc Kupietz2a8ab822024-06-26 21:13:37 +020047 res <- as_tibble(res$document$fields) %>%
48 select(key, value) %>%
49 tidyr::pivot_wider(names_from = key, values_from = value, names_repair = "unique") %>%
50 mutate(
51 textSigle = as.character(textSigle),
52 requestUrl = url,
53 webUIRequestUrl = paste0(kco@KorAPUrl, sprintf('?q=<base/s=t>&cq=textSigle+%%3D+"%s"', url_encode(enc2utf8(textSigle))))) %>%
Marc Kupietz6ddece42023-12-18 17:02:36 +010054 relocate(textSigle)
Marc Kupietz2a8ab822024-06-26 21:13:37 +020055 } else {
56 res <- lapply(res, function(x) paste0(x, collapse = "\\t")) # flatten list
57 res <- as_tibble(res) %>%
58 head(n=1) %>%
59 mutate(
60 requestUrl = url,
61 textSigle = textSigle,
62 webUIRequestUrl = paste0(kco@KorAPUrl, sprintf('?q=<base/s=t>&cq=textSigle+%%3D+"%s"', url_encode(enc2utf8(textSigle))))) %>%
63 relocate(textSigle)
64 }
Marc Kupietz6ddece42023-12-18 17:02:36 +010065 }
66 res
67 }
68})
69
70