My work is about library book of recommendation systems . that as input I need book Classification ontology . in my ontology classify library books. this classification has 14 categories, beside the sibling classes Author, book, Isbn. Individuals in book class are book’s subject(about 600 subjects) , and individuals in author class are name’s author and also isbn class. I design this ontology with protege 4.1.
also I collected and Have got in part of belong book to categories manually. That a object properties is name “hasSubject” related individual book class with categories. Example book “A” hasSubject Categories “S” and “F” and... As result I want to get the matrix belonging to Book Categories. This is the way that if the book belongs to a categories then get 1 and Otherwise Takes the value 0. Like this:
cat1 cat2 cat3
book1 1 0 0
book2 1 0 1
book3 1 1 0
In this example Expresses that book1 belong to category 1 and Does not belong category 2 and 3. How can I do this work with sparql in protege?
Handling a fixed number of categories
Given data like
you can use a query like
in which certain variables are bound (the particular value to which they are bound doesn't really matter though) based on the whether certain triples are present to get results like these:
Handling an arbitrary number of categories
Here's a solution that seems to work in Jena, though I'm not sure that the specific results are guaranteed. (Update: Based on this answers.semanticweb.com question and answer, it seems that this behavior is not guaranteed by the SPARQL specification.) If we have a little bit more data, e.g., about which things are categories and which are books, e.g.,
then we can run a subquery that selects all the categories in order, and then for each book computes a string indicating whether or not the book is in each category.
This has the output:
which is much closer to the output in the question, and handles an arbitrary number categories. However, it depends on the values of
?category
being processed in the same order for each?book
, and I'm not sure whether that's guaranteed or not.We can even use this approach to generate a header row for the table. Again, this depends on the
?category
values being processed in the same order for each?book
, which might not be guaranteed, but seems to work in Jena. To get a category header, all we need to do is create a row where?book
is unbound, and the value of the?isCat
indicates the particular category:We get this output:
Using some string manipulation, you could shorten the URIs used for the categories, or widen the array entries to get correct alignment. One possibility is this:
which produces this output:
which is almost exactly what you had in the question.