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 | 05664aa | 2024-12-07 16:56:50 +0100 | [diff] [blame] | 46 | if ("document" %in% names(res) & "fields" %in% names(res$document) && length(res$document$fields) > 0) { |
Marc Kupietz | 2a8ab82 | 2024-06-26 21:13:37 +0200 | [diff] [blame] | 47 | res <- as_tibble(res$document$fields) %>% |
Marc Kupietz | 06e5b97 | 2024-12-07 17:22:06 +0100 | [diff] [blame^] | 48 | dplyr::mutate(across(where(is.list), ~ purrr::map(.x, ~ if (length(.x) < 2) unlist(.x) else paste(.x, collapse = "\\t")))) %>% |
Marc Kupietz | 2a8ab82 | 2024-06-26 21:13:37 +0200 | [diff] [blame] | 49 | select(key, value) %>% |
| 50 | tidyr::pivot_wider(names_from = key, values_from = value, names_repair = "unique") %>% |
| 51 | mutate( |
| 52 | textSigle = as.character(textSigle), |
| 53 | requestUrl = url, |
| 54 | webUIRequestUrl = paste0(kco@KorAPUrl, sprintf('?q=<base/s=t>&cq=textSigle+%%3D+"%s"', url_encode(enc2utf8(textSigle))))) %>% |
Marc Kupietz | 06e5b97 | 2024-12-07 17:22:06 +0100 | [diff] [blame^] | 55 | mutate(across(everything(), as.character)) %>% |
| 56 | relocate(textSigle) |
Marc Kupietz | 2a8ab82 | 2024-06-26 21:13:37 +0200 | [diff] [blame] | 57 | } else { |
| 58 | res <- lapply(res, function(x) paste0(x, collapse = "\\t")) # flatten list |
| 59 | res <- as_tibble(res) %>% |
| 60 | head(n=1) %>% |
| 61 | mutate( |
| 62 | requestUrl = url, |
| 63 | textSigle = textSigle, |
| 64 | webUIRequestUrl = paste0(kco@KorAPUrl, sprintf('?q=<base/s=t>&cq=textSigle+%%3D+"%s"', url_encode(enc2utf8(textSigle))))) %>% |
| 65 | relocate(textSigle) |
| 66 | } |
Marc Kupietz | 6ddece4 | 2023-12-18 17:02:36 +0100 | [diff] [blame] | 67 | } |
| 68 | res |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | |