gensim logo

gensim
gensim tagline

Get Expert Help From The Gensim Authors

Consulting in Machine Learning & NLP

• Commercial document similarity engine: ScaleText.ai

Corporate trainings in Python Data Science and Deep Learning

summarization.graph – Graph

summarization.graph – Graph

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

Bases: gensim.summarization.graph.IGraph

Implementation of an undirected graph, based on IGraph.

WEIGHT_ATTRIBUTE_NAME

str – Name of weight attribute in graph.

DEFAULT_WEIGHT

float – Weight set by default.

LABEL_ATTRIBUTE_NAME

str – Default name of attribute. Not used.

DEFAULT_LABEL

str – Label set by default. Not used.

Initializes object.

DEFAULT_LABEL = ''
DEFAULT_WEIGHT = 0
LABEL_ATTRIBUTE_NAME = 'label'
WEIGHT_ATTRIBUTE_NAME = 'weight'
add_edge(edge, wt=1, label='', attrs=None)

Adds an edge to the graph connecting two nodes.

Parameters:
  • edge ((hashable, hashable)) – Given edge.
  • wt (float, optional) – Weight of new edge.
  • label (str, optional) – Edge label.
  • attrs (list, optional) – Node attributes specified as (attribute, value).
Raises:

ValueError – If edge already exists in graph.

add_edge_attribute(edge, attr)

Adds attribute attr to given edge, order of nodes in edge doesn’t matter.

Parameters:
  • edge ((hashable, hashable)) – Given edge.
  • attr (object) – Provided attribute to add.
add_edge_attributes(edge, attrs)

Adds attributes attrs to given edge, order of nodes in edge doesn’t matter.

Parameters:
  • edge ((hashable, hashable)) – Given edge.
  • attrs (list) – Provided attributes to add.
add_node(node, attrs=None)

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.
  • attrs (list of (hashable, hashable), optional) – Node attributes specified as (attribute, value)
Raises:

ValueError – If node already exists in graph.

del_edge(edge)

Removes given edges from the graph.

Parameters:edge ((hashable, hashable)) – Given edge.
del_edge_labeling(edge)

Removes attributes and properties of given edge.

Parameters:edge ((hashable, hashable)) – Given edge.
del_node(node)

Removes given node and its edges from the graph.

Parameters:node (hashable) – Given node.
edge_attributes(edge)

Returns attributes of given edge.

Note

In case of non existing edge returns empty list.

Parameters:edge ((hashable, hashable)) – Given edge.
Returns:Attributes of given edge.
Return type:list
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)
get_edge_properties(edge)

Returns properties of given given edge. If edge doesn’t exist empty dictionary will be returned.

Parameters:edge ((hashable, hashable)) – Given edge.
Returns:Properties of graph.
Return type:dict
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
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
set_edge_properties(edge, **properties)

Adds properties to given edge, order of nodes in edge doesn’t matter.

Parameters:
  • edge ((hashable, hashable)) – Given edge.
  • properties (dict) – Properties to add.
class gensim.summarization.graph.IGraph

Bases: object

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

add_edge(edge, wt=1, label='', attrs=None)

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.
  • label (str, optional) – Edge label.
  • attrs (list, optional) – Node attributes specified as (attribute, value)
add_node(node, attrs=None)

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
  • attrs (list, optional) – Node attributes specified as (attribute, value)
del_node(node)

Removes node and its edges from the graph.

Parameters:node (hashable) – Node to delete.
edge_weight(edge)

Returns weigth of given edge.

Parameters:edge ((hashable, hashable)) – Given edge.
Returns:Edge weight.
Return type:float
edges()

Returns all edges of 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 identifier.
Returns:True if node exists, False otherwise.
Return type:bool
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
nodes()

Returns all nodes of graph.

Returns:Nodes of graph.
Return type:list of hashable