Count n-grams, either of words, or of characters.

ngrams(.Object, ...)

# S4 method for class 'partition'
ngrams(
  .Object,
  n = 2,
  p_attribute = "word",
  char = NULL,
  progress = FALSE,
  mc = 1L,
  ...
)

# S4 method for class 'character'
ngrams(
  .Object,
  n = 2,
  p_attribute = "word",
  char = NULL,
  progress = FALSE,
  mc = 1L,
  ...
)

# S4 method for class 'partition'
ngrams(
  .Object,
  n = 2,
  p_attribute = "word",
  char = NULL,
  progress = FALSE,
  mc = 1L,
  ...
)

# S4 method for class 'subcorpus'
ngrams(
  .Object,
  n = 2,
  p_attribute = "word",
  char = NULL,
  progress = FALSE,
  mc = 1L,
  ...
)

# S4 method for class 'character'
ngrams(
  .Object,
  n = 2,
  p_attribute = "word",
  char = NULL,
  progress = FALSE,
  mc = 1L,
  ...
)

# S4 method for class 'data.table'
ngrams(.Object, n = 2L, p_attribute = "word")

# S4 method for class 'corpus'
ngrams(
  .Object,
  n = 2,
  p_attribute = "word",
  char = NULL,
  progress = FALSE,
  mc = 1L,
  ...
)

# S4 method for class 'list'
ngrams(
  .Object,
  n = 2,
  char = NULL,
  mc = FALSE,
  verbose = FALSE,
  progress = FALSE,
  ...
)

# S4 method for class 'partition_bundle'
ngrams(
  .Object,
  n = 2,
  char = NULL,
  vocab = NULL,
  p_attribute = "word",
  mc = FALSE,
  verbose = FALSE,
  progress = FALSE,
  ...
)

Arguments

.Object

object of class partition

...

Further arguments.

n

Number of tokens (if char is NULL) or characters otherwise.

p_attribute

the p-attribute to use (can be > 1)

char

If NULL, tokens will be counted, else characters, keeping only those provided by a character vector

progress

A logical value.

mc

A logical value, whether to use multicore, passed into call to blapply().

verbose

A length-one logical value, whether to show messages.

vocab

A character vector with an alternative vocabulary to the one stored on disk.

Examples

use("polmineR")
#> ✔ corpus loaded: GERMAPARLMINI (version: 0.1.0 | build date: 2023-04-16)
P <- partition("GERMAPARLMINI", date = "2009-10-27")
#> ... get cpos and strucs
ngrm <- ngrams(P, n = 2, p_attribute = "word", char = NULL)

# a more complex scenario: get most frequent ADJA/NN-combinations
ngrm <- ngrams(P, n = 2, p_attribute = c("word", "pos"), char = NULL)
ngrm2 <- subset(
 ngrm,
 ngrm[["1_pos"]] == "ADJA"  & ngrm[["2_pos"]] == "NN"
 )
#> Error in .checkTypos(e, names_x): Object 'ngrm' not found amongst [word_1, pos_1, word_2, pos_2, count]
ngrm2@stat[, "1_pos" := NULL][, "2_pos" := NULL]
#> Error: object 'ngrm2' not found
ngrm3 <- sort(ngrm2, by = "count")
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'sort': object 'ngrm2' not found
head(ngrm3)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'head': object 'ngrm3' not found
use(pkg = "RcppCWB", corpus = "REUTERS")
#> ✔ corpus loaded: REUTERS
dt <- decode("REUTERS", p_attribute = "word", s_attribute = character(), to = "data.table")
#> ℹ decoding p-attribute: word
#> ✔ decoding p-attribute: word [10ms]
#> 
#> ℹ assembling data.table
#> ✔ assembling data.table [6ms]
#> 
y <- ngrams(dt, n = 3L, p_attribute = "word")