Coverage for nltk.corpus.util : 81%
![](keybd_closed.png)
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Natural Language Toolkit: Corpus Reader Utility Functions # # Copyright (C) 2001-2012 NLTK Project # Author: Edward Loper <edloper@gradient.cis.upenn.edu> # URL: <http://www.nltk.org/> # For license information, see LICENSE.TXT
###################################################################### #{ Lazy Corpus Loader ######################################################################
""" A proxy object which is used to stand in for a corpus object before the corpus is loaded. This allows NLTK to create an object for each corpus, but defer the costs associated with loading those corpora until the first time that they're actually accessed.
The first time this object is accessed in any way, it will load the corresponding corpus, and transform itself into that corpus (by modifying its own ``__class__`` and ``__dict__`` attributes).
If the corpus can not be found, then accessing this object will raise an exception, displaying installation instructions for the NLTK data package. Once they've properly installed the data package (or modified ``nltk.data.path`` to point to its location), they can then use the corpus object without restarting python. """
# Find the corpus root directory. try: root = nltk.data.find('corpora/%s' % zip_name) except LookupError: raise root = nltk.data.find('corpora/%s' % self.__name) else:
# Load the corpus.
# This is where the magic happens! Transform ourselves into # the corpus by modifying our own __dict__ and __class__ to # match that of the corpus.
# This looks circular, but its not, since __load() changes our # __class__ to something new:
return '<%s in %r (not loaded yet)>' % ( self.__reader_cls.__name__, '.../corpora/'+self.__name) |