x <- c("We", "walked", "anxiously", "to", "the", "doctor", "!")
## Look 1 word before + word itself
y <- txt_context(x, n = c(-1, 0), na.rm = FALSE)
data.frame(x, y)
## Look 1 word before + word itself + 1 word after
y <- txt_context(x, n = c(-1, 0, 1), na.rm = FALSE)
data.frame(x, y)
y <- txt_context(x, n = c(-1, 0, 1), na.rm = TRUE)
data.frame(x, y)
## Look 2 words before + word itself + 1 word after
## even if not all words are there
y <- txt_context(x, n = c(-2, -1, 0, 1), na.rm = TRUE, sep = "_")
data.frame(x, y)
y <- txt_context(x, n = c(-2, -1, 1, 2), na.rm = FALSE, sep = "_")
data.frame(x, y)
x <- c("We", NA, NA, "to", "the", "doctor", "!")
y <- txt_context(x, n = c(-1, 0), na.rm = FALSE)
data.frame(x, y)
y <- txt_context(x, n = c(-1, 0), na.rm = TRUE)
data.frame(x, y)
library(data.table)
data(brussels_reviews_anno, package = "udpipe")
x <- as.data.table(brussels_reviews_anno)
x <- subset(x, doc_id %in% txt_sample(unique(x$doc_id), n = 10))
x <- x[, context := txt_context(lemma), by = list(doc_id, sentence_id)]
head(x, 20)
x$term <- sprintf("%s/%s", x$lemma, x$upos)
x <- x[, context := txt_context(term), by = list(doc_id, sentence_id)]
head(x, 20)
Run the code above in your browser using DataLab