Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions entityshape/api_v1/api_v1_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

api_v1 = Blueprint('api_v1', __name__,)


@api_v1.route('/')
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)
schema: str = request.args.get("entityschema", default="", type=str)
entity: str = request.args.get("entity", default="", type=str)
if "Lexeme" in entity:
entity = entity[7:]
language: str = request.args.get("language", type=str)
language: str = request.args.get("language", default="", type=str)
try:
valid: dict = {}
entity_shape: Shape = Shape(schema, language)
Expand All @@ -41,4 +42,4 @@ def v1():
response: Response = Response(response=json.dumps(payload),
status=status,
mimetype="application/json")
return response
return response
29 changes: 22 additions & 7 deletions entityshape/api_v1/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ def _convert_shape(self, shape: str) -> None:
for line in shape_array:
if re.match(r".+:P\d", line):
child: dict = {}
selected_property: str = re.search(r"P\d+", line).group(0)
selected_property: str = ""
match = re.search(r"P\d+", line)
if match:
selected_property = match.group(0)
if shape_json.get(selected_property):
child = shape_json[selected_property]
shape_json[selected_property] = self._assess_property(line, child)
Expand All @@ -109,10 +112,16 @@ def _assess_property(self, line: str, child: dict) -> dict:
"""
snak: str = self._get_snak_type(line)
if "@<" in line:
sub_shape_name: str = re.search(r"<.*>", line).group(0)
sub_shape_name: str = ""
match = re.search(r"<.*>", line)
if match:
sub_shape_name = match.group(0)
child["shape"] = sub_shape_name[1:-1]
if re.search(r"\[.*]", line):
required_parameters_string: str = re.search(r"\[.*]", line).group(0)
required_parameters_string: str = ""
match = re.search(r"\[.*]", line)
if match:
required_parameters_string = match.group(0)
required_parameters_string = required_parameters_string.replace("wd:", "")
if "^" in line:
child["not_allowed"] = required_parameters_string[1:-1].split()
Expand Down Expand Up @@ -208,9 +217,15 @@ def _get_specific_shape(self, shape_name: str) -> str:
% shape_name)
parentheses = self._find_parentheses(self._schema_text)
try:
shape_index: int = re.search(search, self._schema_text).start()
shape_index: int = 0
match = re.search(search, self._schema_text)
if match:
shape_index = match.start()
except AttributeError:
shape_index = re.search("<%s>" % shape_name, self._schema_text).start()
shape_index: int = 0
match = re.search("<%s>" % shape_name, self._schema_text)
if match:
shape_index = match.start()
closest = None
for character in parentheses:
if (character >= shape_index) and (closest is None or character < closest):
Expand Down Expand Up @@ -285,7 +300,7 @@ def _get_cardinality(schema_line: str) -> dict:
cardinality["min"] = 0
elif re.search(r"{.+}", schema_line):
match = re.search(r"{((\d+)|(\d+,\d+))}", schema_line)
if hasattr(match, "group"):
if match is not None and hasattr(match, "group"):
match = match.group()
cardinalities = match[1:-1].split(",")
cardinality["min"] = int(cardinalities[0])
Expand Down Expand Up @@ -339,7 +354,7 @@ def _assess_sub_shape_key(self,
schema_json["required"] = sub_shape_json
if sub_shape[key]["status"] == "statement" and "allowed" in sub_shape[key]:
value = sub_shape[key]["allowed"]
schema_json["required"] = {key: value}
schema_json["required"] = {str(key): value}
if sub_shape[key]["status"] == "qualifier":
qualifier_child[key] = sub_shape[key]
if sub_shape[key]["status"] == "reference":
Expand Down
17 changes: 12 additions & 5 deletions entityshape/api_v2/api_v2_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@

api_v2 = Blueprint('api_v2', __name__,)


@api_v2.route('/')
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)
schema: (str | None) = request.args.get("entityschema", type=str)
schema_list: list[str] = []
if schema:
schema_list = schema.split(', ')
entity: (str | None) = request.args.get("entity", type=str)
if entity is None:
entity = ""
if "Lexeme" in entity:
entity = entity[7:]
language: str = request.args.get("language", type=str)
language: (str | None) = request.args.get("language", type=str)
if language is None:
language = ""
try:
valid: dict = {}
names: list = []
Expand Down Expand Up @@ -52,4 +59,4 @@ def v2():
response: Response = Response(response=json.dumps(payload),
status=status,
mimetype="application/json")
return response
return response
13 changes: 10 additions & 3 deletions entityshape/api_v2/getjsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import requests
from jsonasobj import as_json
from pyshexc.parser_impl.generate_shexj import parse
from pyshexc.parser_impl.generate_shexj import Schema, parse


class JSONLDShape:
Expand All @@ -25,7 +25,14 @@ def get_json_ld(self) -> dict:
Gets the JSON_LD form of the Schema
"""
try:
return json.loads(as_json(parse(self._json_text["schemaText"])))
schema_text: (str | None) = self._json_text.get("schemaText")
if not schema_text:
return {}
parsed_schema: (Schema | None) = parse(schema_text)
if parsed_schema is None:
return {}

