R:annotate()在R中出错

3

我是R语言的新手。我的代码中需要使用POSTagger,我正在使用R的openNLP包。在尝试以下示例代码(位于Sample.R文件中)时遇到了问题:

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控制台运行这段代码(使用source("Sample.R")),我得到了以下错误:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
cannot coerce class "c("Simple_POS_Tag_Annotator", "Annotator")" to a data.frame

以下是traceback()命令的输出结果:
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")

可能出了什么问题?我在Windows 7上使用Rx64 3.1.1。非常感谢您的帮助。谢谢。

这是因为qdap默认加载了ggplot2,而ggplot2也有一个名为annotate的函数。先加载qdap,一切都应该没问题。在qdap的开发版本中已经进行了更改:https://github.com/trinker/qdap/issues/199 您可能想要安装这个版本。 - Tyler Rinker
1
这确实是函数覆盖的问题。我使用 NLP::annotate() 而不是单独的 annotate() 解决了这个错误。它有效了。谢谢! - Shruti
3个回答

4

我遇到了同样的问题,通过删除/卸载ggplot2包来解决它。ggplot2中有一个名为Annotate的函数,在两个软件包中都是相同的名称。我建议你确保它在库中查找正确的函数...在我的情况下,它正在查找ggplot2的Annotate函数而不是NLP包的函数。


0

我没有确切的答案,但在使用NLP、openNLP、tm和qdap时遇到了同样的错误。我倒退工作,重新启动R并加载(library)一个包,在运行代码后加载另一个包并运行代码,直到我遇到“无法强制转换为数据框”的错误。我发现,在我的情况下,qdap干扰了openNLP annotate()函数调用——实际上是使用NLP包装器。

openNLP版本0.2-3导入NLP(≥ 0.1-2)、openNLPdata(≥ 1.5.3-1)和rJava(≥ 0.6-3)。因为您明确加载了NLP,可能是两个NLP实例在内存中相互干扰的情况。尝试只加载openNLP并运行您的代码。


0

多个包名称相同。如果您明确告诉 R 使用哪个包,它可能会解决问题。例如,不要使用 Arrange(...),尝试使用 openNLP::Arrange(...)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接