pyrdf2vec.graphs package

Submodules

Module contents

class pyrdf2vec.graphs.KG(location=None, skip_predicates=NOTHING, literals=NOTHING, is_remote=False, *, fmt=None, mul_req=False, skip_verify=False, cache=NOTHING)

Bases: object

Represents a Knowledge Graph.

_entity_hops

Caches the results of asynchronous requests. Defaults to {}.

_entities

Stores the entities. Defaults to set.

_is_remote

True if the Knowledge Graph is in remote, False otherwise. Defaults to False.

_vertices

Stores the vertices. Defaults to set.

_inv_transition_matrix

Contains the parents of vertices. Defaults to defaultdict.

_transition_matrix

Contains the children of vertices. Defaults to defaultdict.

cache

The policy and size cache to use. Defaults to TTLCache(maxsize=1024, ttl=1200).

connector

The connector to use. Defaults to SPARQLConnector.

fmt

The format of the file. It should be used only if the format can not be determined from source. Defaults to None.

literals

The predicate chains to get the literals. Defaults to [].

location

The location of the file to load. Defaults to None.

mul_req

True to allow bundling of SPARQL queries, False otherwise. This attribute accelerates the extraction of walks for remote Knowledge Graphs. Beware that this may violate the policy of some SPARQL endpoint server. Defaults to False.

skip_predicates

The label predicates to skip from the KG. Defaults to set.

skip_verify

To skip or not the verification of existing entities in a Knowledge Graph. Its deactivation can improve HTTP latency for KG remotes. Defaults to False.

add_edge(v1, v2)

Adds a uni-directional edge.

Parameters
  • v1 (Vertex) – The first vertex.

  • v2 (Vertex) – The second vertex.

Return type

bool

Returns

True if the edge has been added, False otherwise.

add_vertex(vertex)

Adds a vertex to the Knowledge Graph.

Parameters

vertex (Vertex) – The vertex to add.

Return type

bool

Returns

True if the vertex has been added, False otherwise.

add_walk(subj, pred, obj)

Adds a walk to the Knowledge Graph.

Parameters
  • subj (Vertex) – The vertex of the subject.

  • pred (Vertex) – The vertex of the predicate.

  • obj (Vertex) – The vertex of the object.

Return type

bool

Returns

True if the walk has been added to the Knowledge Graph, False otherwise.

fetch_hops(vertex)

Fetchs the hops of the vertex from a SPARQL endpoint server and add the hops for this vertex in a cache dictionary.

Parameters

vertex (Vertex) – The vertex to get the hops.

Return type

List[Tuple[Any, Any]]

Returns

The hops of the vertex.

get_hops(vertex, is_reverse=False)

Returns the hops of a vertex.

Parameters
  • vertex (Vertex) – The name of the vertex to get the hops.

  • is_reverse (bool) – If True, this function gets the parent nodes of a vertex. Otherwise, get the child nodes for this vertex. Defaults to False.

Return type

List[Tuple[Any, Any]]

Returns

The hops of a vertex in a (predicate, object) form.

get_literals(entities, verbose=0)

Gets the literals for one or more entities for all the predicates chain.

Parameters
  • entities (List[str]) – The entity or entities to get the literals.

  • verbose (int) – The verbosity level. 0: does not display anything; 1: display of the progress of extraction and training of walks; 2: debugging. Defaults to 0.

Return type

List[List[Union[float, str, Tuple[Union[float, str], ...]]]]

Returns

The list that contains literals for each entity.

get_neighbors(vertex, is_reverse=False)

Gets the children or parents neighbors of a vertex.

Parameters
  • vertex (Vertex) – The vertex.

  • is_reverse (bool) – If True, this function gets the parent nodes of a vertex. Otherwise, get the child nodes for this vertex. Defaults to False.

Return type

Set[Vertex]

Returns

The children or parents neighbors of a vertex.

get_pliterals(entity, preds)

Gets the literals for an entity and a local KG based on a chain of predicates.

Parameters
  • entity (str) – The entity.

  • preds (List[str]) – The chain of predicates.

Return type

List[str]

Returns

The literals for the given entity.

is_exist(entities)

Checks that all provided entities exists in the Knowledge Graph.

Parameters

entities (List[str]) – The entities to check the existence

Return type

bool

Returns

True if all the entities exists, False otherwise.

remove_edge(v1, v2)

Removes the edge (v1 -> v2) if present.

Parameters
  • v1 (Vertex) – The first vertex.

  • v2 (Vertex) – The second vertex.

Return type

bool

Returns

True if the edge has been removed, False otherwise.

class pyrdf2vec.graphs.Vertex(name, predicate=False, vprev=None, vnext=None)

Bases: object

Represents a vertex in a Knowledge Graph.

name
predicate
vnext
vprev