1

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

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());
} 

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

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

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

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 相关......并且该实现取得了成功。

其他信息:

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

4

1 回答 1

1

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

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());
}
于 2020-12-23T08:59:46.353 回答