gpt4 book ai didi

R: annotate() 在 R 中给出错误

转载 作者:行者123 更新时间:2023-12-01 12:39:34 26 4
gpt4 key购买 nike

我是 R 的新手。我必须在我的代码中使用 POSTagger。我正在将 openNLP 与 R 一起使用。在尝试以下示例代码时(在 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。非常感谢任何帮助。提前致谢。

最佳答案

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

关于R: annotate() 在 R 中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26257725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com