mirror of
https://github.com/msberends/AMR.git
synced 2026-11-16 05:00:50 +01:00
(v3.0.1.9125) Fix translations being translated again (e.g. Ticarcillinee, groupee) and untranslated parts of combination names, and let as.mo() recognise translated names in the set language, cached per language
This commit is contained in:
1 parent
d4ba5051e7
commit
04a2f0b3ac
9 files changed
+98
-35
No files matched your search
@@ -98,6 +98,7 @@ reset_ab_cache <- function() {
|
||||
|
||||
reset_mo_cache <- function() {
|
||||
AMR_env$MO_index <- NULL
|
||||
AMR_env$MO_translated <- NULL
|
||||
AMR_env$mo_accepted <- NULL
|
||||
AMR_env$mo_previously_coerced <- new_coercion_cache()
|
||||
AMR_env$mo_previously_uncertain <- NULL
|
||||
@@ -132,3 +133,29 @@ get_mo_index <- function() {
|
||||
}
|
||||
AMR_env$MO_index
|
||||
}
|
||||
|
||||
get_mo_translated_index <- function(language) {
|
||||
# translated names of microorganisms, such as "Staphylocoque à coagulase négative (CoNS)", so that as.mo() recognises the output of mo_name() in that language
|
||||
# this is built once per language and only contains the few names that change by translation
|
||||
add_MO_lookup_to_AMR_env()
|
||||
if (is.null(AMR_env$MO_translated[[language]])) {
|
||||
trns <- TRANSLATIONS[which(TRANSLATIONS$affect_mo_name == TRUE), , drop = FALSE]
|
||||
fullname <- AMR_env$MO_lookup$fullname
|
||||
is_translatable <- grepl(paste0("(", paste(trns$pattern, collapse = "|"), ")"), fullname, perl = TRUE)
|
||||
fullname <- fullname[is_translatable]
|
||||
mo <- as.character(AMR_env$MO_lookup$mo[is_translatable])
|
||||
translated <- translate_into_language(fullname, language = language, only_affect_mo_names = TRUE)
|
||||
is_changed <- translated != fullname
|
||||
translated <- translated[is_changed]
|
||||
mo <- mo[is_changed]
|
||||
# also without the abbreviation, such as "(CoNS)"
|
||||
translated_short <- gsub(" +\\([^()]+\\)$", "", translated, perl = TRUE)
|
||||
name <- tolower(c(translated, translated_short))
|
||||
mo <- c(mo, mo)
|
||||
AMR_env$MO_translated[[language]] <- list(
|
||||
name = name[!duplicated(name)],
|
||||
mo = mo[!duplicated(name)]
|
||||
)
|
||||
}
|
||||
AMR_env$MO_translated[[language]]
|
||||
}
|
||||
@@ -247,6 +247,12 @@ as.mo <- function(x,
|
||||
out[is.na(out) & x_lower %in% AMR_env$MO_lookup$fullname_lower] <- AMR_env$MO_lookup$mo[match(x_lower[is.na(out) & x_lower %in% AMR_env$MO_lookup$fullname_lower], AMR_env$MO_lookup$fullname_lower)]
|
||||
# one exception: "Fungi" matches the domain, but instead it should return the 'unknown' code for fungi
|
||||
out[out == "F_[KNG]_FUNGI"] <- "F_FUNGUS"
|
||||
# From translated names, such as the output of mo_name() in the set language ----
|
||||
if (language != "en" && any(is.na(out) & !is.na(x))) {
|
||||
mo_translated <- get_mo_translated_index(language)
|
||||
ind <- is.na(out) & x_lower %in% mo_translated$name
|
||||
out[ind] <- mo_translated$mo[match(x_lower[ind], mo_translated$name)]
|
||||
}
|
||||
# From known codes ----
|
||||
ind <- is.na(out) & toupper(x) %in% AMR::microorganisms.codes$code
|
||||
out[ind] <- AMR::microorganisms.codes$mo[match(toupper(x)[ind], AMR::microorganisms.codes$code)]
|
||||
@@ -1167,6 +1173,8 @@ convert_colloquial_input <- function(x) {
|
||||
perl = TRUE
|
||||
)
|
||||
out[x %like_case% "ha?emoly.*strep"] <- "B_STRPT_HAEM"
|
||||
# also in other word orders, like "streptocoque beta-hemolytique"
|
||||
out[x %like_case% "strep.*(beta|b\u00eata|\u03b2).?h(a?e|\u00e9)moly"] <- "B_STRPT_HAEM"
|
||||
out[x %like_case% "(strepto.* [abcg, ]{2,4}$)"] <- "B_STRPT_ABCG"
|
||||
out[x %like_case% "(strepto.* mil+er+i|^mgs[^a-z]*$)"] <- "B_STRPT_MILL"
|
||||
out[x %like_case% "mil+er+i gr"] <- "B_STRPT_MILL"
|
||||
@@ -1188,8 +1196,8 @@ convert_colloquial_input <- function(x) {
|
||||
perl = TRUE
|
||||
)
|
||||
|
||||
# CoNS/CoPS in different languages (support for German, Dutch, Spanish, Portuguese)
|
||||
out[x %like_case% "([ck]oagulas[ea].negatie?[vf]|^[ck]o?ns[^a-z]*$)"] <- "B_STPHY_CONS"
|
||||
# CoNS/CoPS in different languages (support for German, Dutch, French, Spanish, Portuguese)
|
||||
out[x %like_case% "([ck]oagulas[ea].n[e\u00e9]gatie?[vf]|^[ck]o?ns[^a-z]*$)"] <- "B_STPHY_CONS"
|
||||
out[x %like_case% "([ck]oagulas[ea].positie?[vf]|^[ck]o?ps[^a-z]*$)"] <- "B_STPHY_COPS"
|
||||
|
||||
# Gram stains
|
||||
|
||||
Binary file not shown.
+31
-27
@@ -203,26 +203,6 @@ translate_into_language <- function(from,
|
||||
df_trans <- TRANSLATIONS # internal data file
|
||||
from.bak <- from
|
||||
from_unique <- unique(from)
|
||||
from_split_combined <- function(vec) {
|
||||
sapply(vec, function(x) {
|
||||
if (grepl("/", x, fixed = TRUE)) {
|
||||
parts <- strsplit(x, "/", fixed = TRUE)[[1]]
|
||||
# Translate each part separately
|
||||
translated_parts <- translate_into_language(
|
||||
parts,
|
||||
language = lang,
|
||||
only_unknown = only_unknown,
|
||||
only_affect_ab_names = only_affect_ab_names,
|
||||
only_affect_mo_names = only_affect_mo_names
|
||||
)
|
||||
paste(translated_parts, collapse = "/")
|
||||
} else {
|
||||
x
|
||||
}
|
||||
}, USE.NAMES = FALSE)
|
||||
}
|
||||
from_unique_translated <- from_split_combined(from_unique)
|
||||
|
||||
# only keep lines where translation is available for this language
|
||||
df_trans <- df_trans[which(!is.na(df_trans[, lang, drop = TRUE])), , drop = FALSE]
|
||||
# and where the original string is not equal to the string in the target language
|
||||
@@ -263,26 +243,50 @@ translate_into_language <- function(from,
|
||||
df_trans$pattern[df_trans$regular_expr == TRUE] <- gsub("$$", "$", df_trans$pattern[df_trans$regular_expr == TRUE], fixed = TRUE)
|
||||
}
|
||||
|
||||
# non-regex part
|
||||
# sort trans file on length of pattern, so that the longest pattern is replaced first (word boundaries of regular expressions do not count)
|
||||
trns <- df_trans[order(nchar(gsub("\\b", "", df_trans$pattern, fixed = TRUE)), decreasing = TRUE), , drop = FALSE]
|
||||
# every translation is first replaced by a placeholder, so that a translation is never translated again, e.g. "Ticarcillin" to "Ticarcilline" and then to "Ticarcillinee"
|
||||
# placeholders consist of ASCII control characters only, so that no pattern can match them
|
||||
control_chars <- vapply(FUN.VALUE = character(1), 3:31, function(i) rawToChar(as.raw(i)))
|
||||
placeholders <- paste0("\001", control_chars[(seq_len(nrow(trns)) - 1) %/% length(control_chars) %/% length(control_chars) + 1], control_chars[(seq_len(nrow(trns)) - 1) %/% length(control_chars) %% length(control_chars) + 1], control_chars[(seq_len(nrow(trns)) - 1) %% length(control_chars) + 1], "\002")
|
||||
replacements <- trns[, lang, drop = TRUE]
|
||||
# keep backreferences like "\\1" of regular expressions outside the placeholder
|
||||
literals <- gsub("\\\\[0-9]", "", replacements)
|
||||
replacements_placeholder <- ifelse(
|
||||
literals == "",
|
||||
replacements,
|
||||
vapply(FUN.VALUE = character(1), seq_along(replacements), function(i) sub(literals[i], placeholders[i], replacements[i], fixed = TRUE))
|
||||
)
|
||||
translate_exec <- function(term) {
|
||||
# sort trans file on length of pattern
|
||||
trns <- df_trans[order(nchar(df_trans$pattern), decreasing = TRUE), ]
|
||||
for (i in seq_len(nrow(trns))) {
|
||||
term <- gsub(
|
||||
pattern = trns$pattern[i],
|
||||
replacement = trns[i, lang, drop = TRUE],
|
||||
replacement = replacements_placeholder[i],
|
||||
x = term,
|
||||
ignore.case = !trns$case_sensitive[i] & trns$regular_expr[i],
|
||||
fixed = !trns$regular_expr[i],
|
||||
perl = trns$regular_expr[i],
|
||||
perl = trns$regular_expr[i]
|
||||
)
|
||||
}
|
||||
for (i in which(vapply(FUN.VALUE = logical(1), placeholders, grepl, x = term, fixed = TRUE))) {
|
||||
term <- gsub(placeholders[i], literals[i], term, fixed = TRUE)
|
||||
}
|
||||
term
|
||||
}
|
||||
from_unique_translated[order(nchar(from_unique_translated), decreasing = TRUE)] <- vapply(
|
||||
|
||||
from_unique_translated <- from_unique
|
||||
# combinations like "Ticarcillin/clavulanic acid" are translated per part, unless the combination itself is in the translations
|
||||
is_combination <- grepl("/", from_unique, fixed = TRUE) & !from_unique %in% df_trans$pattern
|
||||
from_unique_translated[is_combination] <- vapply(
|
||||
FUN.VALUE = character(1),
|
||||
USE.NAMES = FALSE,
|
||||
from_unique_translated[order(nchar(from_unique_translated), decreasing = TRUE)],
|
||||
strsplit(from_unique[is_combination], "/", fixed = TRUE),
|
||||
function(parts) paste(translate_into_language(parts, language = lang, only_unknown = only_unknown, only_affect_ab_names = only_affect_ab_names, only_affect_mo_names = only_affect_mo_names), collapse = "/")
|
||||
)
|
||||
from_unique_translated[!is_combination] <- vapply(
|
||||
FUN.VALUE = character(1),
|
||||
USE.NAMES = FALSE,
|
||||
from_unique[!is_combination],
|
||||
translate_exec
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user