Are nltk.corpus.wordnet and nltk.corpus.reader.wordnet different?

44 views Asked by At

I'm looking at a G4G page about wordnet, and see from nltk.corpus import wordnet. So I'm trying things out, but want to look at the documentation and see the methods etc. available, but can't seem to find nltk.corpus.wordnet, only nltk.corpus.reader.wordnet.

Then, I tried:

from nltk.corpus import wordnet as WD1
from nltk.corpus.reader import wordnet as WD2

print(WD1 == WD2)
print(WD1 is WD2)

and got:

False
False

Could someone help me navigate nltk/wordnet's structure?

1

There are 1 answers

0
حمزة نبيل On

WD1 is instance of nltk.corpus.util.LazyCorpusLoader that transform itself into WD2.WordNetCorpusReader after the first time it accessed :

from nltk.corpus import wordnet as WD1
from nltk.corpus.reader import wordnet as WD2


# download wordnet
#import nltk
#nltk.download('wordnet')

print("Before : ", type(WD1)) # LazyCorpusLoader
synsets = WD1.synsets('example')
print("After : ", type(WD1))

print(isinstance(WD1, WD2.WordNetCorpusReader))

output:

Before :  <class 'nltk.corpus.util.LazyCorpusLoader'>
After :  <class 'nltk.corpus.reader.wordnet.WordNetCorpusReader'>
True

The nltk.corpus.reader.wordnet is an interface for WordNet that contain readers and related tools for WordNet.

When import wordnet from nltk.corpus it automatically create instance of a LazyCorpusLoader then it transform itself to WordNetCorpusReader reader instances that can be used to access the WordNet.

You can find the documentation for nltk.corpus.wordnet in Sample usage for wordnet