Coverage for nltk.corpus.reader.chunked : 41%
![](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: Chunked Corpus Reader # # 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
A reader for corpora that contain chunked (and optionally tagged) documents. """
""" Reader for chunked (and optionally tagged) corpora. Paragraphs are split using a block reader. They are then tokenized into sentences using a sentence tokenizer. Finally, these sentences are parsed into chunk trees using a string-to-chunktree conversion function. Each of these steps can be performed using a default function or a custom function. By default, paragraphs are split on blank lines; sentences are listed one per line; and sentences are parsed into chunk trees using ``nltk.chunk.tagstr2tree``. """ str2chunktree=tagstr2tree, sent_tokenizer=RegexpTokenizer('\n', gaps=True), para_block_reader=read_blankline_block, encoding=None): """ :param root: The root directory for this corpus. :param fileids: A list or regexp specifying the fileids in this corpus. """
(str2chunktree, sent_tokenizer, para_block_tokenizer)"""
""" :return: the given file(s) as a single string. :rtype: str """ 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])
""" :return: the given file(s) as a list of words and punctuation symbols. :rtype: list(str) """ return concat([ChunkedCorpusView(f, enc, 0, 0, 0, 0, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
""" :return: the given file(s) as a list of sentences or utterances, each encoded as a list of word strings. :rtype: list(list(str)) """ return concat([ChunkedCorpusView(f, enc, 0, 1, 0, 0, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
""" :return: the given file(s) as a list of paragraphs, each encoded as a list of sentences, which are in turn encoded as lists of word strings. :rtype: list(list(list(str))) """ return concat([ChunkedCorpusView(f, enc, 0, 1, 1, 0, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
""" :return: the given file(s) as a list of tagged words and punctuation symbols, encoded as tuples ``(word,tag)``. :rtype: list(tuple(str,str)) """ return concat([ChunkedCorpusView(f, enc, 1, 0, 0, 0, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
""" :return: the given file(s) as a list of sentences, each encoded as a list of ``(word,tag)`` tuples.
:rtype: list(list(tuple(str,str))) """ return concat([ChunkedCorpusView(f, enc, 1, 1, 0, 0, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
""" :return: the given file(s) as a list of paragraphs, each encoded as a list of sentences, which are in turn encoded as lists of ``(word,tag)`` tuples. :rtype: list(list(list(tuple(str,str)))) """ return concat([ChunkedCorpusView(f, enc, 1, 1, 1, 0, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
""" :return: the given file(s) as a list of tagged words and chunks. Words are encoded as ``(word, tag)`` tuples (if the corpus has tags) or word strings (if the corpus has no tags). Chunks are encoded as depth-one trees over ``(word,tag)`` tuples or word strings. :rtype: list(tuple(str,str) and Tree) """ return concat([ChunkedCorpusView(f, enc, 1, 0, 0, 1, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
""" :return: the given file(s) as a list of sentences, each encoded as a shallow Tree. The leaves of these trees are encoded as ``(word, tag)`` tuples (if the corpus has tags) or word strings (if the corpus has no tags). :rtype: list(Tree) """ return concat([ChunkedCorpusView(f, enc, 1, 1, 0, 1, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
""" :return: the given file(s) as a list of paragraphs, each encoded as a list of sentences, which are in turn encoded as a shallow Tree. The leaves of these trees are encoded as ``(word, tag)`` tuples (if the corpus has tags) or word strings (if the corpus has no tags). :rtype: list(list(Tree)) """ return concat([ChunkedCorpusView(f, enc, 1, 1, 1, 1, *self._cv_args) for (f, enc) in self.abspaths(fileids, True)])
return [tagstr2tree(t) for t in read_blankline_block(stream)]
group_by_para, chunked, str2chunktree, sent_tokenizer, para_block_reader): StreamBackedCorpusView.__init__(self, fileid, encoding=encoding) self._tagged = tagged self._group_by_sent = group_by_sent self._group_by_para = group_by_para self._chunked = chunked self._str2chunktree = str2chunktree self._sent_tokenizer = sent_tokenizer self._para_block_reader = para_block_reader
block = [] for para_str in self._para_block_reader(stream): para = [] for sent_str in self._sent_tokenizer.tokenize(para_str): sent = self._str2chunktree(sent_str)
# If requested, throw away the tags. if not self._tagged: sent = self._untag(sent)
# If requested, throw away the chunks. if not self._chunked: sent = sent.leaves()
# Add the sentence to `para`. if self._group_by_sent: para.append(sent) else: para.extend(sent)
# Add the paragraph to `block`. if self._group_by_para: block.append(para) else: block.extend(para)
# Return the block return block
for i, child in enumerate(tree): if isinstance(child, Tree): self._untag(child) elif isinstance(child, tuple): tree[i] = child[0] else: raise ValueError('expected child to be Tree or tuple') return tree
|