NLTK 仅搜索名词同义词集

wat*_*sit 1 python nltk wordnet

这是我编写的一个函数,用于检查名词列表中是否存在上义词和下义词。

def check_hyper_hypo(wordlist):
    returnlist=[]
    for word in wordlist: #by definition a base word has a word above and below heirachy
        x = wn.synsets(word)
        for syn in x:    
            if not(((len(syn.hypernyms()))==0)or((len(syn.hyponyms()))==0)):
                returnlist.append(word)
                break
    return returnlist
Run Code Online (Sandbox Code Playgroud)

如何仅检查作为名词的同义词集的上位词/下位词的长度?例如

for syn in x:
    if ".n." in syn:
        #rest of code
Run Code Online (Sandbox Code Playgroud)

mel*_*des 5

你也可以尝试

wordnet.synsets(word, pos='n')
Run Code Online (Sandbox Code Playgroud)

它只会返回名词,如果你想要动词,你应该使用pos='v'