Learn R Programming

quanteda (version 0.99)

kwic: locate keywords-in-context

Description

For a text or a collection of texts (in a quanteda corpus object), return a list of a keyword supplied by the user in its immediate context, identifying the source text and the word index number within the source text. (Not the line number, since the text may or may not be segmented using end-of-line delimiters.)

Usage

kwic(x, pattern, window = 5, valuetype = c("glob", "regex", "fixed"),
  case_insensitive = TRUE, join = FALSE, ...)

is.kwic(x)

# S3 method for kwic as.tokens(x, ...)

Arguments

x

a character, corpus, or tokens object

pattern

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

window

the number of context words to be displayed around the keyword.

valuetype

how to interpret keyword expressions: "glob" for "glob"-style wildcard expressions; "regex" for regular expressions; or "fixed" for exact matching. See valuetype for details.

case_insensitive

match without respect to case if TRUE

join

join adjacent keywords in the concordance view if TRUE

...

additional arguments passed to tokens, for applicable object types

Value

A kwic classed data.frame, with the document name (docname), the token index positions (from and to, which will be the same for single-word patterns, or a sequence equal in length to the number of elements for multi-word phrases), the context before (pre), the keyword in its original format (keyword, preserving case and attached punctuation), and the context after (post). The return object has its own print method, plus some special attributes that are hidden in the print view. If you want to turn this into a simple data.frame, simply wrap the result in data.frame.

as.tokens.kwic converts the kwic object into a tokens object, with each new "document" consisting of one keyword match, and the contents of the pre, keyword, and post fields forming the tokens. This is one way to save the output for subsequent usage; another way is to form a corpus from the return object.

Examples

Run this code
# NOT RUN {
head(kwic(data_corpus_inaugural, "secure*", window = 3, valuetype = "glob"))
head(kwic(data_corpus_inaugural, "secur", window = 3, valuetype = "regex"))
head(kwic(data_corpus_inaugural, "security", window = 3, valuetype = "fixed"))

toks <- tokens(data_corpus_inaugural)
kwic(data_corpus_inaugural, phrase("war against"))
kwic(data_corpus_inaugural, phrase("war against"), valuetype = "regex")

mykwic <- kwic(data_corpus_inaugural, "provident*")
is.kwic(mykwic)
is.kwic("Not a kwic")
# }

Run the code above in your browser using DataLab