Skip to content

Commit 1d9b933

Browse files
committed
Clean give_hints_authors()
1 parent f97b2e5 commit 1d9b933

2 files changed

Lines changed: 58 additions & 21 deletions

File tree

R/give_hints.R

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ give_hints_project_names <- function(project_names, dbg = TRUE) {
1616
}
1717

1818

19+
get_indices_multiple_authors <- function(author_names) {
20+
which(unlist(lapply(author_names, function(x)
21+
length(stringr::str_split(x, pattern = ",")[[1]]))) > 2)
22+
}
23+
24+
get_indices_firstname_lastname <- function(author_names) {
25+
which(
26+
stringr::str_detect(author_names, "^\\s?\\w+\\.?\\s+\\w+") &
27+
!stringr::str_detect(author_names, ",")
28+
)}
29+
30+
31+
get_indices_ends_with_semicolon <- function(author_names) {
32+
which(author_names %>%
33+
stringr::str_trim() %>%
34+
stringr::str_detect(".*,$"))
35+
}
36+
1937
#' Helper Function: Give Hints For Author Names
2038
#'
2139
#' @param author_names vector with author names to check
@@ -30,26 +48,24 @@ give_hints_author_names <- function(author_names, dbg = TRUE) {
3048
"Generate hints for 'Author Names'",
3149
dbg = dbg,
3250
expr = {
33-
multiple_authors_idx <- which(unlist(lapply(author_names, function(x)
34-
length(stringr::str_split(x, pattern = ",")[[1]]))) > 2)
35-
36-
firstname_lastname_idx <- which(
37-
stringr::str_detect(author_names, "^\\s?\\w+\\.?\\s+\\w+") &
38-
!stringr::str_detect(author_names, ",")
39-
)
40-
41-
if (length(multiple_authors_idx) > 0) {
42-
author_names[multiple_authors_idx] <- "fix_multiple_authors_per_line"
43-
}
51+
multiple_authors_idx <- get_indices_multiple_authors(author_names)
52+
firstname_lastname_idx <- get_indices_firstname_lastname(author_names)
53+
ends_with_semicolon_idx <- get_indices_ends_with_semicolon(author_names)
4454

45-
if (length(firstname_lastname_idx) > 0) {
46-
author_names[firstname_lastname_idx] <- paste0(
47-
"fix_firstname_lastname_with_",
48-
"lastname_semicolon_firstname"
49-
)
50-
}
5155

52-
replace_na_with_value(author_names, "add_author_lastname_semicolon_firstname")
56+
author_fullname$value %>%
57+
replace_indices_with_value(multiple_authors_idx,
58+
"fix_multiple_authors_per_line",
59+
dbg) %>%
60+
replace_indices_with_value(firstname_lastname_idx,
61+
paste0("fix_firstname_lastname_with_",
62+
"lastname_semicolon_firstname"),
63+
dbg) %>%
64+
replace_indices_with_value(ends_with_semicolon_idx,
65+
"fix_author_name_by_deleting_last_semicolon",
66+
dbg) %>%
67+
replace_na_with_value("add_author_lastname_semicolon_firstname",
68+
dbg)
5369
}
5470
)
5571
}

R/helper.R

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,30 @@ tidy_df <- function(df, exclude_cols = "rec_number") {
143143
}
144144

145145

146-
replace_na_with_value <- function(vector, value) {
147-
if (any(is.na(vector))) {
148-
vector[is.na(vector)] <- value
146+
replace_na_with_value <- function(vector, value, dbg = TRUE) {
147+
148+
na_indices <- which(is.na(vector))
149+
150+
if (any(na_indices)) {
151+
msg <- sprintf("Replacing %d NA values with %s",
152+
length(na_indices),
153+
value)
154+
kwb.utils::catAndRun(msg, expr = {vector[na_indices] <- value}, dbg = dbg)
155+
}
156+
157+
vector
158+
}
159+
160+
161+
replace_indices_with_value <- function(vector, indices, value, dbg = TRUE) {
162+
163+
n_indices <- length(indices)
164+
165+
if (n_indices > 0) {
166+
msg <- sprintf("Replacing %d values with %s",
167+
n_indices,
168+
value)
169+
kwb.utils::catAndRun(msg, expr = {vector[indices] <- value}, dbg = dbg)
149170
}
150171

151172
vector

0 commit comments

Comments
 (0)