R/helper_calculate_score.R

Defines functions calculate_score

#' Calculate score for keywords - helper.
#'
#' Helper function. Calculates score for keywords.
#'
#' @param string String. String to search for keywords.
#' @param keywords Character vector. Vector containing keywords to
#' search for in string.
#' @param case Boolean. If `case = TRUE`, keywords are case sensitive.
#' If `case = FALSE`, keywords are case insensitive.
#'
#' @return Integer corresponding keyword score
#' @noRd
calculate_score <- function(string,
                            keywords,
                            case = FALSE) {

    if(case == FALSE) {
        score <- stringr::str_match_all(stringr::str_to_lower(string),
                                        stringr::str_to_lower(keywords)) %>%
            purrr::flatten() %>%
            length()
    } else if (case == TRUE) {
        score <- stringr::str_match_all(string, keywords) %>%
            purrr::flatten() %>%
            length()
    }


  return(score)
}

Try the miRetrieve package in your browser

Any scripts or data that you put into this service are public.

miRetrieve documentation built on Sept. 19, 2021, 1:06 a.m.