library(cwbtools)
library(RcppCWB)
library(NLP)
library(openNLP)
library(data.table)
<- cl_attribute_size("UNGA", attribute = "word", attribute_type ="p")
corpus_size <- data.table(cpos = 0L:(corpus_size - 1L))
dt <- dt[, "id" := cl_cpos2id("UNGA", p_attribute = "word", cpos = dt[["cpos"]])]
dt "word" := cl_id2str("UNGA", p_attribute = "word", id = dt[["id"]])] dt[,
"whitespace" := c(ifelse(word %in% c(".", ",", ":", "!", "?", ";"), FALSE, TRUE)[2L:length(word)], FALSE)]
dt[, <- String(paste(paste(dt[["word"]], ifelse(dt[["whitespace"]], " ", ""), sep = ""), collapse = "")) txt
"nchar" := sapply(dt[["word"]], nchar)]
dt[, "start" := c(1L, (cumsum(dt[["nchar"]] + dt[["whitespace"]]) + 1L)[1L:(nrow(dt) - 1L)])]
dt[, "end" := (dt[["start"]] + dt[["nchar"]] - 1L)]
dt[,
<- NLP::Annotation(
w id = dt[["cpos"]],
rep.int("word", nrow(dt)),
start = dt[["start"]],
end = dt[["end"]]
)
<- Maxent_Sent_Token_Annotator()
sentence_annotator <- annotate(s = txt, f = sentence_annotator)
s
<- as.data.table((as.Span(s)))[, "sentence" := 1L:length(s)]
spans "cpos_left" := dt[, c("cpos", "start", "end")][spans, on = "start"][["cpos"]]]
spans[, "cpos_right" := dt[, c("cpos", "start", "end")][spans, on = "end"][["cpos"]]]
spans[, <- spans[, c("cpos_left", "cpos_right", "sentence")] regions
<- Maxent_POS_Tag_Annotator(language = "en", probs = FALSE, model = NULL)
pos_annotator <- annotate(s = txt, f = pos_annotator, a = c(w, s))
p <- unlist(lapply(as.data.frame(p)[["features"]], `[[`, "POS")) pos
install.packages(pkgs = "openNLPmodels.en", repos = "https://datacube.wu.ac.at")
<- Maxent_Entity_Annotator(
ne_annotator language = "en",
kind = "person",
probs = FALSE,
model = NULL
)<- annotate(s = txt, f = ne_annotator, a = c(w, s))
ne
<- subset(ne, type == "entity")
ne_min <- as.data.table((as.Span(ne_min)))
spans "cpos_left" := dt[, c("cpos", "start", "end")][spans, on = "start"][["cpos"]]]
spans[, "cpos_right" := dt[, c("cpos", "start", "end")][spans, on = "end"][["cpos"]]]
spans[, "ne" := sapply(as.data.frame(ne_min)[["features"]], `[[`, "kind")]
spans[, <- spans[, c("cpos_left", "cpos_right", "ne")]
regions
<- regions[, {paste(dt[cpos %in% .SD[["cpos_left"]]:.SD[["cpos_right"]]][["word"]], collapse = " ")}, by = "cpos_left", .SDcols = c("cpos_left", "cpos_right")][["V1"]] txt
<- unlist(lapply(as.data.frame(p)[["features"]], `[[`, "POS")) pos