在java中获取单词的反义词 - Wordnet JWI [英] Get antonyms for a word in java - Wordnet JWI

查看:64
本文介绍了在java中获取单词的反义词 - Wordnet JWI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在 Java 中使用 wordnet 查找单词的反义词很感兴趣.我目前正在使用这种方法来查找反义词,但我还没有找到任何带有反义词的单词.反义词在 Wordnet 中不常见吗?还是这个实现有缺陷?

I am interested in finding antonyms for a word using wordnet in Java. I am currently using this method to find antonyms but I have yet to find any words which have antonyms. Are antonyms not common in Wordnet? Or is this implementation flawed?

public List<String> getAntonyms(String baseWord) {
    List<String> synonymList = new ArrayList<>();
    IIndexWord[] baseWordPOS = getAllPOSForBaseWord(baseWord);
    for (IIndexWord iIndexWord : baseWordPOS) {
        if (iIndexWord == null) {
            continue;
        }
        for (IWordID wordID : iIndexWord.getWordIDs()) {
            IWord word = dict.getWord(wordID);
            ISynset synset = word.getSynset();
            Map<IPointer, List<ISynsetID>> relatedMap = synset.getRelatedMap();
            for (Map.Entry<IPointer, List<ISynsetID>> entry : relatedMap.entrySet()) {
                IPointer iPointer = entry.getKey();
                if (iPointer.equals(Pointer.ANTONYM)) {
                    List<ISynsetID> iSunsetIDS = entry.getValue();
                    for (ISynsetID iSynsetID : iSunsetIDS) {
                        ISynset synset1 = dict.getSynset(iSynsetID);
                        for (IWord w : synset1.getWords()) {
                            synonymList.add(ConvertToPolyFormat(w.getLemma()));
                        }
                    }
                }
            }
        }
    }
    return synonymList.stream()
            .distinct()
            .filter(s -> !s.equals(baseWord))
            .collect(Collectors.toList());
} 

我正在用这些词进行测试:

I am testing with these words:

String[] testWords = {"able", "unable", "adsorbent", "apple", "tart", "computer", "cat", "great", "allowable"};

但它们都返回空列表.我正在检查单词的所有词性 (POS):

but they all return empty lists. I am checking for all parts of speech (POS) for the word:

private static final POS[] POS_ARRAY = {POS.ADJECTIVE, POS.ADVERB, POS.NOUN, POS.VERB};

private IIndexWord[] getAllPOSForBaseWord(String baseWord) {
    IIndexWord[] returnValue = new IIndexWord[4];
    for (int i = 0; i < returnValue.length; i++) {
        returnValue[i] = dict.getIndexWord(baseWord, POS_ARRAY[i]);
    }
    return returnValue;
}

我使用相同的方法来查找同义词,但检查了指针类型是否与/hypernym/etc...相关,并且在该实现中取得了成功.

I used this same approach to find synonyms but checked if the pointer type is related to/hypernym/etc... and had success with that implementation.

其他信息:

dict 变量是来自 (edu.mit.jwi.IDictionary) 的 IDictionary 对象

The dict variable is an IDictionary object from (edu.mit.jwi.IDictionary)

推荐答案

我想你弄错了 relatedMap.我已经像这样更改了您的代码并成功:

I think you got wrong relatedMap. I have changed your code like this and successfully:

public List<String> getAntonyms(String baseWord) {
    List<String> synonymList = new ArrayList<>();
    IIndexWord[] baseWordPOS = getAllPOSForBaseWord(baseWord);
    for (IIndexWord iIndexWord : baseWordPOS) {
        if (iIndexWord == null) {
            continue;
        }
        for (IWordID wordID : iIndexWord.getWordIDs()) {
            IWord word = dict.getWord(wordID);

            Map<IPointer, List<IWordID>> relatedMap = word.getRelatedMap();
            for (Map.Entry<IPointer, List<IWordID>> entry : relatedMap.entrySet()) {
                IPointer iPointer = entry.getKey();
                if (iPointer.equals(Pointer.ANTONYM)) {
                    List<IWordID> iWordIDs = entry.getValue();
                    for (IWordID iWordID : iWordIDs) {
                        IWord iWord = dict.getWord(iWordID);
                        ISynset synset = iWord.getSynset();
                        for (IWord w : synset.getWords()) {
                            synonymList.add(ConvertToPolyFormat(w.getLemma()));
                        }
                    }
                }
            }
        }
    }
    return synonymList.stream()
            .distinct()
            .filter(s -> !s.equals(baseWord))
            .collect(Collectors.toList());
}

这篇关于在java中获取单词的反义词 - Wordnet JWI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