Skip to content

Commit

Permalink
Merge pull request #21 from dpriskorn/check_relations_for_wikidata_qi…
Browse files Browse the repository at this point in the history
…d_before_suggesting

Check relations for wikidata qid before suggesting them
  • Loading branch information
dpriskorn authored Jul 14, 2023
2 parents ef12c4b + 7b80e91 commit 1b3a79a
Show file tree
Hide file tree
Showing 7 changed files with 904 additions and 196 deletions.
994 changes: 827 additions & 167 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ questionary = "^1.10.0"
python-dateutil = "^2.8.2"
wikibaseintegrator = "^0.12.3"
pydash = "^7.0.4"
osmpythontools = "^0.3.5"


[tool.poetry.group.dev.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_level = DEBUG
71 changes: 48 additions & 23 deletions src/models/trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,28 @@ def waymarked_hiking_trails_search_url(self):

def __convert_waymarked_results_to_choices__(self):
for result in self.waymarked_results:
title = f"{result.name}"
if result.id:
title += f" ({result.id})"
if result.ref:
title += f", ref: {result.ref}"
if result.number_of_subroutes:
title += f", subroutes #: {result.number_of_subroutes}"
if result.names_of_subroutes_as_string:
title += f", subroutes: {result.names_of_subroutes_as_string}"
# if result.description:
# title += f", description: {result.description}"
if result.group:
title += f", group: {result.group}"
if result.itinerary:
title += f", itinerary: {', '.join(result.itinerary)}"
choice = Choice(
title=textwrap.fill(title, 100),
value=QuestionaryReturn(osm_id=result.id),
)
self.choices.append(choice)
# We only want choices which are missing a Wikidata tag
if not result.already_has_wikidata_tag:
title = f"{result.name}"
if result.id:
title += f" ({result.id})"
if result.ref:
title += f", ref: {result.ref}"
if result.number_of_subroutes:
title += f", subroutes #: {result.number_of_subroutes}"
if result.names_of_subroutes_as_string:
title += f", subroutes: {result.names_of_subroutes_as_string}"
# if result.description:
# title += f", description: {result.description}"
if result.group:
title += f", group: {result.group}"
if result.itinerary:
title += f", itinerary: {', '.join(result.itinerary)}"
choice = Choice(
title=textwrap.fill(title, 100),
value=QuestionaryReturn(osm_id=result.id),
)
self.choices.append(choice)

def __remove_waymaked_result_duplicates__(self):
self.waymarked_results = list(set(self.waymarked_results))
Expand Down Expand Up @@ -166,7 +168,7 @@ def __parse_not_found_in_osm_last_update_statement__(self):
else:
print("No not found in-property with a know value found")
except KeyError:
logger.info("No not found in-claims on this item")
logger.info("No 'not found in'-claims on this item")

def __lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__(
self,
Expand Down Expand Up @@ -253,6 +255,7 @@ def enrich_wikidata(self):
if self.item:
if self.chosen_osm_id:
self.__add_osm_id_to_item__()
self.__remove_not_found_in_osm_claim__()
self.summary = (
"Added match to OpenStreetMap via "
"the [[Wikidata:Tools/hiking trail matcher"
Expand Down Expand Up @@ -413,7 +416,9 @@ def __add_osm_id_to_item__(self):
prop_nr=Property.OSM_RELATION_ID.value,
value=str(self.chosen_osm_id),
references=[self.__create_heuristic_reference__()],
)
),
# Replace no-value statement if it exists
action_if_exists=ActionIfExists.REPLACE_ALL,
)
else:
# We got it from OSM Wikidata Link so add a reference
Expand All @@ -430,7 +435,9 @@ def __add_osm_id_to_item__(self):
prop_nr=Property.OSM_RELATION_ID.value,
value=str(self.chosen_osm_id),
references=References().add(reference=reference),
)
),
# Replace no-value statement if it exists
action_if_exists=ActionIfExists.REPLACE_ALL,
)

def time_to_check_again(self, testing: bool = False) -> bool:
Expand Down Expand Up @@ -497,3 +504,21 @@ def __remove_osm_relation_no_value_claim__(self):
self.item.claims.remove(Property.OSM_RELATION_ID.value)
except KeyError:
logger.debug("No OSM_RELATION_ID found on this item to clean up")

def __remove_not_found_in_osm_claim__(self):
try:
claims = self.item.claims.get(Property.NOT_FOUND_IN.value)
if claims:
if len(claims) > 1:
# todo iterate and remove only the right one
console.print(claims)
raise NotImplementedError(
"removing only one of "
"multiple not-found-in-"
"statements is not supported yet"
)
else:
logger.info("Removing 'not found in'-claim")
self.item.claims.remove(Property.OSM_RELATION_ID.value)
except KeyError:
logger.debug("No NOT_FOUND_IN found on this item to remove")
13 changes: 13 additions & 0 deletions src/models/waymarked_result.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from typing import Dict, List

from OSMPythonTools.api import Api # type: ignore
from pydantic import BaseModel
from requests import Session

Expand Down Expand Up @@ -78,3 +79,15 @@ def names_of_subroutes_as_string(self) -> str:
return ", ".join(self.__names_of_subroutes__)
else:
return ""

@property
def already_has_wikidata_tag(self) -> bool:
"""This check uses the Openstreetmap API because it is fast"""
api = Api()
relation = api.query(f"relation/{self.id}")
wikidata = relation.tag("wikidata")
logging.debug(f"wikidata tag: {wikidata}")
if wikidata is None:
return False
else:
return True
9 changes: 4 additions & 5 deletions tests/test_trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ def test_remove_duplicates(self):

def test_convert_to_choices(self):
trail_item = TrailItem(wbi=WikibaseIntegrator())
trail_item.waymarked_results.append(WaymarkedResult(name="test", id=1))
trail_item.waymarked_results.append(WaymarkedResult(name="test2", id=2))
trail_item.waymarked_results.append(WaymarkedResult(name="test", id=10528596))
trail_item.__convert_waymarked_results_to_choices__()
assert len(trail_item.choices) == 2
assert trail_item.choices[0].title == "test (1)"
assert trail_item.choices[0].value.osm_id == 1
assert len(trail_item.choices) == 1
assert trail_item.choices[0].title == "test (10528596)"
assert trail_item.choices[0].value.osm_id == 10528596

def test_lookup_in_osm_wikidata_link_api_no_match(self):
trail_item = TrailItem(wbi=WikibaseIntegrator(), qid="Q820225")
Expand Down
10 changes: 10 additions & 0 deletions tests/test_waymarked_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ def test_number_of_subroutes(self):
wr = WaymarkedResult(id=1014050, name="skåneleden") # skåneleden
wr.get_details()
assert wr.number_of_subroutes == 6

def test_already_has_wikidata_tag_true(self):
wr = WaymarkedResult(id=1014050, name="skåneleden") # skåneleden
wr.get_details()
assert wr.already_has_wikidata_tag is True

def test_already_has_wikidata_tag_false(self):
wr = WaymarkedResult(id=10528596, name="test") # östra leden på kebnekaise
wr.get_details()
assert wr.already_has_wikidata_tag is False

0 comments on commit 1b3a79a

Please sign in to comment.