基于DBPedia构建关联疾病与医学专科的SPARQL查询
Great question! Since you're already comfortable pulling a list of diseases from DBpedia, extending that query to include associated medical specialties is straightforward once you know the right property to use.
First, let's break down what you need: DBpedia uses the dbo:medicalSpecialty property to link instances of dbo:Disease to their corresponding dbo:MedicalSpecialty entities. To make the results human-readable (instead of just URIs), we'll also pull in the rdfs:label for both diseases and specialties.
Here's the modified query:
PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?disease ?diseaseLabel ?specialty ?specialtyLabel WHERE { ?disease a dbo:Disease ; dbo:medicalSpecialty ?specialty . ?disease rdfs:label ?diseaseLabel . ?specialty rdfs:label ?specialtyLabel . FILTER(LANG(?diseaseLabel) = "en") FILTER(LANG(?specialtyLabel) = "en") } ORDER BY ?diseaseLabel
Key changes explained:
- Added
dbo:medicalSpecialty ?specialtyto the WHERE clause: this creates the link between each disease and its associated medical specialty. - Included
rdfs:labelfor both?diseaseand?specialty: this returns the human-friendly name instead of just the DBpedia URI (likehttp://dbpedia.org/resource/Diabetes_mellitus). - Added
FILTER(LANG(...)): ensures we only get English labels (adjust the language code if you need labels in another language). - Changed
ORDER BYto?diseaseLabel: sorts results by the readable disease name instead of the URI, which is easier to scan.
Handling diseases without a linked specialty
Some diseases in DBpedia might not have an associated medical specialty yet. If you want to include all diseases (even those without a specialty), use the OPTIONAL clause to make that part of the query non-required:
PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?disease ?diseaseLabel ?specialty ?specialtyLabel WHERE { ?disease a dbo:Disease . ?disease rdfs:label ?diseaseLabel . FILTER(LANG(?diseaseLabel) = "en") OPTIONAL { ?disease dbo:medicalSpecialty ?specialty . ?specialty rdfs:label ?specialtyLabel . FILTER(LANG(?specialtyLabel) = "en") } } ORDER BY ?diseaseLabel
In this version, diseases without a specialty will still appear in the results, with ?specialty and ?specialtyLabel showing as empty values.
内容的提问来源于stack exchange,提问作者eandrews8

