The following counting methods are currently implemented:
ngram
Count all word n-grams of order 1,...,n
.
string
Count all word sequence n-grams of order n
.
prefix
Count all word prefixes of at most length n
.
suffix
Count all word suffixes of at most length n
.
The n-grams of a word are defined to be the substrings of length
n = min(length(word), n)
starting at positions
1,...,length(word)-n
. Note that the value of marker
is pre- and appended to word before counting. However, the empty word
is never marked and therefore not counted. Note that
marker = "\1"
is reserved for counting of an efficient set
of ngrams and marker = "\2"
for the set proposed by Cavnar
and Trenkle (see references).
If method = "string"
word-sequences of and only of length
n
are counted. Therefore, documents with less than n
words are omitted.
By default all documents are preprocessed and counted using a single C
function call. For large document collections this may come at the
price of considerable memory consumption. If persistent = TRUE
and
recursive = TRUE
documents are counted incrementally, i.e., into a
persistent prefix tree using as many C function calls as there are
documents. Further, if persistent = TRUE
and recursive = FALSE
the documents are counted using a single call but no result is returned
until the next call with persistent = FALSE
. Thus, persistent
acts as a switch with the counts being accumulated until release. Timing
statistics have shown that incremental counting can be order of
magnitudes faster than the default.
Be aware that the character strings in the documents are translated
to the encoding of the current locale if the encoding is set (see
Encoding
). Therefore, with the possibility of "unknown"
encodings when in an "UTF-8"
locale, or invalid "UTF-8"
strings declared to be in "UTF-8"
, the code checks if each string
is a valid "UTF-8"
string and stops if not. Otherwise, strings
are processed bytewise without any checks. However, embedded nul
bytes are always removed from a string. Finally, note that during
incremental counting a change of locale is not allowed (and a change
in method is not recommended).
Note that the C implementation counts words into a prefix tree. Whereas this is highly efficient for n-gram, prefix, or suffix counting
it may be less efficient for simple word counting. That is, implementations
which use hash tables may be more efficient if the dictionary is large.
format.textcnt
pretty prints a named vector of counts (see below)
including information about the rank and encoding details of the strings.