class documentation

A container for the nodes and labelled edges of a dependency structure.

Static Method load rather than 1 (as produced by, e.g., zpar) :param str cell_separator: the cell separator. If not provided, cells are split by whitespace. :param str top_relation_label: the label by which the top relation is identified, for examlple, ...
Method __init__ Dependency graph.
Method __repr__ Undocumented
Method __str__ Undocumented
Method add_arc Adds an arc from the node specified by head_address to the node specified by the mod address.
Method add_node Undocumented
Method connect_graph Fully connects all non-root nodes. All nodes are set to be dependents of the root node.
Method contains_address Returns true if the graph contains a node with the given node address, false otherwise.
Method contains_cycle Check whether there are cycles.
Method get_by_address Return the node with the given address.
Method get_cycle_path Undocumented
Method left_children Returns the number of left children under the node specified by the given address.
Method nx_graph Convert the data in a nodelist into a networkx labeled directed graph.
Method redirect_arcs Redirects arcs to any of the nodes in the originals list to the redirect node address.
Method remove_by_address Removes the node with the given address. References to this node in others will still exist.
Method right_children Returns the number of right children under the node specified by the given address.
Method to_conll The dependency graph in CoNLL format.
Method to_dot Return a dot representation suitable for using with Graphviz.
Method tree Starting with the root node, build a dependency tree using the NLTK Tree constructor. Dependency labels are omitted.
Method triples Extract dependency triples of the form: ((head word, head tag), rel, (dep word, dep tag))
Instance Variable nodes Undocumented
Instance Variable nx_labels Undocumented
Instance Variable root Undocumented
Instance Variable top_relation_label Undocumented
Method _hd Undocumented
Method _parse Parse a sentence.
Method _rel Undocumented
Method _repr_svg_ Show SVG representation of the transducer (IPython magic).
Method _tree Turn dependency graphs into NLTK trees.
Method _word Undocumented
@staticmethod
def load(filename, zero_based=False, cell_separator=None, top_relation_label='ROOT'): (source)

rather than 1 (as produced by, e.g., zpar) :param str cell_separator: the cell separator. If not provided, cells are split by whitespace. :param str top_relation_label: the label by which the top relation is identified, for examlple, ROOT, null or TOP.

Parameters
filenamea name of a file in Malt-TAB format
zero_basednodes in the input file are numbered starting from 0
cell_separatorUndocumented
top_relation_labelUndocumented
Returns
a list of DependencyGraphs
def __init__(self, tree_str=None, cell_extractor=None, zero_based=False, cell_separator=None, top_relation_label='ROOT'): (source)

Dependency graph.

We place a dummy TOP node with the index 0, since the root node is often assigned 0 as its head. This also means that the indexing of the nodes corresponds directly to the Malt-TAB format, which starts at 1.

If zero-based is True, then Malt-TAB-like input with node numbers starting at 0 and the root node assigned -1 (as produced by, e.g., zpar).

are split by whitespace.

identified, for examlple, ROOT, null or TOP.

Parameters
tree_strUndocumented
cell_extractorUndocumented
zero_basedUndocumented
cell_separatorUndocumented
top_relation_labelUndocumented
str cell_separatorthe cell separator. If not provided, cells
str top_relation_labelthe label by which the top relation is
def __repr__(self): (source)

Undocumented

def __str__(self): (source)

Undocumented

def add_arc(self, head_address, mod_address): (source)

Adds an arc from the node specified by head_address to the node specified by the mod address.

def add_node(self, node): (source)

Undocumented

def connect_graph(self): (source)

Fully connects all non-root nodes. All nodes are set to be dependents of the root node.

def contains_address(self, node_address): (source)

Returns true if the graph contains a node with the given node address, false otherwise.

def contains_cycle(self): (source)

Check whether there are cycles.

>>> dg = DependencyGraph(treebank_data)
>>> dg.contains_cycle()
False
>>> cyclic_dg = DependencyGraph()
>>> top = {'word': None, 'deps': [1], 'rel': 'TOP', 'address': 0}
>>> child1 = {'word': None, 'deps': [2], 'rel': 'NTOP', 'address': 1}
>>> child2 = {'word': None, 'deps': [4], 'rel': 'NTOP', 'address': 2}
>>> child3 = {'word': None, 'deps': [1], 'rel': 'NTOP', 'address': 3}
>>> child4 = {'word': None, 'deps': [3], 'rel': 'NTOP', 'address': 4}
>>> cyclic_dg.nodes = {
...     0: top,
...     1: child1,
...     2: child2,
...     3: child3,
...     4: child4,
... }
>>> cyclic_dg.root = top
>>> cyclic_dg.contains_cycle()
[3, 1, 2, 4]
def get_by_address(self, node_address): (source)

Return the node with the given address.

def get_cycle_path(self, curr_node, goal_node_index): (source)

Undocumented

def left_children(self, node_index): (source)

Returns the number of left children under the node specified by the given address.

def nx_graph(self): (source)

Convert the data in a nodelist into a networkx labeled directed graph.

def redirect_arcs(self, originals, redirect): (source)

Redirects arcs to any of the nodes in the originals list to the redirect node address.

def remove_by_address(self, address): (source)

Removes the node with the given address. References to this node in others will still exist.

def right_children(self, node_index): (source)

Returns the number of right children under the node specified by the given address.

def to_conll(self, style): (source)

The dependency graph in CoNLL format.

Parameters
style:intthe style to use for the format (3, 4, 10 columns)
Returns
strUndocumented
def to_dot(self): (source)

Return a dot representation suitable for using with Graphviz.

>>> dg = DependencyGraph(
...     'John N 2\n'
...     'loves V 0\n'
...     'Mary N 2'
... )
>>> print(dg.to_dot())
digraph G{
edge [dir=forward]
node [shape=plaintext]
<BLANKLINE>
0 [label="0 (None)"]
0 -> 2 [label="ROOT"]
1 [label="1 (John)"]
2 [label="2 (loves)"]
2 -> 1 [label=""]
2 -> 3 [label=""]
3 [label="3 (Mary)"]
}
def tree(self): (source)

Starting with the root node, build a dependency tree using the NLTK Tree constructor. Dependency labels are omitted.

def triples(self, node=None): (source)

Extract dependency triples of the form: ((head word, head tag), rel, (dep word, dep tag))

Undocumented

nx_labels: dict = (source)

Undocumented

Undocumented

top_relation_label = (source)

Undocumented

def _hd(self, i): (source)

Undocumented

def _parse(self, input_, cell_extractor=None, zero_based=False, cell_separator=None, top_relation_label='ROOT'): (source)

Parse a sentence.

7-tuple, where the values are word, lemma, ctag, tag, feats, head, rel.

are split by whitespace.

identified, for examlple, ROOT, null or TOP.

Parameters
input_Undocumented
cell_extractorUndocumented
zero_basedUndocumented
cell_separatorUndocumented
top_relation_labelUndocumented
extractora function that given a tuple of cells returns a
str cell_separatorthe cell separator. If not provided, cells
str top_relation_labelthe label by which the top relation is
def _rel(self, i): (source)

Undocumented

def _repr_svg_(self): (source)

Show SVG representation of the transducer (IPython magic).

>>> dg = DependencyGraph(
...     'John N 2\n'
...     'loves V 0\n'
...     'Mary N 2'
... )
>>> dg._repr_svg_().split('\n')[0]
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
def _tree(self, i): (source)

Turn dependency graphs into NLTK trees.

Parameters
iUndocumented
int iindex of a node
Returns
either a word (if the indexed node is a leaf) or a Tree.
def _word(self, node, filter=True): (source)

Undocumented