用wordnet对文档语料库进行翻译时的R错误



我正试图用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) ... 

怎么了?

因此,这并没有解决您对wordnet的使用问题,但确实提供了一个可能对您有效的旅鼠化选项(更好的是,IMO.…)。这使用了西北大学开发的MorphAdorner API。您可以在这里找到详细的文档。在下面的代码中,我使用他们的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约定。

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

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

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

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

第三:看起来getTermFilter(...)需要单个单词(或短语)。例如,如果您将"This is a phrase"传递给getTermFilter(...),它将在字典中查找"This is a phrases"。它当然找不到,所以getIndexTerms(...)返回NULLgetLemma(...)失败。。。因此,您必须首先将PlainTextDocument的文本解析为单词。

最后,我不确定删除标点符号是个好主意。例如,"it’s"将被转换为"its",但这些词是不同的,有不同的含义,它们的旅名也不同。

将所有这些汇总起来:

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" 

正如您所看到的,输出仍然是胡言乱语。"This"被称为"蓟"等等。可能是我的字典配置不正确,所以你可能运气更好。如果您出于某种原因致力于wordnet,我建议您联系包作者。

最新更新