def parse_country(self) -> dict: """ Parse a country entity type Examples: - France: https://www.wikidata.org/wiki/Q142 - United States: https://www.wikidata.org/wiki/Q30 - Ghana: https://www.wikidata.org/wiki/Q117 """ query = read_sparql("get_country.sparql", self.entity_id) response = wikidata_sparql_query(query) return format_country(response)
def parse_landmark(self) -> dict: """ Parse a landmark entity type Examples: - Great Pyramid of Giza: https://www.wikidata.org/wiki/Q37200 - Lincoln Memorial: https://www.wikidata.org/wiki/Q213559 - Mount Rushmore: https://www.wikidata.org/wiki/Q83497 - Eiffel Tower: https://www.wikidata.org/wiki/Q243 - Table Mountain: https://www.wikidata.org/wiki/Q213360 """ query = read_sparql("get_landmark.sparql", self.entity_id) response = wikidata_sparql_query(query) # Format the query return format_landmark(response)
def test_read_replaces_0(self): """ test_read_replaces_0 will check that read_sparql will replace the $0 in the file with a test uri """ # arrange expected = ("SELECT ?label{\n" " wd:Q41513 p:P31 [ps:P31 ?instanceOf].\n" " ?instanceOf rdfs:label ?label\n" " FILTER((LANG(?label)) = \"en\")\n}\n") # act actual = read_sparql("get_instance.sparql", "Q41513") # assert self.assertEqual(expected, actual)
def get_entity_type(self) -> str: """ gets the entity type """ query = read_sparql("get_instance.sparql", self.entity_id) response = wikidata_sparql_query(query) if not response: # Response failed, so return None return None for entry in response['results']['bindings']: entity_type = entry['label']['value'] if entity_type in PERSON_ENTITY_TYPES: return EntityType.PERSON if entity_type in BOOK_ENTITY_TYPES: return EntityType.BOOK if entity_type in COUNTRY_TYPES: return EntityType.COUNTRY if entity_type in LANDMARK_TYPES: return EntityType.LANDMARK raise UnsupportedEntityTypeException("Unsupported entity type")
def parse_book(self) -> dict: """ Parses a book entity type """ query = read_sparql("get_book.sparql", self.entity_id) response = wikidata_sparql_query(query) return format_book(response)
def parse_person(self) -> dict: """ Parses a person entity type """ query = read_sparql("get_person.sparql", self.entity_id) response = wikidata_sparql_query(query) return format_person(response)