R/getCapitalAttributes.R

Defines functions getCapitalAttributes

Documented in getCapitalAttributes

#' getCapitalAttributes
#'
#' Calculate the mean, max, and sum of substring containing consecutive capital letters
#' @param text Text where the attributes will be calculated from
#' @return Vector of the mean, max, and sum
#' @examples
#' getCapitalAttributes("MY text HASmany CAPITAL LETTERS")
#'

getCapitalAttributes <- function(text) {
  capitalWords <- regmatches(text, gregexpr("(([A-Z]+\\s)+|[A-Z]+)", text))
  wordsLength <- nchar(capitalWords[[1]])
  if (length(capitalWords[[1]]) == 0) wordsLength = c(0)
  capitalAverage <- mean(wordsLength)
  capitalLongest <- max(wordsLength)
  capitalTotal <- sum(wordsLength)
  return(c(capitalAverage, capitalLongest, capitalTotal))
}
megahf/spamfilter documentation built on May 29, 2019, 4:42 a.m.