nizam.sp
nizam.sp

Reputation: 4072

In SPARQL, how to find whether one article belongs to a category

I am using basekb to extract some semantic information.

Following sparql query can be taken as a freebase query also (since basekb has freebase also).

Problem statement: to check whether an entity is a subcategory of a some other entity.

Eg: to check en.academy_award_for_best_supporting_actor belongs to the category en.academy_award or not.

prefix basekb: <http://rdf.basekb.com/ns/>
prefix public: <http://rdf.basekb.com/public/>
prefix fbase: <http://rdf.freebase.com/ns/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?aa_ct ?bb {
?aa_ct public:knownAs basekb:en.academy_awards .
?bb public:knownAs basekb:en.academy_award_for_best_supporting_actor .
**?bb rdfs:categoryOf ?aa .**
}

We need to change the text in bold with appropriate syntax (which i am not aware of).

Upvotes: 1

Views: 176

Answers (1)

RobV
RobV

Reputation: 28655

If your SPARQL implementation supports the 1.1 standard you should be able to use property paths for this assuming that the data has a strict category hierarchy e.g.

?bb rdfs:categoryOf+ ?aa .

Property paths gives a simple regex style syntax which can be used against known predicates to say that you want to match where there is a connection between ?bb and ?aa.

In this case the rdfs:categoryOf+ means we require the values for ?bb to be linked to ?aa by a sequence of one or more rdfs:categoryOf links.

Edit

You can achieve an affect akin to property paths using UNION for fixed length simple paths e.g.

{ ?bb rdfs:categoryOf ?aa }
UNION
{ ?bb rdfs:categoryOf [ rdfs:categoryOf ?aa ] }
UNION
{ ?bb rdfs:categoryOf [ rdfs:categoryOf [ rdfs:categoryOf ?aa ] ] }

This will match things where ?bb is a sub-category of ?aa in 1-3 steps, you can expand this as much as necessary up to path length you expect. Performance will likely be poor compared to an implementation that supports property paths.

Upvotes: 1

Related Questions