Knowledge Graph

The information can be saved into a local rdf-file or a Virtuoso-database.

class src.knowledge_graph.graph.Graph(storage_type: str = 'memory', memory_path: str = 'models/store', virtuoso_url: Optional[str] = None, virtuoso_graph: Optional[str] = None, virtuoso_username: Optional[str] = None, virtuoso_password: Optional[str] = None, dbpedia_csv: Optional[str] = None, wikidata_csv: Optional[str] = None)[source]

Bases: object

Links new videos with their entities in a knowledge graph and allows to look up user queries.

get_scenes_from_video(identifier: str)[source]

Returns all scenes for a video

Parameters

identifier (str) – Identifier of the video on YouTube

Returns

Returns a list of the scenes with a scene_uri, entity, start and end

Return type

scenes (list)

get_scenes_with_entity(identifier: str)[source]

Returns all scenes for an entity

Parameters

identifier (str) – Can be the name of the entity or a dbpedia/wikidata link.

Returns

Returns a list of the videos in which a entity occurs. Format: [[<link>, <title>], …]

Return type

scenes (list)

get_videos_with_filters(query: str, filters: str)[source]

Returns videos for a user specific query.

Example: select distinct ?title ?link ?dbpedia_entity where { ?scene a video:Scene; foaf:depicts ?dbpedia_entity; video:sceneFrom ?video. ?video dc:identifier ?link; dc:title ?title.

service <http://dbpedia.org/sparql> { ?dbpedia_entity dbo:birthDate ?date; owl:sameAs ?wikidata_entity }

service <https://query.wikidata.org/sparql> { ?wikidata_entity <http://www.wikidata.org/prop/direct/P21> ?sex . ?sex rdfs:label ?sex_label }

filter (regex(str(?wikidata_entity), “www.wikidata.org”) && (?sex_label = “male”@en) && ?date < “19700101”^^xsd:date) }

Parameters
  • query (str) – Further query details which are being inserted into the main query.

  • filters (str) – Allows the specification of filters to apply in the query.

Returns

Returns a list of the scenes in which a entity occurs. Format: [[<title>, <link>, <dbpedia_entity>, <start>, <end>], …]

Return type

scenes (list)

insert_scene(entities: list, youtube_id: str, start_time: datetime.timedelta, end_time: datetime.timedelta)[source]

Creates the link between an entity and a video from youtube.

Parameters
  • entities (list) – Names of the occurring entities.

  • youtube_id (str) – Id of a youtube video to be linked. For example a4T5ylNQk6g for https://www.youtube.com/watch?v=a4T5ylNQk6g.

  • start_time (timedelta) – Start time of the scene in the respective video.

  • end_time (timedelta) – End time of the scene in the respective video.

insert_video(youtube_id: str, title: str)[source]

Creates the rdf triples for a new video.

Parameters
video_exists(youtube_id: str) bool[source]

Returns whether a video is already in the graph or not

Parameters

youtube_id (str) – Id of the YouTube-Video.

Returns

Whether it exists or not.

Return type

exists (bool)

class src.knowledge_graph.memory_store.MemoryStore(path: str = './models/store')[source]

Bases: object

Implementation to store and query rdf-triples in a local file.

commit()[source]

Make the changes persistent and available

exists(youtube_id: str) bool[source]

Checks if a video already exists in the graph

Parameters

youtube_id (str) – The id of the video to check.

insert(triple: tuple)[source]

Add a triple to the knowledge graph

Parameters

triple (tuple) – The triple to be saved.

query(query: str) list[source]

Execute a SPARQL query on a local file

Parameters

query – (str): A valid SPARQL query.

Returns

Returns a list of lists with the queried properties. Format: [[<property1>, <property2>, …], …]

Return type

results (list)

class src.knowledge_graph.virtuoso_store.VirtuosoStore(conn_url: str = 'http://localhost:8890/sparql-auth', graph: str = 'http://localhost:8890/DAV/', username: str = 'dba', password: str = 'dba')[source]

Bases: object

Implementation to store and query rdf-triples in a Virtuoso instance

commit()[source]

Make the changes persistent and available

exists(youtube_id) bool[source]

Checks if a video already exists in the graph

Parameters

youtube_id (str) – The id of the video to check.

insert(triple: tuple)[source]

Add a triple to the knowledge graph

Parameters

triple (tuple) – The triple to save.

query(query: str) list[source]

Execute a SPARQL query with a Virtuoso endpoint

Parameters

query – (str): A valid SPARQL query.

Returns

Returns a list of lists with the queried properties. Format: [[<property1>, <property2>, …], …]

Return type

results (list)