Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用wordnet建立文档语料库的引理错误_R_Wordnet_Lemmatization - Fatal编程技术网

用wordnet建立文档语料库的引理错误

用wordnet建立文档语料库的引理错误,r,wordnet,lemmatization,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(

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

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

您使用
wordnet
软件包的方式有几个问题,如下所述,但底线是,即使解决了这些问题,我也无法让
wordnet
产生乱七八糟的东西

首先:你不能只在R中安装
wordnet
软件包,你必须在你的计算机上安装wordnet,或者至少下载字典。然后,在使用该软件包之前,需要运行
initDict(“wordnet字典路径”)

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

第三:它看起来像是
getTermFilter(…)
需要单个单词(或短语)。例如,如果将“这是一个短语”传递给
getTermFilter(…)
它将在字典中查找“这是一个短语”。它当然找不到它,因此
getIndexTerms(…)
返回
NULL
并且
getLemma(…)
失败。。。因此,您必须首先将
明文文档的文本
解析为单词

最后,我不确定删除标点符号是否是个好主意。例如,“it’s”将被转换为“it”,但这些是具有不同含义的不同单词,它们的语法化方式也不同

把所有这些加起来:

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" 
library(tm)

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

您使用
wordnet
软件包的方式有几个问题,如下所述,但底线是,即使解决了这些问题,我也无法让
wordnet
产生乱七八糟的东西

首先:你不能只在R中安装
wordnet
软件包,你必须在你的计算机上安装wordnet,或者至少下载字典。然后,在使用该软件包之前,需要运行
initDict(“wordnet字典路径”)

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

第三:它看起来像是
getTermFilter(…)
需要单个单词(或短语)。例如,如果将“这是一个短语”传递给
getTermFilter(…)
它将在字典中查找“这是一个短语”。它当然找不到它,因此
getIndexTerms(…)
返回
NULL
并且
getLemma(…)
失败。。。因此,您必须首先将
明文文档的文本
解析为单词

最后,我不确定删除标点符号是否是个好主意。例如,“it’s”将被转换为“it”,但这些是具有不同含义的不同单词,它们的语法化方式也不同

把所有这些加起来:

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" 
library(tm)

vector.documents因此,这并没有解决您对
wordnet
的使用问题,而是提供了一个可能适合您的柠檬化选项(我认为更好)。这使用了西北大学开发的MorphanderAPI。您可以找到详细的文档。在下面的代码中,我使用了他们的

#MorphAdorner(西北大学)web服务

装饰,因此这并没有解决您对
wordnet
的使用问题,而是提供了一个可能适合您的柠檬化选项(我认为更好)。这使用了西北大学开发的MorphanderAPI。您可以找到详细的文档。在下面的代码中,我使用了他们的

#MorphAdorner(西北大学)web服务

修饰什么是
getLemma
?在上面的代码中看不到它它是包
wordnet
@ntrax的一部分-我认为
getTermFilter(…,x)
需要一个字符参数。您正在传递一个
TextDocument
对象。尝试
getTermFilter(“ContainsFilter”,as.character(x),TRUE)
。当我尝试这个(在一个简单的文档上-您没有提供任何数据!!!)时,这个函数可以工作。什么是
getLemma
?在上面的代码中看不到它它是包
wordnet
@ntrax的一部分-我认为
getTermFilter(…,x)
需要一个字符参数。您正在传递一个
TextDocument
对象。尝试
getTermFilter(“ContainsFilter”,as.character(x),TRUE)
。当我尝试这个(在一个简单的文档上-你没有提供任何数据!!!)时,这个函数可以工作。首先,非常感谢大家的帮助!Morphander web服务似乎非常有趣!谢谢你的建议!首先,非常感谢大家的帮助!Morphander web服务似乎非常有趣!谢谢你的建议!
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" 
# 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     .        .                .       .             .
# ...