Coverage for nltk.data : 86%
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: 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
Functions to find and load NLTK resource files, such as corpora, grammars, and saved processing objects. Resource files are identified using URLs, such as ``nltk:corpora/abc/rural.txt`` or ``http://nltk.org/sample/toy.cfg``. The following URL protocols are supported:
- ``file:path``: Specifies the file whose path is *path*. Both relative and absolute paths may be used.
- ``http://host/path``: Specifies the file stored on the web server *host* at path *path*.
- ``nltk:path``: Specifies the file stored in the NLTK data package at *path*. NLTK will search for these files in the directories specified by ``nltk.data.path``.
If no protocol is specified, then the default protocol ``nltk:`` will be used.
This module provides to functions that can be used to access a resource file, given its URL: ``load()`` loads a given resource, and adds it to a resource cache; and ``retrieve()`` copies a given resource to a local file. """
except: from zlib import Z_FINISH as FLUSH
###################################################################### # Search Path ######################################################################
"""A list of directories where the NLTK data package might reside. These directories will be checked in order when looking for a resource in the data package. Note that this allows users to substitute in their own versions of resources, if they have them (e.g., in their home directory under ~/nltk_data)."""
# User-specified locations: os.path.expanduser('~/nltk_data')]
# Common locations on Windows: r'C:\nltk_data', r'D:\nltk_data', r'E:\nltk_data', os.path.join(sys.prefix, 'nltk_data'), os.path.join(sys.prefix, 'lib', 'nltk_data'), os.path.join(os.environ.get('APPDATA', 'C:\\'), 'nltk_data')]
# Common locations on UNIX & OS X: '/usr/share/nltk_data', '/usr/local/share/nltk_data', '/usr/lib/nltk_data', '/usr/local/lib/nltk_data']
###################################################################### # Path Pointers ######################################################################
""" An abstract base class for 'path pointers,' used by NLTK's data package to identify specific paths. Two subclasses exist: ``FileSystemPathPointer`` identifies a file that can be accessed directly via a given absolute path. ``ZipFilePathPointer`` identifies a file contained within a zipfile, that can be accessed by reading that zipfile. """ """ Return a seekable read-only stream that can be used to read the contents of the file identified by this path pointer.
:raise IOError: If the path specified by this pointer does not contain a readable file. """ raise NotImplementedError('abstract base class')
""" Return the size of the file pointed to by this path pointer, in bytes.
:raise IOError: If the path specified by this pointer does not contain a readable file. """ raise NotImplementedError('abstract base class')
""" Return a new path pointer formed by starting at the path identified by this pointer, and then following the relative path given by ``fileid``. The path components of ``fileid`` should be separated by forward slashes, regardless of the underlying file system's path seperator character. """ raise NotImplementedError('abstract base class')
""" A path pointer that identifies a file which can be accessed directly via a given absolute path. ``FileSystemPathPointer`` is a subclass of ``str`` for backwards compatibility purposes -- this allows old code that expected ``nltk.data.find()`` to expect a string to usually work (assuming the resource is not found in a zipfile). It also permits ``open()`` to work on a ``FileSystemPathPointer``.
""" """ Create a new path pointer for the given absolute path.
:raise IOError: If the given path does not exist. """
# There's no need to call str.__init__(), since it's a no-op; # str does all of its setup work in __new__.
def path(self): """The absolute path identified by this path pointer."""
return 'FileSystemPathPointer(%r)' % self._path
""" A ``GzipFile`` subclass that buffers calls to ``read()`` and ``write()``. This allows faster reads and writes of data to and from gzip-compressed files at the cost of using more memory.
The default buffer size is 2MB.
``BufferedGzipFile`` is useful for loading large gzipped pickle objects as well as writing large encoded feature files for classifier training. """
fileobj=None, **kwargs): """ Return a buffered gzip file object.
:param filename: a filesystem path :type filename: str :param mode: a file mode which can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb' :type mode: str :param compresslevel: The compresslevel argument is an integer from 1 to 9 controlling the level of compression; 1 is fastest and produces the least compression, and 9 is slowest and produces the most compression. The default is 9. :type compresslevel: int :param fileobj: a StringIO stream to read from instead of a file. :type fileobj: StringIO :param size: number of bytes to buffer during calls to read() and write() :type size: int :rtype: BufferedGzipFile """ # cStringIO does not support len.
# For some reason calling StringIO.truncate() here will lead to # inconsistent writes so just set _buffer to a new StringIO object.
# Simply write to the buffer and increment the buffer size.
# Write the current buffer to the GzipFile. # Then reset the buffer and write the new data to the buffer.
# GzipFile.close() doesn't actuallly close anything.
self._buffer.flush() GzipFile.flush(self, lib_mode)
else: return GzipFile.read(self, size)
""" :param data: str to write to file or buffer :type data: str :param size: buffer at least size bytes before writing to file :type size: int """ size = self._size self._write_buffer(data) else:
""" A subclass of ``FileSystemPathPointer`` that identifies a gzip-compressed file located at a given absolute path. ``GzipFileSystemPathPointer`` is appropriate for loading large gzip-compressed pickle objects efficiently. """ stream = BufferedGzipFile(self._path, 'rb') if encoding: stream = SeekableUnicodeStreamReader(stream, encoding) return stream
""" A path pointer that identifies a file contained within a zipfile, which can be accessed by reading that zipfile. """ """ Create a new path pointer pointing at the specified entry in the given zipfile.
:raise IOError: If the given zipfile does not exist, or if it does not contain the specified entry. """
# Normalize the entry string:
# Check that the entry exists: # Sometimes directories aren't explicitly listed in # the zip file. So if `entry` is a directory name, # then check if the zipfile contains any files that # are under the given directory. [n for n in zipfile.namelist() if n.startswith(entry)]): pass # zipfile contains a file in that directory. else: # Otherwise, complain. (zipfile.filename, entry))
def zipfile(self): """ The zipfile.ZipFile object used to access the zip file containing the entry identified by this path pointer. """
def entry(self): """ The name of the file within zipfile that this path pointer points to. """
stream = BufferedGzipFile(self._entry, fileobj=stream)
return 'ZipFilePathPointer(%r, %r)' % ( self._zipfile.filename, self._entry)
return '%r/%r' % (self._zipfile.filename, self._entry)
###################################################################### # Access Functions ######################################################################
# Don't use a weak dictionary, because in the common case this # causes a lot more reloading that necessary. """A dictionary used to cache resources so that they won't need to be loaded more than once."""
""" Find the given resource by searching through the directories and zip files in ``nltk.data.path``, and return a corresponding path name. If the given resource is not found, raise a ``LookupError``, whose message gives a pointer to the installation instructions for the NLTK downloader.
Zip File Handling:
- If ``resource_name`` contains a component with a ``.zip`` extension, then it is assumed to be a zipfile; and the remaining path components are used to look inside the zipfile.
- If any element of ``nltk.data.path`` has a ``.zip`` extension, then it is assumed to be a zipfile.
- If a given resource name that does not contain any zipfile component is not found initially, then ``find()`` will make a second attempt to find that resource, by replacing each component *p* in the path with *p.zip/p*. For example, this allows ``find()`` to map the resource name ``corpora/chat80/cities.pl`` to a zip file path pointer to ``corpora/chat80.zip/chat80/cities.pl``.
- When using ``find()`` to locate a directory contained in a zipfile, the resource name must end with the forward slash character. Otherwise, ``find()`` will not locate the directory.
:type resource_name: str :param resource_name: The name of the resource to search for. Resource names are posix-style relative path names, such as ``corpora/brown``. In particular, directory names should always be separated by the forward slash character, which will be automatically converted to a platform-appropriate path separator. :rtype: str """ # Check if the resource name includes a zipfile name
# Check each item in our path
# Is the path item a zipfile? try: return ZipFilePathPointer(path_item, resource_name) except IOError: continue # resource not in zipfile
# Is the path item a directory? return GzipFileSystemPathPointer(p) else: else:
# Fallback: if the path doesn't include a zip file, then try # again, assuming that one of the path components is inside a # zipfile of the same name.
# Display a friendly error message if the resource wasn't found: 'Resource %r not found. Please use the NLTK Downloader to ' 'obtain the resource: >>> nltk.download()' % (resource_name,), initial_indent=' ', subsequent_indent=' ', width=66)
""" Copy the given resource to a local file. If no filename is specified, then use the URL's filename. If there is already a file named ``filename``, then raise a ``ValueError``.
:type resource_url: str :param resource_url: A URL specifying where the resource should be loaded from. The default protocol is "nltk:", which searches for the file in the the NLTK data package. """ filename = os.path.split(filename)[-1] else:
# Open the input & output streams.
# Copy infile -> outfile, using 64k blocks.
# Close both files.
#: A dictionary describing the formats that are supported by NLTK's #: load() method. Keys are format names, and values are format #: descriptions. 'pickle': "A serialized python object, stored using the pickle module.", 'yaml': "A serialized python object, stored using the yaml module.", 'cfg': "A context free grammar, parsed by nltk.parse_cfg().", 'pcfg': "A probabilistic CFG, parsed by nltk.parse_pcfg().", 'fcfg': "A feature CFG, parsed by nltk.parse_fcfg().", 'fol': "A list of first order logic expressions, parsed by " "nltk.sem.parse_fol() using nltk.sem.logic.LogicParser.", 'logic': "A list of first order logic expressions, parsed by " "nltk.sem.parse_logic(). Requires an additional logic_parser " "parameter", 'val': "A semantic valuation, parsed by nltk.sem.parse_valuation().", 'raw': "The raw (byte string) contents of a file.", }
#: A dictionary mapping from file extensions to format names, used #: by load() when format="auto" to decide the format for a #: given resource url. 'pickle': 'pickle', 'yaml': 'yaml', 'cfg': 'cfg', 'pcfg': 'pcfg', 'fcfg': 'fcfg', 'fol': 'fol', 'logic': 'logic', 'val': 'val'}
logic_parser=None, fstruct_parser=None): """ Load a given resource from the NLTK data package. The following resource formats are currently supported:
- ``pickle`` - ``yaml`` - ``cfg`` (context free grammars) - ``pcfg`` (probabilistic CFGs) - ``fcfg`` (feature-based CFGs) - ``fol`` (formulas of First Order Logic) - ``logic`` (Logical formulas to be parsed by the given logic_parser) - ``val`` (valuation of First Order Logic model) - ``raw``
If no format is specified, ``load()`` will attempt to determine a format based on the resource name's file extension. If that fails, ``load()`` will raise a ``ValueError`` exception.
:type resource_url: str :param resource_url: A URL specifying where the resource should be loaded from. The default protocol is "nltk:", which searches for the file in the the NLTK data package. :type cache: bool :param cache: If true, add this resource to a cache. If load() finds a resource in its cache, then it will return it from the cache rather than loading it. The cache uses weak references, so a resource wil automatically be expunged from the cache when no more objects are using it. :type verbose: bool :param verbose: If true, print a message when loading a resource. Messages are not displayed when a resource is retrieved from the cache. :type logic_parser: LogicParser :param logic_parser: The parser that will be used to parse logical expressions. :type fstruct_parser: FeatStructParser :param fstruct_parser: The parser that will be used to parse the feature structure of an fcfg. """ # If we've cached the resource, then just return it.
# Let the user know what's going on.
# Determine the format of the resource. ext = resource_url_parts[-2] 'on its file\nextension; use the "format" ' 'argument to specify the format explicitly.' % resource_url)
# Load the resource. import yaml resource_val = yaml.load(_open(resource_url)) resource_val = nltk.grammar.parse_pcfg(_open(resource_url).read()) logic_parser=logic_parser, fstruct_parser=fstruct_parser) logic_parser=nltk.sem.logic.LogicParser()) resource_val = nltk.sem.parse_logic(_open(resource_url).read(), logic_parser=logic_parser) else:
# If requested, add it to the cache. except TypeError: # We can't create weak references to some object types, like # strings and tuples. For now, just don't cache them. pass
""" Write out a grammar file, ignoring escaped and empty lines.
:type resource_url: str :param resource_url: A URL specifying where the resource should be loaded from. The default protocol is "nltk:", which searches for the file in the the NLTK data package. :type escape: str :param escape: Prepended string that signals lines to be ignored """
""" Remove all objects from the resource cache. :see: load() """
""" Helper function that returns an open file object for a resource, given its resource URL. If the given resource URL uses the "nltk:" protocol, or uses no protocol, then use ``nltk.data.find`` to find its path, and open it with the given mode; if the resource URL uses the 'file' protocol, then open the file with the given mode; otherwise, delegate to ``urllib2.urlopen``.
:type resource_url: str :param resource_url: A URL specifying where the resource should be loaded from. The default protocol is "nltk:", which searches for the file in the the NLTK data package. """ # Divide the resource name into "<protocol>:<path>".
# urllib might not use mode='rb', so handle this one ourselves: else:
###################################################################### # Lazy Resource Loader ######################################################################
# This is where the magic happens! Transform ourselves into # the object by modifying our own __dict__ and __class__ to # match that of `resource`.
# This looks circular, but its not, since __load() changes our # __class__ to something new:
# This looks circular, but its not, since __load() changes our # __class__ to something new:
###################################################################### # Open-On-Demand ZipFile ######################################################################
""" A subclass of ``zipfile.ZipFile`` that closes its file pointer whenever it is not using it; and re-opens it when it needs to read data from the zipfile. This is useful for reducing the number of open file handles when many zip files are being accessed at once. ``OpenOnDemandZipFile`` must be constructed from a filename, not a file-like object (to allow re-opening). ``OpenOnDemandZipFile`` is read-only (i.e. ``write()`` and ``writestr()`` are disabled. """ raise TypeError('ReopenableZipFile filename must be a string')
""":raise NotImplementedError: OpenOnDemandZipfile is read-only""" raise NotImplementedError('OpenOnDemandZipfile is read-only')
""":raise NotImplementedError: OpenOnDemandZipfile is read-only""" raise NotImplementedError('OpenOnDemandZipfile is read-only')
return 'OpenOnDemandZipFile(%r)' % self.filename
###################################################################### #{ Seekable Unicode Stream Reader ######################################################################
""" A stream reader that automatically encodes the source byte stream into unicode (like ``codecs.StreamReader``); but still supports the ``seek()`` and ``tell()`` operations correctly. This is in contrast to ``codecs.StreamReader``, which provide *broken* ``seek()`` and ``tell()`` methods.
This class was motivated by ``StreamBackedCorpusView``, which makes extensive use of ``seek()`` and ``tell()``, and needs to be able to handle unicode-encoded files.
Note: this class requires stateless decoders. To my knowledge, this shouldn't cause a problem with any of python's builtin unicode encodings. """
# Rewind the stream to its beginning.
"""The underlying stream."""
"""The name of the encoding that should be used to encode the underlying stream."""
"""The error mode that should be used when decoding data from the underlying stream. Can be 'strict', 'ignore', or 'replace'."""
"""The function that is used to decode byte strings into unicode strings."""
"""A buffer to use bytes that have been read but have not yet been decoded. This is only used when the final bytes from a read do not form a complete encoding for a character."""
"""A buffer used by ``readline()`` to hold characters that have been read, but have not yet been returned by ``read()`` or ``readline()``. This buffer consists of a list of unicode strings, where each string corresponds to a single line. The final element of the list may or may not be a complete line. Note that the existence of a linebuffer makes the ``tell()`` operation more complex, because it must backtrack to the beginning of the buffer to determine the correct file position in the underlying byte stream."""
"""The file position at which the most recent read on the underlying stream began. This is used, together with ``_rewind_numchars``, to backtrack to the beginning of ``linebuffer`` (which is required by ``tell()``)."""
"""The number of characters that have been returned since the read that started at ``_rewind_checkpoint``. This is used, together with ``_rewind_checkpoint``, to backtrack to the beginning of ``linebuffer`` (which is required by ``tell()``)."""
the stream (or None for no byte order marker)."""
#///////////////////////////////////////////////////////////////// # Read methods #/////////////////////////////////////////////////////////////////
""" Read up to ``size`` bytes, decode them using this reader's encoding, and return the resulting unicode string.
:param size: The maximum number of bytes to read. If not specified, then read as many bytes as possible. :type size: int :rtype: unicode """
# If linebuffer is not empty, then include it in the result chars = ''.join(self.linebuffer) + chars self.linebuffer = None self._rewind_numchars = None
""" Read a line of text, decode it using this reader's encoding, and return the resulting unicode string.
:param size: The maximum number of bytes to read. If no newline is encountered before ``size`` bytes have been read, then the returned value may not be a complete line of text. :type size: int """ # If we have a non-empty linebuffer, then return the first # line from it. (Note that the last element of linebuffer may # not be a complete line; so let _read() deal with it.)
# If there's a remaining incomplete line in the buffer, add it.
# If we're at a '\r', then read one extra character, since # it might be a '\n', to get the proper line ending. new_chars += self._read(1)
# Read successively larger blocks of text.
""" Read this file's contents, decode them using this reader's encoding, and return it as a list of unicode lines.
:rtype: list(unicode) :param sizehint: Ignored. :param keepends: If false, then strip newlines. """ return self.read().splitlines(keepends)
"""Return the next decoded line from the underlying stream.""" line = self.readline() if line: return line else: raise StopIteration
return self.next()
"""Return self""" return self
"""Return self""" return self
#///////////////////////////////////////////////////////////////// # Pass-through methods & properties #/////////////////////////////////////////////////////////////////
def closed(self): """True if the underlying stream is closed.""" return self.stream.closed
def name(self): """The name of the underlying stream.""" return self.stream.name
def mode(self): """The mode of the underlying stream.""" return self.stream.mode
""" Close the underlying stream. """
#///////////////////////////////////////////////////////////////// # Seek and tell #/////////////////////////////////////////////////////////////////
""" Move the stream to a new file position. If the reader is maintaining any buffers, tehn they will be cleared.
:param offset: A byte count offset. :param whence: If 0, then the offset is from the start of the file (offset should be positive), if 1, then the offset is from the current position (offset may be positive or negative); and if 2, then the offset is from the end of the file (offset should typically be negative). """ raise ValueError('Relative seek is not supported for ' 'SeekableUnicodeStreamReader -- consider ' 'using char_seek_forward() instead.')
""" Move the read pointer forward by ``offset`` characters. """ raise ValueError('Negative offsets are not supported') # Clear all buffers. # Perform the seek operation.
""" Move the file position forward by ``offset`` characters, ignoring all buffers.
:param est_bytes: A hint, giving an estimate of the number of bytes that will be neded to move forward by ``offset`` chars. Defaults to ``offset``. """
# Read in a block of bytes.
# Decode the bytes to characters.
# If we got the right number of characters, then seek # backwards over any truncated characters, and return.
# If we went too far, then we can back-up until we get it # right, using the bytes we've already read. # Assume at least one byte/char.
# Otherwise, we haven't read enough bytes yet; loop again.
""" Return the current file position on the underlying byte stream. If this reader is maintaining any buffers, then the returned file position will be the position of the beginning of those buffers. """ # If nothing's buffered, then just return our current filepos:
# Otherwise, we'll need to backtrack the filepos until we # reach the beginning of the buffer.
# Store our original file position, so we can return here.
# Calculate an estimate of where we think the newline is. self._rewind_checkpoint ) (self._rewind_numchars + buf_size))
# Sanity check
# Return to our original filepos (so we don't have to throw # out our buffer.)
# Return the calculated filepos
#///////////////////////////////////////////////////////////////// # Helper methods #/////////////////////////////////////////////////////////////////
""" Read up to ``size`` bytes from the underlying stream, decode them using this reader's encoding, and return the resulting unicode string. ``linebuffer`` is not included in the result. """
# Skip past the byte order marker, if present. self.stream.read(self._bom)
# Read the requested number of bytes. new_bytes = self.stream.read() else:
# Decode the bytes into unicode characters
# If we got bytes but couldn't decode any, then read further. while not chars: new_bytes = self.stream.read(1) if not new_bytes: break # end of file. bytes += new_bytes chars, bytes_decoded = self._incr_decode(bytes)
# Record any bytes we didn't consume.
# Return the result
""" Decode the given byte string into a unicode string, using this reader's encoding. If an exception is encountered that appears to be caused by a truncation error, then just decode the byte string without the bytes that cause the trunctaion error.
Return a tuple ``(chars, num_consumed)``, where ``chars`` is the decoded unicode string, and ``num_consumed`` is the number of bytes that were consumed. """ # If the exception occurs at the end of the string, # then assume that it's a truncation error.
# Otherwise, if we're being strict, then raise it. elif self.errors == 'strict': raise
# If we're not strict, then re-process it with our # errors setting. This *may* raise an exception. else: return self.decode(bytes, self.errors)
'utf8': [(codecs.BOM_UTF8, None)], 'utf16': [(codecs.BOM_UTF16_LE, 'utf16-le'), (codecs.BOM_UTF16_BE, 'utf16-be')], 'utf16le': [(codecs.BOM_UTF16_LE, None)], 'utf16be': [(codecs.BOM_UTF16_BE, None)], 'utf32': [(codecs.BOM_UTF32_LE, 'utf32-le'), (codecs.BOM_UTF32_BE, 'utf32-be')], 'utf32le': [(codecs.BOM_UTF32_LE, None)], 'utf32be': [(codecs.BOM_UTF32_BE, None)], }
# Normalize our encoding name
# Look up our encoding in the BOM table.
# Read a prefix, to check against the BOM(s)
# Check for each possible BOM. if new_encoding: self.encoding = new_encoding return len(bom)
'GzipFileSystemPathPointer', 'GzipFileSystemPathPointer', 'find', 'retrieve', 'FORMATS', 'AUTO_FORMATS', 'load', 'show_cfg', 'clear_cache', 'LazyLoader', 'OpenOnDemandZipFile', 'GzipFileSystemPathPointer', 'SeekableUnicodeStreamReader'] |