warn on rewrites
Change-Id: Ib7f86993eb3febaaaeeb25b0672e4f3ab91cba1c
diff --git a/NEWS.md b/NEWS.md
index 5ba8e4d..bd4e50b 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,7 @@
# RKorAPClient 1.2.0.9000
+- warnings are now issued, when queries had to be rewritten (for licensing reasons)
+
# RKorAPClient 1.2.0
- `fetchAnnotations()` method added to `KorAPQuery` class, to fetch annotations for all collected matches
diff --git a/R/KorAPQuery.R b/R/KorAPQuery.R
index 6dd5b44..e43467d 100644
--- a/R/KorAPQuery.R
+++ b/R/KorAPQuery.R
@@ -260,6 +260,9 @@
log_info(verbose, ": API call failed\n")
totalResults <- 0
} else {
+ # Check for query rewrites and warn the user
+ warnOnRewrites(res)
+
totalResults <- as.integer(res$meta$totalResults)
log_info(verbose, ": ", totalResults, " hits")
if (!is.null(res$meta$cached)) {
@@ -337,6 +340,9 @@
message("API call failed.")
totalResults <- 0
} else {
+ # Check for query rewrites and warn the user
+ warnOnRewrites(res)
+
totalResults <- as.integer(res$meta$totalResults)
log_info(verbose, ": ", totalResults, " hits")
if (!is.null(res$meta$cached)) {
@@ -387,6 +393,17 @@
}
)
+# Helper function to check if a query rewrite warning should be shown
+warnOnRewrites <- function(res) {
+ if (!is.null(res$collection$rewrites)) {
+ comment <- res$collection$rewrites$`_comment`
+ # Only show warning if it's not just the standard policy message
+ if (!is.null(comment) && comment != "All corpus access policy has been added.") {
+ warning(res$collection$rewrites$editor, " had to rewrite your query: ", comment)
+ }
+ }
+}
+
#' @importFrom purrr map
repair_data_strcuture <- function(x) {
if (is.list(x)) {
@@ -512,11 +529,15 @@
# Rebuild the URL with all parameters
query <- httr2::url_modify(kqo@requestUrl, query = existing_query)
+
res <- apiCall(kqo@korapConnection, query)
if (length(res$matches) == 0) {
break
}
+ # Check for query rewrites and warn the user
+ warnOnRewrites(res)
+
if ("fields" %in% colnames(res$matches) && (is.na(use_korap_api) || as.numeric(use_korap_api) >= 1.0)) {
log_info(verbose, "Using fields API: ")
currentMatches <- res$matches$fields %>%
@@ -556,6 +577,7 @@
collectedMatches <- bind_rows(collectedMatches, currentMatches)
}
+
# Get the actual items per page from the API response
# We now consistently use maxResultsPerPage instead
@@ -1134,6 +1156,7 @@
#' foundry while adding morph from another).
#' @param verbose print progress information if true
#' @return The updated `kqo` object with annotation columns
+#' @return The updated `kqo` object with annotation columns
#' like `pos`, `lemma`, `morph` (and `atokens` and `annotation_snippet`)
#' in the `@collectedMatches` slot. Each column is a data frame
#' with `left`, `match`, and `right` columns containing list vectors of annotations