Coverage for nltk.parse.api : 51%
![](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: Parser API # # Copyright (C) 2001-2012 NLTK Project # Author: Steven Bird <sb@csse.unimelb.edu.au> # Edward Loper <edloper@gradient.cis.upenn.edu> # URL: <http://www.nltk.org/> # For license information, see LICENSE.TXT #
""" A processing class for deriving trees that represent possible structures for a sequence of tokens. These tree structures are known as "parses". Typically, parsers are used to derive syntax trees for sentences. But parsers can also be used to derive other kinds of tree structure, such as morphological trees and discourse structures.
Subclasses must define: - at least one of: ``parse()``, ``nbest_parse()``, ``iter_parse()``, ``batch_parse()``, ``batch_nbest_parse()``, ``batch_iter_parse()``.
Subclasses may define: - ``grammar()`` - either ``prob_parse()`` or ``batch_prob_parse()`` (or both) """ """ :return: The grammar used by this parser. """ raise NotImplementedError()
""" :return: A parse tree that represents the structure of the given sentence, or None if no parse tree is found. If multiple parses are found, then return the best parse.
:param sent: The sentence to be parsed :type sent: list(str) :rtype: Tree """ return self.batch_parse([sent])[0] else:
""" :return: A list of parse trees that represent possible structures for the given sentence. When possible, this list is sorted from most likely to least likely. If ``n`` is specified, then the returned list will contain at most ``n`` parse trees.
:param sent: The sentence to be parsed :type sent: list(str) :param n: The maximum number of trees to return. :type n: int :rtype: list(Tree) """ return self.batch_nbest_parse([sent],n)[0] else: return list(itertools.islice(self.iter_parse(sent), n))
""" :return: An iterator that generates parse trees that represent possible structures for the given sentence. When possible, this list is sorted from most likely to least likely.
:param sent: The sentence to be parsed :type sent: list(str) :rtype: iter(Tree) """ if overridden(self.batch_iter_parse): return self.batch_iter_parse([sent])[0] elif overridden(self.nbest_parse) or overridden(self.batch_nbest_parse): return iter(self.nbest_parse(sent)) elif overridden(self.parse) or overridden(self.batch_parse): tree = self.parse(sent) if tree: return iter([tree]) else: return iter([]) else: raise NotImplementedError()
""" :return: A probability distribution over the possible parse trees for the given sentence. If there are no possible parse trees for the given sentence, return a probability distribution that assigns a probability of 1.0 to None.
:param sent: The sentence to be parsed :type sent: list(str) :rtype: ProbDistI(Tree) """ if overridden(self.batch_prob_parse): return self.batch_prob_parse([sent])[0] else: raise NotImplementedError
""" Apply ``self.parse()`` to each element of ``sents``. I.e.:
return [self.parse(sent) for sent in sents]
:rtype: list(Tree) """ return [self.parse(sent) for sent in sents]
""" Apply ``self.nbest_parse()`` to each element of ``sents``. I.e.:
return [self.nbest_parse(sent, n) for sent in sents]
:rtype: list(list(Tree)) """ return [self.nbest_parse(sent,n ) for sent in sents]
""" Apply ``self.iter_parse()`` to each element of ``sents``. I.e.:
return [self.iter_parse(sent) for sent in sents]
:rtype: list(iter(Tree)) """ return [self.iter_parse(sent) for sent in sents]
""" Apply ``self.prob_parse()`` to each element of ``sents``. I.e.:
return [self.prob_parse(sent) for sent in sents]
:rtype: list(ProbDistI(Tree)) """ return [self.prob_parse(sent) for sent in sents]
|