return json.loads(as_json(parsed_schema))
except (KeyError, IndexError, AttributeError, ValueError):
return {}

Expand All @@ -51,4 +58,4 @@ def get_name(self) -> str:
return ""
if self._language in self._json_text["labels"]:
return self._json_text["labels"][self._language]
return ""
return ""
3 changes: 1 addition & 2 deletions tests/fixtures/E438.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"id": "E438",
"serializationVersion": "3.0",
"labels": {
"en": "Wikimedia Disambiguation Page",
"ja": "ウィキメディアの曖昧さ回避ページ"
"en": "Wikimedia Disambiguation Page"
},
"descriptions": {
"en": "Entity schema of Wikimedia Disambiguation Page"
Expand Down
2 changes: 0 additions & 2 deletions tests/fixtures/E56.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"en": "Danish verb",
"eo": "dana verbo",
"fr": "verbe danois",
"ja": "デンマーク語の動詞",
"nl": "Deens werkwoord",
"pl": "czasownik duński",
"pt": "verbo (danês)",
Expand All @@ -16,7 +15,6 @@
"descriptions": {
"en": "basic schema for Danish verbs",
"fr": "lexème danois",
"ja": "デンマーク語の動詞を記述するための基本的なスキーマ",
"nl": "basisschema voor een Deens werkwoord",
"pl": "podstawowy schemat czasowników duńskich",
"szl": "bazowy schymat czasowników duńskich"
Expand Down
10 changes: 9 additions & 1 deletion tests/tests_api_v1/test_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def dynamic_mock_response(self, url, *args, **kwargs):

params = kwargs.get('params', {})
action = params.get('action')
target_id: str = ""
if url == "https://www.wikidata.org/w/api.php":
if action == "wbgetentities":
target_id = "names"
Expand All @@ -60,7 +61,7 @@ def dynamic_mock_response(self, url, *args, **kwargs):
# Final fallback
mock_resp.status_code = 404
return mock_resp

def test_specific_wikidata_item_against_schema(self):
"""
Tests a specific entity against a certain schema and checks that
Expand All @@ -73,6 +74,7 @@ def test_specific_wikidata_item_against_schema(self):
value = test_pairs[key]
response = self.app.get(f'/api?entityschema={key}&entity={value}&language=en',
follow_redirects=True)
assert response.json is not None
self.assertIsNotNone(response.json["statements"])
self.assertIsNotNone(response.json["properties"])

Expand All @@ -87,6 +89,7 @@ def test_lexical_category(self):
value = test_pairs[key]
response = self.app.get(f'/api?entityschema={key}&entity={value}&language=en',
follow_redirects=True)
assert response.json is not None
self.assertIsNotNone(response.json["general"]["lexicalCategory"])
self.assertIsNotNone(response.json["general"]["language"])

Expand Down Expand Up @@ -128,6 +131,7 @@ def test_specific_entityschema(self) -> None:
schema: str = "E236"
response = self.app.get(f'/api?entityschema={schema}&entity=Q100532807&language=en',
follow_redirects=True)
assert response.json is not None
self.assertEqual(200, response.status_code)
self.assertEqual("Member of the Oireachtas", response.json["name"])
self.assertEqual({'name': 'occupation', 'necessity': 'required', 'response': 'missing'},
Expand All @@ -146,6 +150,7 @@ def test_entityschema_e236(self):
properties: list = ["P102", "P18", "P31", "P734", "P735", "P39", "P21",
"P27", "P106", "P569", "P4690"]
for prop in properties:
assert response.json is not None
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][prop]["response"], ["correct", "present"])

Expand All @@ -163,6 +168,7 @@ def test_entityschema_e297(self):
self.assertEqual(200, response.status_code)
properties: list = ["P2043", "P2067"]
for prop in properties:
assert response.json is not None
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][prop]["response"], ["correct", "present"])

Expand All @@ -179,6 +185,7 @@ def test_entityschema_e295(self):
self.assertEqual(200, response.status_code)
properties: list = ["P361"]
for prop in properties:
assert response.json is not None
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][prop]["response"], ["too many statements"])
self.assertIn(response.json["properties"][prop]["necessity"], ["absent"])
Expand All @@ -196,6 +203,7 @@ def test_entityschema_e300(self):
self.assertEqual(200, response.status_code)
properties: list = ["P3450"]
for prop in properties:
assert response.json is not None
with self.subTest(prop=prop):
self.assertIn(response.json["properties"][prop]["response"], ["present"])
self.assertIn(response.json["properties"][prop]["necessity"], ["required"])
Expand Down
Loading
Loading