1

I have the following SPARQL query that supposed to retrieve the common categroies between Category:Computer_science category and Category:German_scientists

SELECT DISTINCT ?subject WHERE {
?subject <http://purl.org/dc/terms/subject> ?cat1, ?cat2 .
?cat1 skos:broader? / skos:broader? / skos:broader?         
  <http://dbpedia.org/resource/Category:Computer_science> .     
?cat2 skos:broader? / skos:broader? / skos:broader?
  <http://dbpedia.org/resource/Category:German_scientists>.
} LIMIT 10

I need to get the closest category to them or who has the shortest path to both categories , How Can I do that ? How to get the lenght between each common categroy between (computer_science) and (German_scientists) for example ?

dewet
  • 31
  • 1
  • 4
  • I'm confused, how is the query (that retrieves articles that are in subcategories of both categories, which is not what you claim it does) related to your question about supercategories? – svick Sep 21 '14 at 18:12
  • Instead of retrieving the articles common between them, I need to retrieve the super category between them and the length to each category in case they have many. I may rewrite the question. – dewet Sep 21 '14 at 18:34
  • Take a look at the results for [\[sparql\] common superclass](http://stackoverflow.com/search?q=%5Bsparql%5D+common+superclass). I think you may find a solution, although you may need to replace some rdfs:subClassOf with skos:broader. – Joshua Taylor Sep 21 '14 at 18:53
  • 1
    **"I need to get the closest category to them or who has the shortest path to both categories"** Can you clarify? What if the lengths are 2 and 2 versus another concept with distances 3 and 1? – Joshua Taylor Sep 21 '14 at 19:01
  • I find what I want on http://stackoverflow.com/questions/19680440/finding-common-superclass-and-length-of-path-in-class-hierarchies/19681697#19681697 thank you , but it can not process the query : Virtuoso 42000 Error TN...: Exceeded 1000000000 bytes in transitive temp memory. How to fix this ? , I am using jena – dewet Sep 21 '14 at 23:24
  • Yes, the DBpedia endpoint has some limitations; that's why I went with the `{0,4}` approach in my question. If you've got a query that you think should work (aside from the Virtuoso transitive temp memory issue), please add it to the question. It will make your question a bit more specific, and as it stands now, we don't know what the *actual* query that you get the warning for is. – Joshua Taylor Sep 22 '14 at 02:00

2 Answers2

3

Virtuoso, the endpoint that serves the DBpedia data supports a non-standard SPARQL extension to property paths: instead of just p*, p+, and p?, to mean a path of length "zero or more", "one or more", and "zero or one", respectively, Virtuoso also supports p{m,n}, meaning a path of length "at least m and at most n". By trying the following query with n=0 and increasing values of m, I started getting results with m=4:

prefix category: <http://dbpedia.org/resource/Category:>

select distinct ?super where {
  ?super (^skos:broader){0,4} category:Computer_science, category:German_scientists
}
super
-----------------------------------------------
http://dbpedia.org/resource/Category:Technology
http://dbpedia.org/resource/Category:Science
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • @dewet The query I posted works if you post it to the web interface at http://dbpedia.org/sparql If you want to use the query somewhere else, you'll need to include the prefix. Check the "namespace prefixes" link at the top right of that page. – Joshua Taylor Sep 22 '14 at 00:57
  • I did thank you , but can you please check the comments for you on the question ? – dewet Sep 22 '14 at 01:23
  • @dewet Ah, I didn't see that comment; if you put "@" followed by someone's name, they get a notification about the comment. – Joshua Taylor Sep 22 '14 at 01:58
  • I can not specify property path in Jena API , (^skos:broader){0,4} only works in endpoint. @Joshua Taylor – dewet Sep 22 '14 at 19:48
  • @dewet yes, as i said, it's not actually legal sparql, it's an extension that virtuoso implements (it was in early drafts, though) – Joshua Taylor Sep 22 '14 at 20:03
  • Is there any method that let me restrict the number of paths to super category ? It keeps shows HttpException: 500 in my program – dewet Sep 22 '14 at 21:11
  • @JoshuaTaylor Hi Joshua... I tried to run the above query..Now the query do not work....Can you please explain? – Rituraj Singh Jul 22 '18 at 10:19
  • @riturajsinghrathore what does "do not work" mean? Does it produce an error? Does it produce results different from what you're expecting? Something else...? – Joshua Taylor Jul 22 '18 at 15:21
  • 1
    @rituraj you may be talking about the "Virtuoso 37000 Error SP030: SPARQL compiler, line 4: Undefined namespace prefix at 'category' before ','" error. The dbpedia public Sparql endpoint changed some of their [predefined namespace prefixes](http://dbpedia.org/sparql?help=nsdecl). It looks like category: should be dbc: now. – Joshua Taylor Jul 22 '18 at 15:24
  • @JoshuaTaylor Thank you for the quick reply. Indeed, you are right. Thanks for – Rituraj Singh Jul 22 '18 at 21:31
0

For the clarification:: The query is the PREFIX version of the below answer which follows current norms :

PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
select distinct ?super where {
  ?super (^skos:broader){0,4} dbc:Computer_science, dbc:German_scientists
} 

SPARQL RESULTS

super
--------------------------------------------
http://dbpedia.org/resource/Category:Science_and_technology
Rituraj Singh
  • 579
  • 1
  • 5
  • 16