nltk.corpus package

Submodules

nltk.corpus.europarl_raw module

nltk.corpus.util module

class nltk.corpus.util.LazyCorpusLoader(name, reader_cls, *args, **kwargs)[source]

Bases: builtins.object

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.

unicode_repr()

Module contents

NLTK corpus readers. The modules in this package provide functions that can be used to read corpus files in a variety of formats. These functions can be used to read both the corpus files that are distributed in the NLTK corpus package, and corpus files that are part of external corpora.

Available Corpora

Please see http://nltk.googlecode.com/svn/trunk/nltk_data/index.xml for a complete list. Install corpora using nltk.download().

Corpus Reader Functions

Each corpus module defines one or more “corpus reader functions”, which can be used to read documents from that corpus. These functions take an argument, item, which is used to indicate which document should be read from the corpus:

  • If item is one of the unique identifiers listed in the corpus module’s items variable, then the corresponding document will be loaded from the NLTK corpus package.
  • If item is a filename, then that file will be read.

Additionally, corpus reader functions can be given lists of item names; in which case, they will return a concatenation of the corresponding documents.

Corpus reader functions are named based on the type of information they return. Some common examples, and their return types, are:

  • words(): list of str
  • sents(): list of (list of str)
  • paras(): list of (list of (list of str))
  • tagged_words(): list of (str,str) tuple
  • tagged_sents(): list of (list of (str,str))
  • tagged_paras(): list of (list of (list of (str,str)))
  • chunked_sents(): list of (Tree w/ (str,str) leaves)
  • parsed_sents(): list of (Tree with str leaves)
  • parsed_paras(): list of (list of (Tree with str leaves))
  • xml(): A single xml ElementTree
  • raw(): unprocessed corpus contents

For example, to read a list of the words in the Brown Corpus, use nltk.corpus.brown.words():

>>> from nltk.corpus import brown
>>> print(", ".join(brown.words()))
The, Fulton, County, Grand, Jury, said, ...
nltk.corpus.demo()[source]
nltk.corpus.teardown_module(module=None)[source]