2014-10-08 28 views
2

我是R新手。我必須在我的代碼中使用POSTagger。我使用openNLP與R.在嘗試下面的示例代碼(在Sample.R文件):R:annotate()在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點控制檯(使用源(「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 

以下是回溯()命令的輸出:

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。任何幫助將不勝感激。提前致謝。

+0

這是因爲'qdap'在默認情況下加載了'ggplot2',它也有一個名爲'annotate'的函數。首先加載'qdap',一切都會好的。這在qdap的開發版本中已經改變了:https://github.com/trinker/qdap/issues/199你可能想要[安裝這個版本。](https://github.com/trinker/qdap#安裝) – 2015-02-05 17:12:04

+1

這確實是功能覆蓋問題。我使用NLP :: annotate()解決了錯誤,而不是單獨使用註釋。它工作了,謝謝! – Shruti 2015-05-17 17:25:37

回答

0

我沒有一個確切的答案,但遭受用NLP,openNLP,TM,qdap同樣的錯誤。我反向重新啓動R並加載(庫)一個程序包,運行代碼,然後加載另一個程序包並運行代碼,直到遇到「無法強制處理數據框」錯誤。我發現,在我的情況下,qdap會干擾openNLP annotate()函數調用 - 實際上它使用的是NLP包裝器。

openNLP版本0.2-3進口NLP(≥0.1-2),openNLPdata(≥1.5.3-1),和rJava(≥0.6-3)。由於您明確加載了NLP,因此可能會出現內存中運行的兩個NLP實例互相干擾的情況。儘量只加載openNLP和運行代碼

3

我有同樣的問題,我通過去除固定它/拆卸GGPLOT2包。在ggplot2中有一個稱爲Annotate的函數,它在兩個包中都是相同的名稱。我建議你確保它正在查看庫中的正確函數......在我的情況下,它正在查看ggplot2的註釋函數,而不是NLP包。

0

多個包具有相同的名稱。如果您明確告訴R使用哪個軟件包,它可能會解決問題。例如,代替Arrange(...),請嘗試使用openNLP::Arrange(...)