#!/usr/bin/env python3 """ Query Wikidata for Chilean Libraries using SPARQL Uses Wikidata Query Service to find libraries in Chile with their Q-numbers """ import yaml from SPARQLWrapper import SPARQLWrapper, JSON from typing import List, Dict from pathlib import Path def query_chilean_libraries() -> List[Dict]: """Query Wikidata for all libraries in Chile.""" sparql = SPARQLWrapper("https://query.wikidata.org/sparql") # SPARQL query for libraries in Chile # P31 = instance of, P17 = country, Q298 = Chile # Q7075 = library, Q28564 = public library, Q856234 = academic library, etc. query = """ SELECT DISTINCT ?library ?libraryLabel ?cityLabel ?coords ?founded WHERE { # Library types (including subclasses) ?library wdt:P31/wdt:P279* wd:Q7075 . # Located in Chile ?library wdt:P17 wd:Q298 . # Get city/location OPTIONAL { ?library wdt:P131 ?city . } # Get coordinates OPTIONAL { ?library wdt:P625 ?coords . } # Get founding date OPTIONAL { ?library wdt:P571 ?founded . } # Get labels in Spanish and English SERVICE wikibase:label { bd:serviceParam wikibase:language "es,en" . } } ORDER BY ?libraryLabel """ sparql.setQuery(query) sparql.setReturnFormat(JSON) print("πŸ” Querying Wikidata for Chilean libraries...") print(" Endpoint: https://query.wikidata.org/sparql") print() try: results = sparql.query().convert() # type: ignore libraries = [] for result in results["results"]["bindings"]: # type: ignore library_uri = result["library"]["value"] # type: ignore q_number = library_uri.split("/")[-1] library = { "q_number": q_number, "name": result.get("libraryLabel", {}).get("value", ""), # type: ignore "city": result.get("cityLabel", {}).get("value", ""), # type: ignore "founded": result.get("founded", {}).get("value", "")[:4] if "founded" in result else "", # type: ignore "wikidata_url": f"https://www.wikidata.org/wiki/{q_number}" } libraries.append(library) return libraries except Exception as e: print(f"❌ Error querying Wikidata: {e}") return [] def load_chilean_institutions(file_path: Path) -> List[Dict]: """Load Chilean institutions from YAML file.""" with open(file_path, 'r', encoding='utf-8') as f: return yaml.safe_load(f) def normalize_name(name: str) -> str: """Normalize institution name for matching.""" return name.lower().strip().replace("'", "").replace(" ", " ") def find_matches(institutions: List[Dict], wikidata_libraries: List[Dict]) -> List[Dict]: """Find matches between our institutions and Wikidata libraries.""" matches = [] # Filter institutions without Wikidata libraries_without_wd = [ inst for inst in institutions if inst.get('institution_type') == 'LIBRARY' and not any( id_obj.get('identifier_scheme') == 'Wikidata' for id_obj in inst.get('identifiers', []) ) ] print(f"πŸ“Š Matching {len(libraries_without_wd)} institutions against {len(wikidata_libraries)} Wikidata entries...") print() for inst in libraries_without_wd: inst_name = normalize_name(inst['name']) inst_city = inst.get('locations', [{}])[0].get('city', '').lower() for wd_library in wikidata_libraries: wd_name = normalize_name(wd_library['name']) wd_city = wd_library['city'].lower() # Name match strategies name_match = False # Strategy 1: Exact match if inst_name == wd_name: name_match = True # Strategy 2: Partial match (institution name contains Wikidata name or vice versa) elif inst_name in wd_name or wd_name in inst_name: name_match = True # Strategy 3: Key words match (biblioteca + significant word) elif 'biblioteca' in inst_name and 'biblioteca' in wd_name: inst_words = set(inst_name.split()) wd_words = set(wd_name.split()) common_words = inst_words & wd_words # Must share at least 2 significant words beyond "biblioteca" significant_common = common_words - {'de', 'del', 'la', 'el', 'biblioteca', 'library', 'publica', 'pΓΊblica', 'municipal', 'n', 'no'} if len(significant_common) >= 2: # At least 2 significant words name_match = True # Strategy 4: "Biblioteca Nacional Digital" special case if 'nacional' in inst_name and 'digital' in inst_name: if 'nacional' in wd_name and 'digital' in wd_name: name_match = True # Strategy 5: William Mulloy Library special case (English name) if 'william' in inst_name and 'mulloy' in inst_name: if 'william' in wd_name and 'mulloy' in wd_name: name_match = True # City match (flexible - allows partial matches) city_match = False if inst_city and wd_city: if inst_city in wd_city or wd_city in inst_city: city_match = True # Accept match if name matches and either city matches or no city info # Allow special exceptions for well-known institutions allow_match = ( city_match or not wd_city or 'nacional' in inst_name or ('william' in inst_name and 'mulloy' in inst_name) # Easter Island library ) if name_match and allow_match: match = { 'institution': inst, 'wikidata': wd_library, 'name_confidence': 'exact' if inst_name == wd_name else 'partial', 'city_match': city_match } matches.append(match) break # Only take first match per institution return matches def main(): print("=" * 80) print("WIKIDATA SPARQL QUERY - CHILEAN LIBRARIES") print("=" * 80) print() # Query Wikidata wikidata_libraries = query_chilean_libraries() if not wikidata_libraries: print("❌ No results from Wikidata") return print(f"βœ… Found {len(wikidata_libraries)} libraries in Wikidata") print() # Show sample print("Sample results (first 10):") for i, library in enumerate(wikidata_libraries[:10], 1): print(f" {i}. {library['name']} ({library['city']}) β†’ {library['q_number']}") print() # Load our institutions input_file = Path('data/instances/chile/chilean_institutions_batch7_enriched.yaml') institutions = load_chilean_institutions(input_file) print(f"πŸ“– Loaded {len(institutions)} Chilean institutions") libraries_count = sum(1 for i in institutions if i.get('institution_type') == 'LIBRARY') print(f" {libraries_count} are libraries") with_wikidata = sum( 1 for inst in institutions if inst.get('institution_type') == 'LIBRARY' and any( id_obj.get('identifier_scheme') == 'Wikidata' for id_obj in inst.get('identifiers', []) ) ) print(f" {with_wikidata} already have Wikidata") print(f" {libraries_count - with_wikidata} need enrichment") print() # Find matches matches = find_matches(institutions, wikidata_libraries) print("=" * 80) print(f"MATCHING RESULTS: {len(matches)} potential matches found") print("=" * 80) print() # Display matches for i, match in enumerate(matches, 1): inst = match['institution'] wd = match['wikidata'] print(f"{i}. {inst['name']}") print(f" Our city: {inst.get('locations', [{}])[0].get('city', 'Unknown')}") print(f" ↓ MATCH ({match['name_confidence']} name, city: {match['city_match']})") print(f" Wikidata: {wd['name']} ({wd['city']})") print(f" Q-number: {wd['q_number']}") if wd['founded']: print(f" Founded: {wd['founded']}") print() # Export matches to JSON for batch processing output_file = Path('data/instances/chile/wikidata_matches_batch8_libraries.json') import json match_data = [ { 'institution_name': match['institution']['name'], 'institution_city': match['institution'].get('locations', [{}])[0].get('city', ''), 'q_number': match['wikidata']['q_number'], 'wikidata_name': match['wikidata']['name'], 'wikidata_city': match['wikidata']['city'], 'founded': match['wikidata']['founded'], 'confidence': match['name_confidence'], 'city_match': match['city_match'] } for match in matches ] with open(output_file, 'w', encoding='utf-8') as f: json.dump(match_data, f, indent=2, ensure_ascii=False) print(f"πŸ’Ύ Saved {len(matches)} matches to: {output_file}") print() print("🎯 Next step: Review matches and create Batch 8 enrichment script") if __name__ == '__main__': main()