nltk.corpus.reader.api module

API for corpus readers.

class nltk.corpus.reader.api.CorpusReader[source]

Bases: object

A base class for “corpus reader” classes, each of which can be used to read a specific corpus format. Each individual corpus reader instance is used to read a specific corpus, consisting of one or more files under a common root directory. Each file is identified by its file identifier, which is the relative path to the file from the root directory.

A separate subclass is defined for each corpus format. These subclasses define one or more methods that provide ‘views’ on the corpus contents, such as words() (for a list of words) and parsed_sents() (for a list of parsed sentences). Called with no arguments, these methods will return the contents of the entire corpus. For most corpora, these methods define one or more selection arguments, such as fileids or categories, which can be used to select which portion of the corpus should be returned.

__init__(root, fileids, encoding='utf8', tagset=None)[source]
Parameters
  • root (PathPointer or str) – A path pointer identifying the root directory for this corpus. If a string is specified, then it will be converted to a PathPointer automatically.

  • fileids – A list of the files that make up this corpus. This list can either be specified explicitly, as a list of strings; or implicitly, as a regular expression over file paths. The absolute path for each file will be constructed by joining the reader’s root to each file name.

  • encoding

    The default unicode encoding for the files that make up the corpus. The value of encoding can be any of the following:

    • A string: encoding is the encoding name for all files.

    • A dictionary: encoding[file_id] is the encoding name for the file whose identifier is file_id. If file_id is not in encoding, then the file contents will be processed using non-unicode byte strings.

    • A list: encoding should be a list of (regexp, encoding) tuples. The encoding for a file whose identifier is file_id will be the encoding value for the first tuple whose regexp matches the file_id. If no tuple’s regexp matches the file_id, the file contents will be processed using non-unicode byte strings.

    • None: the file contents of all files will be processed using non-unicode byte strings.

  • tagset – The name of the tagset used by this corpus, to be used for normalizing or converting the POS tags returned by the tagged_...() methods.

ensure_loaded()[source]

Load this corpus (if it has not already been loaded). This is used by LazyCorpusLoader as a simple method that can be used to make sure a corpus is loaded – e.g., in case a user wants to do help(some_corpus).

readme()[source]

Return the contents of the corpus README file, if it exists.

license()[source]

Return the contents of the corpus LICENSE file, if it exists.

citation()[source]

Return the contents of the corpus citation.bib file, if it exists.

fileids()[source]

Return a list of file identifiers for the fileids that make up this corpus.

abspath(fileid)[source]

Return the absolute path for the given file.

Parameters

fileid (str) – The file identifier for the file whose path should be returned.

Return type

PathPointer

abspaths(fileids=None, include_encoding=False, include_fileid=False)[source]

Return a list of the absolute paths for all fileids in this corpus; or for the given list of fileids, if specified.

Parameters
  • fileids (None or str or list) – Specifies the set of fileids for which paths should be returned. Can be None, for all fileids; a list of file identifiers, for a specified set of fileids; or a single file identifier, for a single file. Note that the return value is always a list of paths, even if fileids is a single file identifier.

  • include_encoding – If true, then return a list of (path_pointer, encoding) tuples.

Return type

list(PathPointer)

raw(fileids=None)[source]
Parameters

fileids – A list specifying the fileids that should be used.

Returns

the given file(s) as a single string.

Return type

str

open(file)[source]

Return an open stream that can be used to read the given file. If the file’s encoding is not None, then the stream will automatically decode the file’s contents into unicode.

Parameters

file – The file identifier of the file to read.

encoding(file)[source]

Return the unicode encoding for the given corpus file, if known. If the encoding is unknown, or if the given file should be processed using byte strings (str), then return None.

property root

The directory where this corpus is stored.

Type

PathPointer

class nltk.corpus.reader.api.CategorizedCorpusReader[source]

Bases: object

A mixin class used to aid in the implementation of corpus readers for categorized corpora. This class defines the method categories(), which returns a list of the categories for the corpus or for a specified set of fileids; and overrides fileids() to take a categories argument, restricting the set of fileids to be returned.

Subclasses are expected to:

  • Call __init__() to set up the mapping.

  • Override all view methods to accept a categories parameter, which can be used instead of the fileids parameter, to select which fileids should be included in the returned view.

__init__(kwargs)[source]

Initialize this mapping based on keyword arguments, as follows:

  • cat_pattern: A regular expression pattern used to find the category for each file identifier. The pattern will be applied to each file identifier, and the first matching group will be used as the category label for that file.

  • cat_map: A dictionary, mapping from file identifiers to category labels.

  • cat_file: The name of a file that contains the mapping from file identifiers to categories. The argument cat_delimiter can be used to specify a delimiter.

The corresponding argument will be deleted from kwargs. If more than one argument is specified, an exception will be raised.

categories(fileids=None)[source]

Return a list of the categories that are defined for this corpus, or for the file(s) if it is given.

fileids(categories=None)[source]

Return a list of file identifiers for the files that make up this corpus, or that make up the given category(s) if specified.

raw(fileids=None, categories=None)[source]
words(fileids=None, categories=None)[source]
sents(fileids=None, categories=None)[source]
paras(fileids=None, categories=None)[source]
class nltk.corpus.reader.api.SyntaxCorpusReader[source]

Bases: nltk.corpus.reader.api.CorpusReader

An abstract base class for reading corpora consisting of syntactically parsed text. Subclasses should define:

  • __init__, which specifies the location of the corpus and a method for detecting the sentence blocks in corpus files.

  • _read_block, which reads a block from the input stream.

  • _word, which takes a block and returns a list of list of words.

  • _tag, which takes a block and returns a list of list of tagged words.

  • _parse, which takes a block and returns a list of parsed sentences.

parsed_sents(fileids=None)[source]
tagged_sents(fileids=None, tagset=None)[source]
sents(fileids=None)[source]
tagged_words(fileids=None, tagset=None)[source]
words(fileids=None)[source]