【问题标题】:Getting antonyms using the R Wordnet package使用 R Wordnet 包获取反义词
【发布时间】:2013-10-14 12:18:53
【问题描述】:

我正在尝试使用 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. 

我的目标是有一个函数,我可以将单词向量应用到其中,以便将它们的反义词作为输出,就像包提供的同义词函数一样。

感谢您的任何想法:)

编辑: 我上线了: 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)。

【问题讨论】:

    标签: r wordnet


    【解决方案1】:

    你的问题是 great 没有直接的反义词。如果您look up great in WordNet Search,您会看到所有反义词都是间接通过其他词。您必须首先检查 similar to 关系,并在那里查找反义词。相反,happy has a direct antonym反对)。

    您可能希望使用 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))
    }
    

    【讨论】:

    • 非常感谢!如果我进一步完善该功能,我会发布,但这肯定是一个很好的开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多