watisit
watisit

Reputation: 303

NLTK only search noun synsets

Here is a function I'm writing to check for the existence of hyper and hyponyms from a list of nouns.

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

How do i check the length of hyper/hyponyms only for the synsets which are nouns? E.g.

for syn in x:
    if ".n." in syn:
        #rest of code

Upvotes: 1

Views: 2941

Answers (3)

Ahalya L
Ahalya L

Reputation: 17

another approach using wordnet and list comprehension

print ([len(wn.morphy(tag,wn.NOUN)) for tag in tags])

Upvotes: -1

melqkiades
melqkiades

Reputation: 473

You could also try

wordnet.synsets(word, pos='n')

It will only return nouns, if you want verbs you should use pos='v'

Upvotes: 5

Jared
Jared

Reputation: 26397

Simply,

for syn in x:
    if syn.pos == 'n':
        #rest of code

Upvotes: 1

Related Questions