summarization.graph – Graph used in TextRank summarization

This module contains abstract class IGraph represents graphs interface and class Graph (based on IGraph) which implements undirected graph.

Examples

Create simple graph with 4 nodes.

>>> g = Graph()
>>> g.add_node('Felidae')
>>> g.add_node('Lion')
>>> g.add_node('Tiger')
>>> g.add_node('Wolf')
>>> sorted(g.nodes())
['Felidae', 'Lion', 'Tiger', 'Wolf']

Add some edges and check neighbours.

>>> g.add_edge(("Felidae", "Lion"))
>>> g.add_edge(("Felidae", "Tiger"))
>>> g.neighbors("Felidae")
['Lion', 'Tiger']

One node has no neighbours.

>>> g.neighbors("Wolf")
[]
class gensim.summarization.graph.Graph

Implementation of an undirected graph, based on IGraph.

DEFAULT_WEIGHT

Weight set by default.

Type

float

Initializes object.

DEFAULT_WEIGHT = 0
add_edge(edge, wt=1)

Adds an edge to the graph connecting two nodes.

Parameters
  • edge ((hashable, hashable)) – Given edge.

  • wt (float, optional) – Weight of new edge.

Raises

ValueError – If edge already exists in graph.

add_node(node)

Adds given node to the graph.

Note

While nodes can be of any type, it’s strongly recommended to use only numbers and single-line strings as node identifiers if you intend to use write().

Parameters

node (hashable) – Given node.

Raises

ValueError – If node already exists in graph.

del_edge(edge)

Removes given edges from the graph.

Parameters

edge ((hashable, hashable)) – Given edge.

del_node(node)

Removes given node and its edges from the graph.

Parameters

node (hashable) – Given node.

edge_weight(edge)

Returns weight of given edge.

Parameters

edge ((hashable, hashable)) – Given edge.

Returns

Edge weight.

Return type

float

edges()

Returns all edges of the graph.

Returns

Edges of graph.

Return type

list of (hashable, hashable)

has_edge(edge)

Returns whether an edge exists.

Parameters

edge ((hashable, hashable)) – Given edge.

Returns

True if edge exists, False otherwise.

Return type

bool

has_node(node)

Returns whether the requested node exists.

Parameters

node (hashable) – Given node.

Returns

True if node exists, False otherwise.

Return type

bool

iter_edges()

Returns iterator of all edges of the graph.

Yields

(hashable, hashable) – Edges of graph.

neighbors(node)

Returns all nodes that are directly accessible from given node.

Parameters

node (hashable) – Given node identifier.

Returns

Nodes directly accessible from given node.

Return type

list of hashable

nodes()

Returns all nodes of the graph.

Returns

Nodes of graph.

Return type

list of hashable

class gensim.summarization.graph.IGraph

Represents the interface or contract that the graph for TextRank should implement.

abstract add_edge(edge, wt=1)

Adds an edge to the graph connecting two nodes. An edge, here, is a tuple of two nodes.

Parameters
  • edge ((hashable, hashable)) – Given edge.

  • wt (float, optional) – Weight of new edge.

abstract add_node(node)

Adds given node to the graph.

Note

While nodes can be of any type, it’s strongly recommended to use only numbers and single-line strings as node identifiers if you intend to use write().

Parameters

node (hashable) – Given node

abstract del_node(node)

Removes node and its edges from the graph.

Parameters

node (hashable) – Node to delete.

abstract edge_weight(edge)

Returns weigth of given edge.

Parameters

edge ((hashable, hashable)) – Given edge.

Returns

Edge weight.

Return type

float

abstract edges()

Returns all edges of graph.

Returns

Edges of graph.

Return type

list of (hashable, hashable)

abstract has_edge(edge)

Returns whether an edge exists.

Parameters

edge ((hashable, hashable)) – Given edge.

Returns

True if edge exists, False otherwise.

Return type

bool

abstract has_node(node)

Returns whether the requested node exists.

Parameters

node (hashable) – Given node identifier.

Returns

True if node exists, False otherwise.

Return type

bool

abstract neighbors(node)

Return all nodes that are directly accessible from given node.

Parameters

node (hashable) – Given node identifier.

Returns

Nodes directly accessible from given node.

Return type

list of hashable

abstract nodes()

Returns all nodes of graph.

Returns

Nodes of graph.

Return type

list of hashable