Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 1 | setGeneric("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 Kupietz | 3687a8c | 2024-01-24 16:18:36 +0100 | [diff] [blame] | 16 | #' @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 Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 18 | #' |
| 19 | #' @importFrom urltools url_encode |
| 20 | #' @importFrom dplyr bind_rows relocate mutate |
Marc Kupietz | 2a8ab82 | 2024-06-26 21:13:37 +0200 | [diff] [blame^] | 21 | #' @importFrom tibble as_tibble |
| 22 | #' @importFrom tidyr pivot_wider |
Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 23 | #' |
| 24 | #' @examples |
| 25 | #' \dontrun{ |
| 26 | #' new("KorAPConnection") %>% textMetadata(c("WUD17/A97/08542", "WUD17/B96/57558", "WUD17/A97/08541")) |
| 27 | #' } |
| 28 | #' |
| 29 | #' @export |
| 30 | setMethod("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 Kupietz | 2a8ab82 | 2024-06-26 21:13:37 +0200 | [diff] [blame^] | 46 | if ("document" %in% names(res) & "fields" %in% names(res$document)) { |
| 47 | 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 Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 54 | relocate(textSigle) |
Marc Kupietz | 2a8ab82 | 2024-06-26 21:13:37 +0200 | [diff] [blame^] | 55 | } 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 Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 65 | } |
| 66 | res |
| 67 | } |
| 68 | }) |
| 69 | |
| 70 | |