-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
129 lines (117 loc) · 4.78 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
A Flask app to compare entityschema with wikidata items without using SPARQL
"""
from json import JSONDecodeError
import requests
from flask import Flask, request, json
from flask_cors import CORS
from requests import Response
from comparejsonld import CompareJSONLD
from compareshape import CompareShape
from shape import Shape
app = Flask(__name__)
CORS(app)
@app.route("/api")
def v1():
"""
Compares an entityschema with a wikidata item
:return: a response to the query
"""
schema: str = request.args.get("entityschema", type=str)
entity: str = request.args.get("entity", type=str)
if "Lexeme" in entity:
entity = entity[7:]
language: str = request.args.get("language", type=str)
try:
# valid: dict = check_against_pyshexy(schema, entity)
valid: dict = {}
entity_shape: Shape = Shape(schema, language)
comparison: CompareShape = CompareShape(entity_shape.get_schema_shape(), entity, language)
payload: dict = {'schema': schema,
'name': entity_shape.get_name(),
'validity': valid,
'general': comparison.get_general(),
'properties': comparison.get_properties(),
'statements': comparison.get_statements(),
'error': ""}
status: int = 200
except (AttributeError, TypeError, KeyError, IndexError) as exception:
payload: dict = {'schema': "",
'name': "",
'validity': "",
'general': "",
'properties': "",
'statements': "",
'error': "An error has occurred while translating this schema"}
status = 500
print(f"Schema: {schema} - {type(exception).__name__}: {exception}")
response: Response = app.response_class(response=json.dumps(payload),
status=status,
mimetype="application/json")
return response
@app.route("/api/v2")
def v2():
"""
Compares an entityschema with a wikidata item
:return: a response to the query
"""
schema: str = request.args.get("entityschema", type=str)
schema_list: list = schema.split(', ')
entity: str = request.args.get("entity", type=str)
if "Lexeme" in entity:
entity = entity[7:]
language: str = request.args.get("language", type=str)
try:
# valid: dict = check_against_pyshexy(schema, entity)
valid: dict = {}
names: list = []
general: list = []
properties: list = []
statements: list = []
for schema in schema_list:
shape: Shape = Shape(schema, language)
comparison: CompareJSONLD = CompareJSONLD(shape.get_json_ld(), entity, language)
names.append(shape.get_name())
general.append(comparison.get_general())
properties.append(comparison.get_properties())
statements.append(comparison.get_statements())
payload: dict = {'schema': schema_list,
'name': names,
'validity': valid,
'general': general,
'properties': properties,
'statements': statements,
'error': ""}
status: int = 200
except (AttributeError, TypeError, KeyError, IndexError) as exception:
payload: dict = {'schema': "",
'name': "",
'validity': "",
'general': "",
'properties': "",
'statements': "",
'error': "An error has occurred while translating this schema"}
status = 500
print(f"Schema: {schema} - {type(exception).__name__}: {exception}")
response: Response = app.response_class(response=json.dumps(payload),
status=status,
mimetype="application/json")
return response
def check_against_pyshexy(entityschema: str, entity: str):
"""
Checks the entityschema and item against the pyshexy api
:param entityschema: the entityschema E number to be checked
:param entity: The entity Q number to be checked
:return: the response from pyshexy
"""
json_text: dict
url: str = f"https://tools.wmflabs.org/pyshexy/api?entityschema={entityschema}&entity={entity}"
try:
response: Response = requests.get(url)
json_text = response.json()
except JSONDecodeError as exception:
print(f"{type(exception).__name__}: {exception}")
json_text = {}
return json_text
if __name__ == '__main__':
app.run()