R: annotate () gives error in R
I am new to R. I have to use POSTagger in my code. I am using openNLP with R. While trying to execute the following example code (in Sample.R file):
library("NLP")
library("openNLP")
s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
"nonexecutive director Nov. 29.\n",
"Mr. Vinken is chairman of Elsevier N.V., ",
"the Dutch publishing group."),
collapse = "")
s <- as.String(s)
sent_token_annotator <- Maxent_Sent_Token_Annotator()
a1 <- annotate(s, sent_token_annotator)
s[a1]
And running this code from the R Console (using source ("Sample.R")) I get the following error:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class "c("Simple_POS_Tag_Annotator", "Annotator")" to a data.frame
Following is the output of the traceback () command:
14: stop(gettextf("cannot coerce class \"%s\" to a data.frame", deparse(class(x))),
domain = NA)
13: as.data.frame.default(x[[i]], optional = TRUE)
12: as.data.frame(x[[i]], optional = TRUE)
11: data.frame(x = function (s, a = Annotation())
{
s <- as.String(s)
y <- f(s)
n <- length(y)
id <- .seq_id(next_id(a$id), n)
type <- rep.int("sentence", n)
if (is.Annotation(y)) {
y$id <- id
y$type <- type
}
else if (is.Span(y)) {
y <- as.Annotation(y, id = id, type = type)
}
else stop("Invalid result from underlying sentence tokenizer.")
if (length(i <- which(a$type == "paragraph"))) {
a <- a[i]
a$features <- lapply(annotations_in_spans(y, a), function(e) list(constituents = e$id))
y <- c(y, a)
}
y
}, check.names = FALSE, stringsAsFactors = FALSE)
10: eval(expr, envir, enclos)
9: eval(as.call(c(expression(data.frame), x, check.names = !optional,
stringsAsFactors = stringsAsFactors)))
8: as.data.frame.list(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors)
7: as.data.frame(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors)
6: data.frame(position)
5: annotate(s, sent_token_annotator) at sample.R#11
4: eval(expr, envir, enclos)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("sample.R")
What could be wrong? I am using Rx64 3.1.1 on Windows 7. Any help would be much appreciated. Thanks in advance.
source to share
I have the same problem and fixed it by removing / detaching the ggplot2 package. Ggplot2 has an Annotate function and it has the same name in both packages. I suggest you make sure it looks at the correct function in the library ... in my case it was looking at the Annotate function of ggplot2, not the NLP package.
source to share
I don't have an exact answer, but it hit the same error using NLP, openNLP, tm, qdap. I worked in reverse restarting R and loaded (the library) one package, ran the code, then loaded another package and executable code, until I came across a "can't force data transfer" error. In my case, I found that qdap prevents the openNLP annotate () function from being called, which is actually using the NLP wrapper.
openNLP version 0.2-3 imports NLP (≥ 0.1-2), openNLPdata (≥ 1.5.3-1), and rJava (≥ 0.6-3). Since you have loaded NLP explicitly, it might be a case where two instances of NLP running in memory interfere with each other. Try to just download openNLP and run the code
source to share