Code Coverage for nltk.parse.pchart
Untested Functions
|
Partially Tested Functions
|
"""
Classes and interfaces for associating probabilities with tree
structures that represent the internal organization of a text. The
probabilistic parser module defines C{BottomUpChartParser}.
C{BottomUpChartParser} is an abstract class that implements a
bottom-up chart parser for C{PCFG}s. It maintains a queue of edges,
and adds them to the chart one at a time. The ordering of this queue
is based on the probabilities associated with the edges, allowing the
parser to expand more likely edges before less likely ones. Each
subclass implements a different queue ordering, producing different
search strategies. Currently the following subclasses are defined:
- C{InsideChartParser} searches edges in decreasing order of
their trees' inside probabilities.
- C{RandomChartParser} searches edges in random order.
- C{LongestChartParser} searches edges in decreasing order of their
location's length.
The C{BottomUpChartParser} constructor has an optional argument beam_size.
If non-zero, this controls the size of the beam (aka the edge queue).
This option is most useful with InsideChartParser.
"""
from nltk.tree import Tree, ProbabilisticTree
from nltk import Nonterminal
from api import *
from chart import Chart, LeafEdge, TreeEdge, AbstractChartRule
class ProbabilisticLeafEdge(LeafEdge):
def prob(self): return 1.0
class ProbabilisticTreeEdge(TreeEdge):
def __init__(self, prob, *args, **kwargs):
self._prob = prob
TreeEdge.__init__(self, *args, **kwargs)
def prob(self): return self._prob
def __cmp__(self, other):
if self._prob != other.prob(): return -1
return TreeEdge.__cmp__(self, other)
def from_production(production, index, p):
return ProbabilisticTreeEdge(p, (index, index), production.lhs(),
production.rhs(), 0)
from_production = staticmethod(from_production)
class BottomUpInitRule(AbstractChartRule):
NUM_EDGES=0
def apply_iter(self, chart, grammar):
for index in range(chart.num_leaves()):
new_edge = ProbabilisticLeafEdge(chart.leaf(index), index)
if chart.insert(new_edge, ()):
yield new_edge
class BottomUpPredictRule(AbstractChartRule):
NUM_EDGES=1
def apply_iter(self, chart, grammar, edge):
if edge.is_incomplete(): return
for prod in grammar.productions():
if edge.lhs() == prod.rhs()[0]:
new_edge = ProbabilisticTreeEdge.from_production(prod, edge.start(), prod.prob())
if chart.insert(new_edge, ()):
yield new_edge
class ProbabilisticFundamentalRule(AbstractChartRule):
NUM_EDGES=2
def apply_iter(self, chart, grammar, left_edge, right_edge):
if not (left_edge.end() == right_edge.start() and
left_edge.next() == right_edge.lhs() and
left_edge.is_incomplete() and right_edge.is_complete()):
return
p = left_edge.prob() * right_edge.prob()
new_edge = ProbabilisticTreeEdge(p,
span=(left_edge.start(), right_edge.end()),
lhs=left_edge.lhs(), rhs=left_edge.rhs(),
dot=left_edge.dot()+1)
changed_chart = False
for cpl1 in chart.child_pointer_lists(left_edge):
if chart.insert(new_edge, cpl1+(right_edge,)):
changed_chart = True
if changed_chart: yield new_edge
class SingleEdgeProbabilisticFundamentalRule(AbstractChartRule):
NUM_EDGES=1
_fundamental_rule = ProbabilisticFundamentalRule()
def apply_iter(self, chart, grammar, edge1):
fr = self._fundamental_rule
if edge1.is_incomplete():
for edge2 in chart.select(start=edge1.end(), is_complete=True,
lhs=edge1.next()):
for new_edge in fr.apply_iter(chart, grammar, edge1, edge2):
yield new_edge
else:
for edge2 in chart.select(end=edge1.start(), is_complete=False,
next=edge1.lhs()):
for new_edge in fr.apply_iter(chart, grammar, edge2, edge1):
yield new_edge
def __str__(self): return 'Fundamental Rule'
class BottomUpChartParser(ParserI):
"""
An abstract bottom-up parser for C{PCFG}s that uses a C{Chart} to
record partial results. C{BottomUpChartParser} maintains a
queue of edges that can be added to the chart. This queue is
initialized with edges for each token in the text that is being
parsed. C{BottomUpChartParser} inserts these edges into the
chart one at a time, starting with the most likely edges, and
proceeding to less likely edges. For each edge that is added to
the chart, it may become possible to insert additional edges into
the chart; these are added to the queue. This process continues
until enough complete parses have been generated, or until the
queue is empty.
The sorting order for the queue is not specified by
C{BottomUpChartParser}. Different sorting orders will result
in different search strategies. The sorting order for the queue
is defined by the method C{sort_queue}; subclasses are required
to provide a definition for this method.
@type _grammar: C{PCFG}
@ivar _grammar: The grammar used to parse sentences.
@type _trace: C{int}
@ivar _trace: The level of tracing output that should be generated
when parsing a text.
"""
def __init__(self, grammar, beam_size=0, trace=0):
"""
Create a new C{BottomUpChartParser}, that uses C{grammar}
to parse texts.
@type grammar: C{PCFG}
@param grammar: The grammar used to parse texts.
@type beam_size: C{int}
@param beam_size: The maximum length for the parser's edge queue.
@type trace: C{int}
@param trace: The level of tracing that should be used when
parsing a text. C{0} will generate no tracing output;
and higher numbers will produce more verbose tracing
output.
"""
self._grammar = grammar
self.beam_size = beam_size
self._trace = trace
def grammar(self):
return self._grammar
def trace(self, trace=2):
"""
Set the level of tracing output that should be generated when
parsing a text.
@type trace: C{int}
@param trace: The trace level. A trace level of C{0} will
generate no tracing output; and higher trace levels will
produce more verbose tracing output.
@rtype: C{None}
"""
self._trace = trace
def nbest_parse(self, tokens, n=None):
self._grammar.check_coverage(tokens)
chart = Chart(list(tokens))
grammar = self._grammar
bu_init = BottomUpInitRule()
bu = BottomUpPredictRule()
fr = SingleEdgeProbabilisticFundamentalRule()
queue = []
for e in bu_init.apply_iter(chart, grammar):
if self._trace>1: chart.pp_edge(e,width=2)
queue.append(e)
while len(queue) > 0:
self.sort_queue(queue, chart)
if self.beam_size:
self._prune(queue, chart)
edge = queue.pop()
if self._trace>0:
print ' %-50s [%s]' % (chart.pp_edge(edge,width=2),
edge.prob())
queue.extend(bu.apply(chart, grammar, edge))
queue.extend(fr.apply(chart, grammar, edge))
parses = chart.parses(grammar.start(), ProbabilisticTree)
prod_probs = {}
for prod in grammar.productions():
prod_probs[prod.lhs(), prod.rhs()] = prod.prob()
for parse in parses:
self._setprob(parse, prod_probs)
parses.sort(lambda a,b: cmp(b.prob(), a.prob()))
return parses[:n]
def _setprob(self, tree, prod_probs):
if tree.prob() is not None: return
lhs = Nonterminal(tree.node)
rhs = []
for child in tree:
if isinstance(child, Tree):
rhs.append(Nonterminal(child.node))
else:
rhs.append(child)
prob = prod_probs[lhs, tuple(rhs)]
for child in tree:
if isinstance(child, Tree):
self._setprob(child, prod_probs)
prob *= child.prob()
tree.set_prob(prob)
def sort_queue(self, queue, chart):
"""
Sort the given queue of C{Edge}s, placing the edge that should
be tried first at the beginning of the queue. This method
will be called after each C{Edge} is added to the queue.
@param queue: The queue of C{Edge}s to sort. Each edge in
this queue is an edge that could be added to the chart by
the fundamental rule; but that has not yet been added.
@type queue: C{list} of C{Edge}
@param chart: The chart being used to parse the text. This
chart can be used to provide extra information for sorting
the queue.
@type chart: C{Chart}
@rtype: C{None}
"""
raise AssertionError, "BottomUpChartParser is an abstract class"
def _prune(self, queue, chart):
""" Discard items in the queue if the queue is longer than the beam."""
if len(queue) > self.beam_size:
split = len(queue)-self.beam_size
if self._trace > 2:
for edge in queue[:split]:
print ' %-50s [DISCARDED]' % chart.pp_edge(edge,2)
del queue[:split]
class InsideChartParser(BottomUpChartParser):
"""
A bottom-up parser for C{PCFG}s that tries edges in descending
order of the inside probabilities of their trees. The X{inside
probability} of a tree is simply the
probability of the entire tree, ignoring its context. In
particular, the inside probability of a tree generated by
production M{p} with children M{c[1]}, M{c[2]}, ..., M{c[n]} is
P(M{p})*P(M{c[1]})*P(M{c[2]})*M{...}*P(M{c[n]}); and the inside
probability of a token is 1 if it is present in the text, and 0 if
it is absent.
This sorting order results in a type of lowest-cost-first search
strategy.
"""
def sort_queue(self, queue, chart):
"""
Sort the given queue of edges, in descending order of the
inside probabilities of the edges' trees.
@param queue: The queue of C{Edge}s to sort. Each edge in
this queue is an edge that could be added to the chart by
the fundamental rule; but that has not yet been added.
@type queue: C{list} of C{Edge}
@param chart: The chart being used to parse the text. This
chart can be used to provide extra information for sorting
the queue.
@type chart: C{Chart}
@rtype: C{None}
"""
queue.sort(lambda e1,e2:cmp(e1.prob(), e2.prob()))
import random
class RandomChartParser(BottomUpChartParser):
"""
A bottom-up parser for C{PCFG}s that tries edges in random order.
This sorting order results in a random search strategy.
"""
def sort_queue(self, queue, chart):
i = random.randint(0, len(queue)-1)
(queue[-1], queue[i]) = (queue[i], queue[-1])
class UnsortedChartParser(BottomUpChartParser):
"""
A bottom-up parser for C{PCFG}s that tries edges in whatever order.
"""
def sort_queue(self, queue, chart): return
class LongestChartParser(BottomUpChartParser):
"""
A bottom-up parser for C{PCFG}s that tries longer edges before
shorter ones. This sorting order results in a type of best-first
search strategy.
"""
def sort_queue(self, queue, chart):
queue.sort(lambda e1,e2: cmp(e1.length(), e2.length()))
def demo():
"""
A demonstration of the probabilistic parsers. The user is
prompted to select which demo to run, and how many parses should
be found; and then each parser is run on the same demo, and a
summary of the results are displayed.
"""
import sys, time
from nltk import tokenize, cfg
from nltk.parse import pchart
demos = [('I saw John with my telescope', cfg.toy_pcfg1),
('the boy saw Jack with Bob under the table with a telescope',
cfg.toy_pcfg2)]
print
for i in range(len(demos)):
print '%3s: %s' % (i+1, demos[i][0])
print ' %r' % demos[i][1]
print
print 'Which demo (%d-%d)? ' % (1, len(demos)),
try:
snum = int(sys.stdin.readline().strip())-1
sent, grammar = demos[snum]
except:
print 'Bad sentence number'
return
tokens = sent.split()
parsers = [
pchart.InsideChartParser(grammar),
pchart.RandomChartParser(grammar),
pchart.UnsortedChartParser(grammar),
pchart.LongestChartParser(grammar),
pchart.InsideChartParser(grammar, beam_size = len(tokens)+1)
]
times = []
average_p = []
num_parses = []
all_parses = {}
for parser in parsers:
print '\ns: %s\nparser: %s\ngrammar: %s' % (sent,parser,grammar)
parser.trace(3)
t = time.time()
parses = parser.nbest_parse(tokens)
times.append(time.time()-t)
if parses: p = reduce(lambda a,b:a+b.prob(), parses, 0)/len(parses)
else: p = 0
average_p.append(p)
num_parses.append(len(parses))
for p in parses: all_parses[p.freeze()] = 1
print
print ' Parser Beam | Time (secs) # Parses Average P(parse)'
print '------------------------+------------------------------------------'
for i in range(len(parsers)):
print '%18s %4d |%11.4f%11d%19.14f' % (parsers[i].__class__.__name__,
parsers[i].beam_size,
times[i],num_parses[i],average_p[i])
parses = all_parses.keys()
if parses: p = reduce(lambda a,b:a+b.prob(), parses, 0)/len(parses)
else: p = 0
print '------------------------+------------------------------------------'
print '%18s |%11s%11d%19.14f' % ('(All Parses)', 'n/a', len(parses), p)
print
print 'Draw parses (y/n)? ',
if sys.stdin.readline().strip().lower().startswith('y'):
from nltk.draw.tree import draw_trees
print ' please wait...'
draw_trees(*parses)
print
print 'Print parses (y/n)? ',
if sys.stdin.readline().strip().lower().startswith('y'):
for parse in parses:
print parse
if __name__ == '__main__':
demo()