R/add_count.R

Defines functions add_count

add_count <- function(ng) {
  if (attr(ng, "smoothing") > 0) {
    warning("Counts are only hypothetical when data is smoothed.",
            call. = FALSE)
  }
  ng_attrib <- attributes(ng)[c("case_sensitive", "smoothing")]
  ng$Phrase.n <- unlist(lapply(stringr::str_split(as.character(ng$Phrase), " "),
                               length))
  ng <- merge(ng, corpus_totals[, 1:4])
  ng$Count <- ng$Frequency * (ng$N.1grams - ng$Pages * (ng$Phrase.n - 1))
  ng$Count <- round(ng$Count, 0)
  ng$N.1grams <- NULL
  ng$Pages <- NULL
  ng$Phrase.n <- NULL
  attributes(ng) <- c(attributes(ng), ng_attrib)
  class(ng) <- c("ngram", class(ng))
  return(ng)
}

Try the ngramr package in your browser

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

ngramr documentation built on Jan. 16, 2023, 5:07 p.m.