nltk.corpus.reader.ComparativeSentencesCorpusReader¶
- class nltk.corpus.reader.ComparativeSentencesCorpusReader[source]¶
Bases:
CorpusReader
Reader for the Comparative Sentence Dataset by Jindal and Liu (2006).
>>> from nltk.corpus import comparative_sentences >>> comparison = comparative_sentences.comparisons()[0] >>> comparison.text ['its', 'fast-forward', 'and', 'rewind', 'work', 'much', 'more', 'smoothly', 'and', 'consistently', 'than', 'those', 'of', 'other', 'models', 'i', "'ve", 'had', '.'] >>> comparison.entity_2 'models' >>> (comparison.feature, comparison.keyword) ('rewind', 'more') >>> len(comparative_sentences.comparisons()) 853
- CorpusView[source]¶
alias of
StreamBackedCorpusView
- __init__(root, fileids, word_tokenizer=WhitespaceTokenizer(pattern='\\s+', gaps=True, discard_empty=True, flags=re.UNICODE | re.MULTILINE | re.DOTALL), sent_tokenizer=None, encoding='utf8')[source]¶
- Parameters
root – The root directory for this corpus.
fileids – a list or regexp specifying the fileids in this corpus.
word_tokenizer – tokenizer for breaking sentences or paragraphs into words. Default: WhitespaceTokenizer
sent_tokenizer – tokenizer for breaking paragraphs into sentences.
encoding – the encoding that should be used to read the corpus.
- comparisons(fileids=None)[source]¶
Return all comparisons in the corpus.
- Parameters
fileids – a list or regexp specifying the ids of the files whose comparisons have to be returned.
- Returns
the given file(s) as a list of Comparison objects.
- Return type
list(Comparison)
- keywords(fileids=None)[source]¶
Return a set of all keywords used in the corpus.
- Parameters
fileids – a list or regexp specifying the ids of the files whose keywords have to be returned.
- Returns
the set of keywords and comparative phrases used in the corpus.
- Return type
set(str)
- keywords_readme()[source]¶
Return the list of words and constituents considered as clues of a comparison (from listOfkeywords.txt).
- sents(fileids=None)[source]¶
Return all sentences in the corpus.
- Parameters
fileids – a list or regexp specifying the ids of the files whose sentences have to be returned.
- Returns
all sentences of the corpus as lists of tokens (or as plain strings, if no word tokenizer is specified).
- Return type
list(list(str)) or list(str)
- words(fileids=None)[source]¶
Return all words and punctuation symbols in the corpus.
- Parameters
fileids – a list or regexp specifying the ids of the files 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)
- 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).
- 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)[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