我正在尝试使用wordnet包获取单词的反义词.这适用于某些单词,同时返回错误,我不会真正得到其他人.该函数基本上只是封装在函数中的包文档中的用法示例.
# The function:
antonyms <- function(x){
filter <- getTermFilter("ExactMatchFilter", x, TRUE)
terms <- getIndexTerms("ADJECTIVE", 5, filter)
synsets <- getSynsets(terms[[1]])
related <- getRelatedSynsets(synsets[[1]], "!")
sapply(related, getWord)
}
# Some words work while others return an error:
> antonyms("happy")
[1] "unhappy"
> antonyms("great")
Error in .jcall(l, "Ljava/util/Iterator;", "iterator") :
RcallMethod: invalid object parameter
# The Error is caused by the "related" step.
Run Code Online (Sandbox Code Playgroud)
我的目标是拥有一个函数,我可以提供单词向量,以便将它们的反义词作为输出,就像包提供的同义词函数一样.
谢谢你的任何想法:)
编辑:我在:OSX 10.8.5,wordnet包(在R中)wordnet_0.1-9和wordnet 3.0_3(系统范围到macports),rJava 0.9-4,R版本3.0.1(2013-05-16 ).
你的问题是伟大没有直接的反义词。如果您在 WordNet Search 中查找得很好,您会发现所有反义词都是通过其他单词间接表示的。你必须首先检查相似的关系,并在那里查找反义词。相反,“快乐”有一个直接反义词(与“相反”)。
您可能想使用以下命令捕获此特定错误tryCatch:
antonyms <- function(x){
filter <- getTermFilter("ExactMatchFilter", x, TRUE)
terms <- getIndexTerms("ADJECTIVE", 5, filter)
synsets <- getSynsets(terms[[1]])
related <- tryCatch(
getRelatedSynsets(synsets[[1]], "!"),
error = function(condition) {
message("No direct antonym found")
if (condition$message == "RcallMethod: invalid object parameter")
message("No direct antonym found")
else
stop(condition)
return(NULL)
}
)
if (is.null(related))
return(NULL)
return(sapply(related, getWord))
}
Run Code Online (Sandbox Code Playgroud)