R/name.R

Defines functions strip_business_legal_entity_type

Documented in strip_business_legal_entity_type

#' Strip business legal entity type from name
#'
#' @param string A character vector.
#' @param trim_ws A boolean; determines if trailing white spaces (generated by
#'   the replacement), are trimmed or not.
#' @param squish_ws A boolean; determines if trailing white spaces and repeated
#'   whitespace inside a string (generated by the replacement), are trimmed or
#'   not.
#'
#' @return A character vector.
#' @export
#'
#' @examples
#' strip_business_legal_entity_type("Farmer Box Plc.")
#'
#' @importFrom stringr str_replace str_trim
#' @importFrom purrr when
strip_business_legal_entity_type <- function(string, trim_ws = FALSE, squish_ws = TRUE) {
  pattern <- "(?:^|\\b)(?:Incorporated|[Ll]imited|P\\.C|[Ll][Tt][Dd][Ss]?|[Pp][Ll]{1,2}[Cc]|[Ll]{2}[Pp])\\.?"
  string %>%
    str_replace_all(pattern, "") %>%
    when(trim_ws ~ str_trim(.),
         ~.
    ) %>%
    when(squish_ws ~ str_squish(.),
         ~.
    )
}


#' Strip honorific title from name
#'
#' Ms, Mrs, Miss, Mr
#'
#' @param string A character vector.
#' @param trim_ws A boolean; determines if trailing white spaces (generated by
#'   the replacement), are trimmed or not.
#' @param squish_ws A boolean; determines if trailing white spaces and repeated
#'   whitespace inside a string (generated by the replacement), are trimmed or
#'   not.
#'
#' @return A character vector.
#' @export
#'
#' @examples
#' strip_business_legal_entity_type("Farmer Box Plc.")
#'
#' @importFrom stringr str_replace str_trim
#' @importFrom purrr when
strip_honorific_title <- function(string, trim_ws = FALSE, squish_ws = TRUE) {
  pattern <- paste0(
    "(\\b([Mm][RrSs]{1,2}|[Mm]iss|[Dd][Rr]|[Pp][Rr][Oo][Ff](essor)?)(\\.|\\b))",
    "(\\sand\\s|\\s&\\s)?",
    "(\\b([Mm][RrSs]{1,2}|[Mm]iss|[Dd][Rr]|[Pp][Rr][Oo][Ff](essor)?)(\\.|\\b))?"
  )

  string %>%
    str_replace_all(pattern, "") %>%
    when(
      trim_ws ~ str_trim(.),
      ~.
    ) %>%
    when(
      squish_ws ~ str_squish(.),
      ~.
    )
}
xavier-gilbert/sabre documentation built on May 7, 2021, 12:40 p.m.