tokens_segment: Segment tokens object by patterns

Description Usage Arguments Value Examples

View source: R/tokens_segment.R

Description

Segment tokens by splitting on a pattern match. This is useful for breaking the tokenized texts into smaller document units, based on a regular pattern or a user-supplied annotation. While it normally makes more sense to do this at the corpus level (see corpus_segment), tokens_segment provides the option to perform this operation on tokens.

Usage

1
2
3
tokens_segment(x, pattern, valuetype = c("glob", "regex", "fixed"),
  case_insensitive = TRUE, extract_pattern = FALSE,
  pattern_position = c("before", "after"), use_docvars = TRUE)

Arguments

x

tokens object whose token elements will be segmented

pattern

a character vector, list of character vectors, dictionary, or collocations object. See pattern for details.

valuetype

the type of pattern matching: "glob" for "glob"-style wildcard expressions; "regex" for regular expressions; or "fixed" for exact matching. See valuetype for details.

case_insensitive

ignore case when matching, if TRUE

extract_pattern

remove matched patterns from the texts and save in docvars, if TRUE

pattern_position

either "before" or "after", depending on whether the pattern precedes the text (as with a tag) or follows the text (as with punctuation delimiters)

use_docvars

if TRUE, repeat the docvar values for each segmented text; if FALSE, drop the docvars in the segmented corpus. Dropping the docvars might be useful in order to conserve space or if these are not desired for the segmented corpus.

Value

tokens_segment returns a tokens object whose documents have been split by patterns

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
txts <- "Fellow citizens, I am again called upon by the voice of my country to
execute the functions of its Chief Magistrate. When the occasion proper for
it shall arrive, I shall endeavor to express the high sense I entertain of
this distinguished honor."
toks <- tokens(txts)

# split by any punctuation
tokens_segment(toks, "^\\p{Sterm}$", valuetype = "regex", 
               extract_pattern = TRUE, 
               pattern_position = "after")
tokens_segment(toks, c(".", "?", "!"), valuetype = "fixed", 
               extract_pattern = TRUE, 
               pattern_position = "after")

quanteda/quanteda documentation built on Feb. 16, 2019, 5:45 a.m.