From 79d4e4f595ea9314f6c86d7e1c1f69cfe1633bf3 Mon Sep 17 00:00:00 2001 From: feger <marc.feger@hhu.de> Date: Tue, 7 May 2019 19:10:30 +0200 Subject: [PATCH] Add unchecked files --- src/lib/__init__.py | 0 src/lib/decapper.py | 19 ++++++++++++++++ src/lib/engine.py | 47 ++++++++++++++++++++++++++++++++++++++++ src/lib/ntriple.py | 23 ++++++++++++++++++++ src/lib/reader.py | 32 +++++++++++++++++++++++++++ src/lib/writer.py | 34 +++++++++++++++++++++++++++++ src/wikidata/__init__.py | 0 src/wikidata/keys.py | 15 +++++++++++++ 8 files changed, 170 insertions(+) create mode 100644 src/lib/__init__.py create mode 100644 src/lib/decapper.py create mode 100644 src/lib/engine.py create mode 100644 src/lib/ntriple.py create mode 100644 src/lib/reader.py create mode 100644 src/lib/writer.py create mode 100644 src/wikidata/__init__.py create mode 100644 src/wikidata/keys.py diff --git a/src/lib/__init__.py b/src/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/lib/decapper.py b/src/lib/decapper.py new file mode 100644 index 0000000..295d19f --- /dev/null +++ b/src/lib/decapper.py @@ -0,0 +1,19 @@ +from tokenize import String +from typing import Dict + + +class Decapper(object): + + def __init__(self, value: Dict) -> None: + """ + This class unpacks the value of a result dict. + """ + self.value = value + + def unpack(self) -> String: + """ + This method unpacks the value. + + :return: The value as String + """ + return self.value['value'] diff --git a/src/lib/engine.py b/src/lib/engine.py new file mode 100644 index 0000000..e54d5fb --- /dev/null +++ b/src/lib/engine.py @@ -0,0 +1,47 @@ +from tokenize import String +from typing import Dict + +from SPARQLWrapper import SPARQLWrapper, JSON + +from src.lib.reader import FileReader + + +class SPARQLEngine(object): + + def __init__(self, entrypoint: String, query: String) -> None: + """ + This class sends a SPARQL query to a specific entrypoint. + + :param entrypoint: The entrypoint where the query will be send to. + :param query: The query to be send. + """ + self.entrypoint = entrypoint + self.query = query + self.engine = SPARQLWrapper(self.entrypoint) + self.results = None + + def get_json(self) -> Dict: + """ + This method returns the results as JSON. + + :return: JSON representation of the results. + """ + self.engine.setQuery( + FileReader(self.query).as_string() + ) + self.engine.setReturnFormat(JSON) + self.results = self.engine.query().convert() + return self.results + + def get_json_test(self) -> Dict: + """ + This method returns the results as JSON. + + :return: JSON representation of the results. + """ + self.engine.setQuery( + self.query + ) + self.engine.setReturnFormat(JSON) + self.results = self.engine.query().convert() + return self.results diff --git a/src/lib/ntriple.py b/src/lib/ntriple.py new file mode 100644 index 0000000..1f9ba0d --- /dev/null +++ b/src/lib/ntriple.py @@ -0,0 +1,23 @@ +from tokenize import String + + +class NTriple(object): + + def __init__(self, subject: String, predicate: String, value: String): + """ + This class represents a N-Triple Statement. + """ + self.subject = subject + self.predicate = predicate + self.value = value + + def as_string(self) -> String: + """ + This method returns the triple as a string. + + :return: String version of the triple. + """ + return "<{subject}> <{predicate}> <{value}> ." \ + .format(subject=self.subject, + predicate=self.predicate, + value=self.value) diff --git a/src/lib/reader.py b/src/lib/reader.py new file mode 100644 index 0000000..774a9aa --- /dev/null +++ b/src/lib/reader.py @@ -0,0 +1,32 @@ +import json +from tokenize import String +from typing import Dict + + +class FileReader(object): + + def __init__(self, source: String): + """ + This class can read a file in a certain location. + + :param source: Where is the file stored ? + """ + self.source = source + + def as_string(self) -> String: + """ + This method reads a file at a certain location and returns it as a string. + + :return: The content of the source as a string. + """ + with open(self.source, 'r') as content_file: + return content_file.read() + + def as_json(self) -> Dict: + """ + This method reads a file which contains a json string. + Then it will assemble the josn. + :return: Dict + """ + with open(self.source) as json_file: + return json.load(json_file) diff --git a/src/lib/writer.py b/src/lib/writer.py new file mode 100644 index 0000000..cbe9ea5 --- /dev/null +++ b/src/lib/writer.py @@ -0,0 +1,34 @@ +import json +from tokenize import String + + +class FileWriter(object): + + def __init__(self, destination: String, data) -> None: + """ + This class can write a certain data to a specific location. + + :param destination: Where to store the data. + :param data: The data to be stored. + """ + self.destination = destination + self.data = data + + def as_json(self) -> None: + """ + This method writes the data as json to the destination. + + :return: None + """ + with open(self.destination, 'w+') as outfile: + json.dump(self.data["results"]["bindings"], outfile) + + def as_string(self) -> None: + """ + This method writes the data as a string linewise to the destination. + + :return: None + """ + with open(self.destination, 'w') as out_file: + for d in self.data: + out_file.write(str(d)) diff --git a/src/wikidata/__init__.py b/src/wikidata/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/wikidata/keys.py b/src/wikidata/keys.py new file mode 100644 index 0000000..7382107 --- /dev/null +++ b/src/wikidata/keys.py @@ -0,0 +1,15 @@ +from enum import Enum + + +class ResultKeys(Enum): + movie = 'Movie' + title = 'Title' + director = 'Director' + author = 'Author' + cast = 'Cast' + published = 'Published' + genre = 'Genre' + duration = 'Duration' + description = 'Description' + production_company = 'ProductionCompany' + line_separator = '|' -- GitLab