class DependencyGraph(object): (source)
Constructor: DependencyGraph(tree_str, cell_extractor, zero_based, cell_separator, top_relation_label)
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 |
Adds an arc from the node specified by head_address to the node specified by the mod address. |
Method | add |
Undocumented |
Method | connect |
Fully connects all non-root nodes. All nodes are set to be dependents of the root node. |
Method | contains |
Returns true if the graph contains a node with the given node address, false otherwise. |
Method | contains |
Check whether there are cycles. |
Method | get |
Return the node with the given address. |
Method | get |
Undocumented |
Method | left |
Returns the number of left children under the node specified by the given address. |
Method | nx |
Convert the data in a nodelist into a networkx labeled directed graph. |
Method | redirect |
Redirects arcs to any of the nodes in the originals list to the redirect node address. |
Method | remove |
Removes the node with the given address. References to this node in others will still exist. |
Method | right |
Returns the number of right children under the node specified by the given address. |
Method | to |
The dependency graph in CoNLL format. |
Method | to |
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 |
Undocumented |
Instance Variable | root |
Undocumented |
Instance Variable | top |
Undocumented |
Method | _hd |
Undocumented |
Method | _parse |
Parse a sentence. |
Method | _rel |
Undocumented |
Method | _repr |
Show SVG representation of the transducer (IPython magic). |
Method | _tree |
Turn dependency graphs into NLTK trees. |
Method | _word |
Undocumented |
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 | |
filename | a name of a file in Malt-TAB format |
zero | nodes in the input file are numbered starting from 0 |
cell | Undocumented |
top | Undocumented |
Returns | |
a list of DependencyGraphs |
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 | Undocumented |
cell | Undocumented |
zero | Undocumented |
cell | Undocumented |
top | Undocumented |
str cell | the cell separator. If not provided, cells |
str top | the label by which the top relation is |
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]
The dependency graph in CoNLL format.
Parameters | |
style:int | the style to use for the format (3, 4, 10 columns) |
Returns | |
str | Undocumented |
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)"] }
Starting with the root node, build a dependency tree using the NLTK Tree constructor. Dependency labels are omitted.
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 | Undocumented |
zero | Undocumented |
cell | Undocumented |
top | Undocumented |
extractor | a function that given a tuple of cells returns a |
str cell | the cell separator. If not provided, cells |
str top | the label by which the top relation is |
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"?>'