Package nltk :: Package parse :: Module chart :: Class Chart
[hide private]
[frames] | no frames]

Class Chart

source code

object --+
         |
        Chart
Known Subclasses:

A blackboard for hypotheses about the syntactic constituents of a sentence. A chart contains a set of edges, and each edge encodes a single hypothesis about the structure of some portion of the sentence.

The select method can be used to select a specific collection of edges. For example chart.select(is_complete=True, start=0) yields all complete edges whose start indices are 0. To ensure the efficiency of these selection operations, Chart dynamically creates and maintains an index for each set of attributes that have been selected on.

In order to reconstruct the trees that are represented by an edge, the chart associates each edge with a set of child pointer lists. A child pointer list is a list of the edges that license an edge's right-hand side.

Instance Methods [hide private]
 
__init__(self, tokens)
Construct a new empty chart.
source code
int
num_leaves(self)
Returns: The number of words in this chart's sentence.
source code
string
leaf(self, index)
Returns: The leaf value of the word at the given index.
source code
list of string
leaves(self)
Returns: A list of the leaf values of each word in the chart's sentence.
source code
list of EdgeI
edges(self)
Returns: A list of all edges in this chart.
source code
iter of EdgeI
iteredges(self)
Returns: An iterator over the edges in this chart.
source code
iter of EdgeI
__iter__(self)
Returns: An iterator over the edges in this chart.
source code
int
num_edges(self)
Returns: The number of edges contained in this chart.
source code
iter of EdgeI
select(self, **restrictions)
Returns: An iterator over the edges in this chart.
source code
 
_add_index(self, restr_keys)
A helper function for select, which creates a new index for a given set of attributes (aka restriction keys).
source code
bool
insert(self, edge, child_pointer_list)
Add a new edge to the chart.
source code
 
parses(self, root, tree_class=<class 'nltk.tree.Tree'>)
Returns: A list of the complete tree structures that span the entire chart, and whose root node is root.
source code
list of Tree
trees(self, edge, tree_class=<class 'nltk.tree.Tree'>, complete=False)
Returns: A list of the tree structures that are associated with edge.
source code
 
_trees(self, edge, complete, memo, tree_class)
A helper function for trees.
source code
 
_choose_children(self, child_choices)
A helper function for _trees that finds the possible sets of subtrees for a new tree.
source code
list of list of EdgeI
child_pointer_lists(self, edge)
Returns: The set of child pointer lists for the given edge.
source code
string
pp_edge(self, edge, width=None)
Returns: A pretty-printed string representation of a given edge in this chart.
source code
 
pp_leaves(self, width=None)
Returns: A pretty-printed string representation of this chart's leaves.
source code
string
pp(self, width=None)
Returns: A pretty-printed string representation of this chart.
source code
 
dot_digraph(self) source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Instance Variables [hide private]
  _edge_to_cpls
A dictionary mapping each edge to a set of child pointer lists that are associated with that edge.
  _edges
A list of the edges in the chart
  _indexes
A dictionary mapping tuples of edge attributes to indices, where each index maps the corresponding edge attribute values to lists of edges.
  _num_leaves
The number of tokens.
  _tokens
The sentence that the chart covers.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, tokens)
(Constructor)

source code 

Construct a new empty chart.

Parameters:
  • tokens (list) - The sentence that this chart will be used to parse.
Overrides: object.__init__

num_leaves(self)

source code 
Returns: int
The number of words in this chart's sentence.

leaf(self, index)

source code 
Returns: string
The leaf value of the word at the given index.

leaves(self)

source code 
Returns: list of string
A list of the leaf values of each word in the chart's sentence.

edges(self)

source code 
Returns: list of EdgeI
A list of all edges in this chart. New edges that are added to the chart after the call to edges() will not be contained in this list.

See Also: iteredges, select

iteredges(self)

source code 
Returns: iter of EdgeI
An iterator over the edges in this chart. Any new edges that are added to the chart before the iterator is exahusted will also be generated.

See Also: edges, select

__iter__(self)

source code 
Returns: iter of EdgeI
An iterator over the edges in this chart. Any new edges that are added to the chart before the iterator is exahusted will also be generated.

See Also: edges, select

num_edges(self)

source code 
Returns: int
The number of edges contained in this chart.

select(self, **restrictions)

source code 
Parameters:
  • span - Only generate edges e where e.span()==span
  • start - Only generate edges e where e.start()==start
  • end - Only generate edges e where e.end()==end
  • length - Only generate edges e where e.length()==length
  • lhs - Only generate edges e where e.lhs()==lhs
  • rhs - Only generate edges e where e.rhs()==rhs
  • next - Only generate edges e where e.next()==next
  • dot - Only generate edges e where e.dot()==dot
  • is_complete - Only generate edges e where e.is_complete()==is_complete
  • is_incomplete - Only generate edges e where e.is_incomplete()==is_incomplete
Returns: iter of EdgeI
An iterator over the edges in this chart. Any new edges that are added to the chart before the iterator is exahusted will also be generated. restrictions can be used to restrict the set of edges that will be generated.

insert(self, edge, child_pointer_list)

source code 

Add a new edge to the chart.

Parameters:
  • edge (EdgeI) - The new edge
  • child_pointer_list (tuple of EdgeI) - A list of the edges that were used to form this edge. This list is used to reconstruct the trees (or partial trees) that are associated with edge.
Returns: bool
True if this operation modified the chart. In particular, return true iff the chart did not already contain edge, or if it did not already associate child_pointer_list with edge.

parses(self, root, tree_class=<class 'nltk.tree.Tree'>)

source code 
Returns:
A list of the complete tree structures that span the entire chart, and whose root node is root.

trees(self, edge, tree_class=<class 'nltk.tree.Tree'>, complete=False)

source code 
Returns: list of Tree
A list of the tree structures that are associated with edge.

If edge is incomplete, then the unexpanded children will be encoded as childless subtrees, whose node value is the corresponding terminal or nonterminal.

Note: If two trees share a common subtree, then the same Tree may be used to encode that subtree in both trees. If you need to eliminate this subtree sharing, then create a deep copy of each tree.

_trees(self, edge, complete, memo, tree_class)

source code 

A helper function for trees.

Parameters:
  • memo - A dictionary used to record the trees that we've generated for each edge, so that when we see an edge more than once, we can reuse the same trees.

_choose_children(self, child_choices)

source code 

A helper function for _trees that finds the possible sets of subtrees for a new tree.

Parameters:
  • child_choices - A list that specifies the options for each child. In particular, child_choices[i] is a list of tokens and subtrees that can be used as the ith child.

child_pointer_lists(self, edge)

source code 
Returns: list of list of EdgeI
The set of child pointer lists for the given edge. Each child pointer list is a list of edges that have been used to form this edge.

pp_edge(self, edge, width=None)

source code 
Parameters:
  • width - The number of characters allotted to each index in the sentence.
Returns: string
A pretty-printed string representation of a given edge in this chart.

pp_leaves(self, width=None)

source code 
Returns:
A pretty-printed string representation of this chart's leaves. This string can be used as a header for calls to pp_edge.

pp(self, width=None)

source code 
Parameters:
  • width - The number of characters allotted to each index in the sentence.
Returns: string
A pretty-printed string representation of this chart.