首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R: annotate()在R中显示错误

R: annotate()在R中显示错误
EN

Stack Overflow用户
提问于 2014-10-08 21:08:16
回答 3查看 3.3K关注 0票数 3

我是R的新手,我必须在我的代码中使用POSTagger。我将openNLP与R一起使用,同时尝试以下示例代码(在Sample.R文件中):

代码语言:javascript
复制
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]

并从R控制台运行此代码(使用源代码(“Sample.R”)),我得到以下错误:

代码语言:javascript
复制
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
cannot coerce class "c("Simple_POS_Tag_Annotator", "Annotator")" to a data.frame

以下是traceback()命令的输出:

代码语言:javascript
复制
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")

有什么可能是错的?我在Windows7上使用的是Rx64 3.1.1,如果有任何帮助,我将不胜感激。提前谢谢。

EN

回答 3

Stack Overflow用户

发布于 2015-08-11 01:48:07

我也有同样的问题,我通过移除/分离ggplot2包修复了这个问题。在ggplot2中有一个名为Annotate的函数,它在两个包中具有相同的名称。我建议您确保它正在查看库中的正确函数……在我的例子中,它查看的是ggplot2的Annotate函数,而不是NLP包。

票数 4
EN

Stack Overflow用户

发布于 2015-02-05 21:49:15

我没有确切的答案,但在使用NLP,openNLP,tm,qdap时遇到了同样的错误。我向后工作,重新启动R并加载(库)一个包,运行代码,然后加载另一个包并运行代码,直到我遇到了"cannot coerce to a dataframe“错误。我发现,在我的例子中,qdap干扰了NLP annotate()函数调用--它实际上使用了openNLP包装器。

openNLP版本0.2-3导入NLP (≥0.1-2)、openNLPdata (≥1.5.3-1)和rJava (≥0.6-3)。因为您显式加载了NLP,所以在内存中运行的两个NLP实例可能会相互干扰。试着加载openNLP并运行你的代码

票数 0
EN

Stack Overflow用户

发布于 2016-07-22 06:33:02

多个包具有相同的名称。如果你明确地告诉R使用哪个包,它可能会解决这个问题。例如,尝试使用openNLP::Arrange(...)而不是Arrange(...)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26257725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档