引理中的R错误带有wordnet的文档语料库

时间:2014-10-04 18:51:40

标签: r wordnet lemmatization

我正在尝试使用wordnet库对R中的文档语料进行推理。这是代码:

corpus.documents <- Corpus(VectorSource(vector.documents))
corpus.documents <- tm_map(corpus.documents removePunctuation)

library(wordnet)
lapply(corpus.documents,function(x){
  x.filter <- getTermFilter("ContainsFilter", x, TRUE)
  terms <- getIndexTerms("NOUN", 1, x.filter)
  sapply(terms, getLemma)
})

但在运行时。我有这个错误:

Errore in .jnew(paste("com.nexagis.jawbone.filter", type, sep = "."), word,  :
java.lang.NoSuchMethodError: <init> 

那些是堆栈调用:

5 stop(structure(list(message = "java.lang.NoSuchMethodError: <init>", 
call = .jnew(paste("com.nexagis.jawbone.filter", type, sep = "."), 
    word, ignoreCase), jobj = <S4 object of class structure("jobjRef", package 
="rJava")>), .Names = c("message", 
"call", "jobj"), class = c("NoSuchMethodError", "IncompatibleClassChangeError",  ... 
4 .jnew(paste("com.nexagis.jawbone.filter", type, sep = "."), word, 
ignoreCase) 
3 getTermFilter("ContainsFilter", x, TRUE) 
2 FUN(X[[1L]], ...) 
1 lapply(corpus.documents, function(x) {
x.filter <- getTermFilter("ContainsFilter", x, TRUE)
terms <- getIndexTerms("NOUN", 1, x.filter)
sapply(terms, getLemma) ... 

出了什么问题?

2 个答案:

答案 0 :(得分:2)

这回答了你的问题,但并没有真正解决你的问题。上面有另一个解决方案(不同答案)试图提供解决方案。

使用wordnet软件包的方式有几个问题,如下所述,但最重要的是,即使解决了这些问题之后,我也无法让wordnet产生任何除了胡言乱语之外的东西。

首先:您不能在R中安装wordnet软件包,您必须在计算机上安装Wordnet,或者至少下载字典。然后,在使用该包之前,您需要运行initDict("path to wordnet dictionaries")

第二:看起来getTermFilter(...)期望x的字符参数。您设置的方式是传递PlainTextDocument类型的对象。因此,您需要使用as.character(x)将其转换为包含的文本,否则您的问题会出现java错误。

第三:看起来getTermFilter(...)期望单个单词(或短语)。例如,如果你通过&#34;这是一个短语&#34;到getTermFilter(...)它会查找&#34;这是一个短语&#34;在字典里。它当然不会找到它,因此getIndexTerms(...)会返回NULLgetLemma(...)会失败...所以你必须首先将PlainTextDocument的文本解析为单词。

最后,我不确定删除标点符号是个好主意。例如&#34;它&#34;&#34;将转换为&#34;其&#34;但这些是具有不同含义的不同词语,并且它们的推理不同。

滚动所有这些:

library(tm)
vector.documents <- c("This is a line of text.", "This is another one.")
corpus.documents <- Corpus(VectorSource(vector.documents))
corpus.documents <- tm_map(corpus.documents, removePunctuation)

library(wordnet)
initDict("C:/Program Files (x86)/WordNet/2.1/dict")
lapply(corpus.documents,function(x){
  sapply(unlist(strsplit(as.character(x),"[[:space:]]+")), function(word) {
    x.filter <- getTermFilter("StartsWithFilter", word, TRUE)
    terms    <- getIndexTerms("NOUN",1,x.filter)
    if(!is.null(terms)) sapply(terms,getLemma)
  })
})
# [[1]]
#                 This                   is                    a                 line                   of                 text 
#            "thistle"              "isaac"                  "a"               "line" "off-axis reflector"               "text" 

正如您所看到的,输出仍然是胡言乱语。 &#34;这&#34;被解释为&#34;蓟?#34;等等。可能是我的字典配置不正确,所以你可能会有更好的运气。如果您致力于wordnet,出于某种原因,我建议您联系包裹作者。

答案 1 :(得分:2)

所以这并没有解决你对wordnet的使用问题,但确实提供了一个可能对你有用的词形变换选项(并且更好,IMO ......)。这使用了西北大学开发的MorphAdorner API。您可以找到详细的文档here。在下面的代码中,我使用的是Adorner for Plain Text API

# MorphAdorner (Northwestern University) web service
adorn <- function(text) {
  require(httr)
  require(XML)
  url <- "http://devadorner.northwestern.edu/maserver/partofspeechtagger"
  response <- GET(url,query=list(text=text, media="xml", 
                                 xmlOutputType="outputPlainXML",
                                 corpusConfig="ncf", # Nineteenth Century Fiction
                                 includeInputText="false", outputReg="true"))
  doc <- content(response,type="text/xml")
  words <- doc["//adornedWord"]
  xmlToDataFrame(doc,nodes=words)
}

library(tm)
vector.documents <- c("Here is some text.", 
                      "This might possibly be some additional text, but then again, maybe not...",
                      "This is an abstruse grammatical construction having as it's sole intention the demonstration of MorhAdorner's capability.")
corpus.documents <- Corpus(VectorSource(vector.documents))
lapply(corpus.documents,function(x) adorn(as.character(x)))
# [[1]]
#   token spelling standardSpelling lemmata partsOfSpeech
# 1  Here     Here             Here    here            av
# 2    is       is               is      be           vbz
# 3  some     some             some    some             d
# 4  text     text             text    text            n1
# 5     .        .                .       .             .
# ...

我只是展示了第一个“文档”的词形还原。 partsOfSpeech遵循NUPOS惯例。