R/wordcount.R

Defines functions wordcount.TextReuseCorpus wordcount.TextDocument wordcount.default wordcount

Documented in wordcount

#' Count words
#'
#' This function counts words in a text, for example, a character vector, a
#' \code{\link{TextReuseTextDocument}}, some other object that inherits from
#' \code{\link[NLP]{TextDocument}}, or a all the documents in a
#' \code{\link{TextReuseCorpus}}.
#'
#' @param x The object containing a text.
#' @export
#' @return An integer vector for the word count.
wordcount <- function(x) UseMethod("wordcount", x)

#' @export
wordcount.default <- function(x) {
  assert_that(is.string(x))
  str_count(x, boundary("word"))
}

#' @export
wordcount.TextDocument <- function(x) wordcount(x$content)

#' @export
wordcount.TextReuseCorpus <- function(x) {
  vapply(x$documents, wordcount, integer(1))
}

Try the textreuse package in your browser

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

textreuse documentation built on July 8, 2020, 6:40 p.m.