nltk.corpus.reader.ProsConsCorpusReader¶
- class nltk.corpus.reader.ProsConsCorpusReader[source]¶
Bases:
CategorizedCorpusReader
,CorpusReader
Reader for the Pros and Cons sentence dataset.
>>> from nltk.corpus import pros_cons >>> pros_cons.sents(categories='Cons') [['East', 'batteries', '!', 'On', '-', 'off', 'switch', 'too', 'easy', 'to', 'maneuver', '.'], ['Eats', '...', 'no', ',', 'GULPS', 'batteries'], ...] >>> pros_cons.words('IntegratedPros.txt') ['Easy', 'to', 'use', ',', 'economical', '!', ...]
- CorpusView[source]¶
alias of
StreamBackedCorpusView
- __init__(root, fileids, word_tokenizer=WordPunctTokenizer(pattern='\\w+|[^\\w\\s]+', gaps=False, discard_empty=True, flags=re.UNICODE | re.MULTILINE | re.DOTALL), encoding='utf8', **kwargs)[source]¶
- Parameters
root – The root directory for the corpus.
fileids – a list or regexp specifying the fileids in the corpus.
word_tokenizer – a tokenizer for breaking sentences or paragraphs into words. Default: WhitespaceTokenizer
encoding – the encoding that should be used to read the corpus.
kwargs – additional parameters passed to CategorizedCorpusReader.
- sents(fileids=None, categories=None)[source]¶
Return all sentences in the corpus or in the specified files/categories.
- Parameters
fileids – a list or regexp specifying the ids of the files whose sentences have to be returned.
categories – a list specifying the categories whose sentences have to be returned.
- Returns
the given file(s) as a list of sentences. Each sentence is tokenized using the specified word_tokenizer.
- Return type
list(list(str))
- words(fileids=None, categories=None)[source]¶
Return all words and punctuation symbols in the corpus or in the specified files/categories.
- Parameters
fileids – a list or regexp specifying the ids of the files whose words have to be returned.
categories – a list specifying the categories whose words have to be returned.
- Returns
the given file(s) as a list of words and punctuation symbols.
- Return type
list(str)
- 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
- 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)
- 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.
- 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.
- 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).
- 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.
- 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.
- raw(fileids=None, categories=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
- property root¶
The directory where this corpus is stored.
- Type