% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ngrams.R
\name{count_ngrams}
\alias{count_ngrams}
\title{Count n-grams from segmented text}
\usage{
count_ngrams(
  x,
  ...,
  n = 2,
  sep = " ",
  sort = TRUE,
  format = c("data.frame", "vector")
)
}
\arguments{
\item{x}{A character vector of tokens or a list of character vectors.}

\item{...}{Must be empty. This enforces that optional arguments such as \code{n},
\code{sep}, \code{sort}, and \code{format} are supplied with explicit names.}

\item{n}{A positive integer or integer vector giving the n-gram sizes to
count. The default is \code{2}. If \code{n} is a integer vector of length > 1,
n-grams of all specified sizes will be counted.}

\item{sep}{Separator inserted between tokens when constructing the n-gram
label. The default is \code{" "}, a single space.}

\item{sort}{Whether to sort results by descending frequency. The default
is \code{TRUE}. If \code{FALSE}, results keep first-appearance order within each
requested n.}

\item{format}{Output format. \code{"data.frame"} returns a data frame with
\code{term}, \code{n}, and \code{count} columns. \code{"vector"} returns a named integer
vector using the n-gram terms as names.}
}
\value{
N-gram counts in the requested format.
}
\description{
Count contiguous n-grams from a segmented character vector or from each
element of a list of segmented character vectors.

This function is a drop-in replacement for \code{jiebaR::get_tuple()}, which
is deprecated in \code{jiebaRS}. See Details for more information.
}
\details{
The original \code{jiebaR::get_tuple()} interface has several design problems:
\enumerate{
\item Its n-gram extraction behavior does not match the most obvious reading of
the argument name: \code{size = n} counts all contiguous n-grams from \code{2:n},
not just the exact size \code{n}.
\item Its documentation says it accepts list input, but the original exported
implementation does not reliably support lists.
\item It concatenates tokens without a separator, which makes tuple boundaries
ambiguous.
}

\code{count_ngrams()} addresses these issues, providing more explicit and
abundant parameters. In addition, this function is about \strong{1.3x} to
\strong{2.0x} faster than \code{jiebaR::get_tuple()}.
}
\examples{
count_ngrams(c("\u6211", "\u7231", "R"), n = 2)
count_ngrams(c("\u6211", "\u7231", "R"), n = 1:2, format = "data.frame")
count_ngrams(c("a", "b", "b", "b", "a"), n = 1, sort = FALSE)
count_ngrams(list(c("a", "b", "c"), c("a", "b")), n = 2)
}
\seealso{
\code{\link[=get_tuple]{get_tuple()}}
}
