Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shorten "editors" names #5

Closed
mrustl opened this issue Aug 4, 2020 · 2 comments
Closed

feat: shorten "editors" names #5

mrustl opened this issue Aug 4, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@mrustl
Copy link
Member

mrustl commented Aug 4, 2020

Add function to shorten editor names in format lastname, firstname (Rustler, Michael) to lastname firstname (Rustler M.)

To be added here:

clean_editors <- function(string) {

Maybe @hsonne has a nice code chunk for it?

Example dataset attached
editors.zip

string <- readRDS(file = "editors.Rds")
kwb.pubs:::clean_editors(string)

@mrustl mrustl added enhancement New feature or request help wanted Extra attention is needed labels Aug 4, 2020
@mrustl mrustl self-assigned this Aug 4, 2020
@hsonne
Copy link
Member

hsonne commented Aug 4, 2020

Hi @mrustl, here is my proposition, for you to test and integrate:

if (FALSE)
{
  shorten_first_name("Christian")
  shorten_first_name("Hans-Peter")
  shorten_first_name("Alfons-Peter Alexander")
  shorten_first_name("Michael Andreas Oliver")
  
  editors <- readRDS("~/../Downloads/S/support/michael-rustler/editors.Rds")
  
  abbreviated <- lapply(strsplit(editors, "\r"), function(x) {
    sapply(x, abbreviate_author, USE.NAMES = FALSE)
  })
  
  sapply(abbreviated, paste, collapse = "\r")
}

# abbreviate_author ------------------------------------------------------------
abbreviate_author <- function(x) {
  last_first <- strsplit(x, "\\s*,\\s*")[[1L]]
  
  if (length(last_first) > 1L) {
    last_first[2L] <- shorten_first_name(last_first[2L])
  }
  
  paste(last_first, collapse = " ")
}

# shorten_first_name -----------------------------------------------------------
shorten_first_name <- function(x) {
  paste(sapply(strsplit(x, "\\s+")[[1]], shorten_dashed_name), collapse = " ")
}

# shorten_dashed_name ----------------------------------------------------------
shorten_dashed_name <- function(x) {
  paste(sapply(strsplit(x, "-")[[1]], dot_after_first_char), collapse = "-")
}

# dot_after_first_char ---------------------------------------------------------
dot_after_first_char <- function(x) {
  paste0(substr(x, 1L, 1L), ".")
}

mrustl added a commit that referenced this issue Aug 4, 2020
@mrustl
Copy link
Member Author

mrustl commented Aug 4, 2020

@hsonne many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants