Coverage for nltk.corpus.reader.api : 77%
![](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: API for Corpus Readers # # Copyright (C) 2001-2012 NLTK Project # Author: Steven Bird <sb@ldc.upenn.edu> # Edward Loper <edloper@gradient.cis.upenn.edu> # URL: <http://www.nltk.org/> # For license information, see LICENSE.TXT
API for corpus readers. """
""" 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 be 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. """ """ :type root: PathPointer or str :param root: A path pointer identifying the root directory for this corpus. If a string is specified, then it will be converted to a ``PathPointer`` automatically. :param 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. :param 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. :param tag_mapping_function: A function for normalizing or simplifying the POS tags returned by the tagged_words() or tagged_sents() methods. """ # Convert the root to a path pointer, if necessary. root = ZipFilePathPointer(zipfile, zipentry) else: raise TypeError('CorpusReader: expected a string or a PathPointer')
# If `fileids` is a regexp, then expand it.
"""A list of the relative paths for the fileids that make up this corpus."""
"""The root directory for this corpus."""
# If encoding was specified as a list of regexps, then convert # it to a dictionary.
"""The default unicode encoding for the fileids that make up this corpus. If ``encoding`` is None, then the file contents are processed using byte strings (str)."""
if isinstance(self._root, ZipFilePathPointer): path = '%s/%s' % (self._root.zipfile.filename, self._root.entry) else: path = '%s' % self._root.path return '<%s in %r>' % (self.__class__.__name__, path)
""" Return the contents of the corpus README file, if it exists. """
return self.open("README").read()
""" Return a list of file identifiers for the fileids that make up this corpus. """
""" Return the absolute path for the given file.
:type file: str :param file: The file identifier for the file whose path should be returned. :rtype: PathPointer """ return self._root.join(fileid)
include_fileid=False): """ Return a list of the absolute paths for all fileids in this corpus; or for the given list of fileids, if specified.
:type fileids: None or str or list :param fileids: 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.
:param include_encoding: If true, then return a list of ``(path_pointer, encoding)`` tuples.
:rtype: list(PathPointer) """
return zip(paths, fileids) else:
""" 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.
:param file: The file identifier of the file to read. """ stream = SourcedStringStream(stream, file)
""" 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. """ else:
The directory where this corpus is stored.
:type: PathPointer""")
###################################################################### #{ Corpora containing categorized items ######################################################################
""" 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. """
""" 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. """
self._map = kwargs['cat_map'] del kwargs['cat_map'] self._delimiter = kwargs['cat_delimiter'] del kwargs['cat_delimiter'] else: raise ValueError('Expected keyword argument cat_pattern or ' 'cat_map or cat_file.')
'cat_file' in kwargs): raise ValueError('Specify exactly one of: cat_pattern, ' 'cat_map, cat_file.')
for file_id in self._fileids: category = re.match(self._pattern, file_id).group(1) self._add(file_id, category)
for (file_id, categories) in self._map.items(): for category in categories: self._add(file_id, category)
'not found' % (self._file, file_id))
""" Return a list of the categories that are defined for this corpus, or for the file(s) if it is given. """ if self._f2c is None: self._init() if fileids is None: return sorted(self._c2f) if isinstance(fileids, compat.string_types): fileids = [fileids] return sorted(set.union(*[self._f2c[d] for d in fileids]))
""" Return a list of file identifiers for the files that make up this corpus, or that make up the given category(s) if specified. """ else: else: if self._f2c is None: self._init() return sorted(set.union(*[self._c2f[c] for c in categories]))
###################################################################### #{ Treebank readers ######################################################################
#[xx] is it worth it to factor this out? """ 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. """ raise NotImplementedError() raise NotImplementedError() raise NotImplementedError() raise NotImplementedError()
if fileids is None: fileids = self._fileids elif isinstance(fileids, compat.string_types): fileids = [fileids] return concat([self.open(f).read() for f in fileids])
for fileid, enc in self.abspaths(fileids, True)])
for fileid, enc in self.abspaths(fileids, True)])
for fileid, enc in self.abspaths(fileids, True)])
for fileid, enc in self.abspaths(fileids, True)])
self._read_word_block, encoding=enc) for fileid, enc in self.abspaths(fileids, True)])
#------------------------------------------------------------ #{ Block Readers
for t in self._read_block(stream)]))
#} End of Block Readers #------------------------------------------------------------
|