grafeno.graph module

This module provides the main Graph class. Graph objects are the core of the library, and most operations revolve around manipulating them.

from grafeno import Graph as CG

g = CG(transformer = MyTransformer)
print(g.linearize(linearizer = MyLinearizer))
class grafeno.graph.Graph(original=None, transformer=None, transformer_args={}, text=None, subgraph=None, from_networkx=None)

Bases: object

Semantic graph class. Nodes represent concepts, while edges stand for the relations between them.

Parameters:

transformer : Transformer, optional

If provided, it will be used to transform all text added to the graph into semantic nodes and edges.

transformer_args : dict, optional

Arguments for the transformer class.

text : string, optional

If provided, this text will be added to the graph (transformed with the transformer class).

original : Graph, optional

If provided, the new graph will be initialized with the existing information in original.

subgraph : bunch of nodes

If original and subgraph are provided, only the nodes in subgraph will be copied over from original.

Attributes

gram (dict) dictionary of parameters global to the conceptual graph.
node (dict) dictionary of concept nodes, indexed by node id.

Methods

add_edge(head, dependent, functor, **gram)

Creates a semantic edge between two concept nodes in the graph.

Parameters:

head, dependent : node_id

The graph ids of the nodes to link. The edge is directed, from head to dependent.

functor : string

The textual representation of the _functor_, the name of the relation between the concepts.

gram : keyword args, optional

Additional ‘grammatemes’, a free-form python dict of attributes to attach to the edge.

Raises:

ValueError

When the head or dependent id’s are not valid.

add_node(concept, **gram)

Creates a concept node in the graph.

Parameters:

concept : string

The (non-unique) textual representation of the concept node.

gram : keyword args, optional

Additional ‘grammatemes’, a free-form python dict of attributes to attach to the node.

Returns:

int

The graph id of the newly created node.

add_text(text)

Processes a text, and adds the resulting nodes and edges to the graph.

Parameters:

text : string

A clean text to process and add to the graph.

all_edges()

Iterates over all the edges in the graph.

Returns:

An iterator over all the edges of the graph, in the form of tuples

(head id, dependent id, edge).

draw(bunch=None)

Draws the graph on screen.

Note

Requires matplotlib and a compatible configured environment.

Parameters:

bunch : list of nodes

An iterable of node ids to draw, if None then all nodes are included.

edges(nid)

Returns a dictionary of the dependents of a node.

Parameters:

nid : int

ID of the node

Returns:

A dictionary of edges, keyed by neighbor id, and with data

the grammatemes of the edge.

linearize(linearizer=None, linearizer_args={})

Linearizes a graph into a string.

Parameters:

linearizer : Linearizer, optional

If provided, from this point on all linearizations of the graph will use an instance of this class. The linearizer is used to transform the semantic data, nodes and edges, into a string representation.

linearizer_args : dict, optional

Arguments for the linearizer class.

Returns:

A string, the result of running the linearizer on the graph.

neighbours(node)

Iterates over the neighbours of a node, giving the edge information for each neighbour.

node = graph.node[0]
for neighbour, edge in graph.neighbours(node)
    print('{}-{}->{}'.format(
            node['concept'],
            edge['functor'],
            neighbour['concept']))
Parameters:

node : node

The node in the graph to explore

Returns:

An iterator over the neighbours of the node, in the form of tuples

(node, edge).

nodes()

Returns a list of all the nodes in the graph. Each node is represented as a dictionary of concept and further grammatemes.

to_json(with_labels=True)

Returns a JSON representation of the graph data.

Parameters:

with_labels : bool

If True, a ‘label’ attribute is added to nodes and edges with the _concept_ and _functor_, respectively. Useful for further consuming by some libraries.

Returns:

A string with the graph data encoded in JSON.