-1

How can I list properties with their values for any given DBpedia class? I'm new to this and have looked at several other questions on this but I haven't found exactly what I'm looking for.

What I'm trying to do is providing some relevant additional information to topics of conversation I have got from text mining. Say for example the topic of conversation in a certain community is iPhones. I would like to use this word to query the DBpedia page for this word, IPhone, to get an output such as:

Type: Smartphone
Operating System: IOS 
Manufacturer: Foxconn

EDIT:

Using the query from AKSW I can print the p (property?) and o (object?), although I'm still not getting the output I want. Instead of getting something like:

weight: 133.0

I get

http://dbpedia.org/property/weight:133.0

Is there a way to just get the name of the property instead of the DBpedia link?

My Code

Nick
  • 1
  • 3
  • As for how to get the property label instead of the full IRI, see this question: http://stackoverflow.com/questions/28479394/why-does-my-sparql-query-return-the-uri-of-a-resource-instead-of-its-name/ – Jeen Broekstra Jan 10 '17 at 21:17
  • @JeenBroekstra This is exactly what I'm looking for. The label name (with the prefix stripped off). However I have very limited knowledge on this subject and I wonder how I could put this into [the code](http://pastebin.com/2Yn5p1Ab) I have right now. – Nick Jan 10 '17 at 23:12
  • Please link relevant questions and describe how they don't answer your question. – Ben Companjen Jan 25 '17 at 08:20

2 Answers2

2

Classes do not "have" properties with values. Instances (resp. resources or individuals) do have a relationship via a property to some value which can be an individual itself or a literal (or some anonymous instance aka blank node). And instances belong to a class. e.g. Berlin belongs to the class City

What you want is to get all outgoing values of a given resource in DBpedia:

SELECT * WHERE { <http://dbpedia.org/resource/IPhone> ?p ?o }

Alternatively, you can use SPARQL DESCRIBE, which return the data in forms of an RDF graph resp. a set of RDF triples:

DESCRIBE <http://dbpedia.org/resource/IPhone>

This might also return incoming information because it's not really specified in the W3C recommendation what has to be returned.

UninformedUser
  • 8,397
  • 1
  • 14
  • 23
0

As stated by AKSW properties often link to other classes rather than values. If you want all properties and their values, including other classes the the below gives you the label and filters by language (put the language code you need where have put "en").

SELECT DISTINCT ?label ?o
WHERE {
 <http://dbpedia.org/resource/IPhone> ?p ?o.
 ?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
 }

If you don't want any properties that link to other classes, then you only want datatype properties so this code could help:

SELECT DISTINCT ?label ?o
WHERE {
 <http://dbpedia.org/resource/IPhone> ?p ?o.
 ?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
 ?p a owl:DatatypeProperty .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
 }

Obviously this gives you far less information and functionality, but it might just be what you're after?

Edit: In reply to your comment, it is also possible to get the labels for the values, using the same technique:

SELECT DISTINCT ?label ?oLabel
WHERE {
 <http://dbpedia.org/resource/IPhone> ?p ?o.
 ?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
 ?o <http://www.w3.org/2000/01/rdf-schema#label> ?oLabel
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
 }

Note that http://www.w3.org/2000/01/rdf-schema#label is often shortened to rdfs:label by defining prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

So you could also do:

prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

SELECT DISTINCT ?label ?oLabel
WHERE {
 <http://dbpedia.org/resource/IPhone> ?p ?o.
 ?p rdfs:label ?label .
 ?o rdfs:label ?oLabel
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
 }

and get exactly the same result but possibly easier to read.

ChrisUK
  • 66
  • 5
  • The first query did exactly what I wanted for the property labels, can you do this for the values aswell? Get `Reduced_instruction_set_computing` instead of `http://dbpedia.org/resource/Reduced_instruction_set_computing` – Nick Jan 11 '17 at 20:29
  • Edited comment as per your request – ChrisUK Jan 12 '17 at 11:44
  • Great, thanks! There's only 1 problem, now it only takes properties/values that have a label name and skips the ones that have a textual/numerical value such as Weight: 135. Although this is very useful already, is it possible to a union of some sort to also get these properties/values? Also, there are a lot of instances where I get near-duplicates from this query where there is a difference in only there being capital letters or not: "CPU - Samsung" vs "cpu - Samsung". Is there a way to deal with this in the query aswell? Or should I filter them out with python code after the query? – Nick Jan 12 '17 at 17:32
  • There's the optional keyword that can help you: SELECT DISTINCT ?label ?oLabel ?o WHERE { ?p ?o. ?p rdfs:label ?label . OPTIONAL {?o rdfs:label ?oLabel} . FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en")) } – ChrisUK Jan 13 '17 at 20:03
  • If I add the optional keyword i get a KeyError: 'oLabel' after it prints the same list as it did without it. [Code](http://pastebin.com/x6Cn5HCu) – Nick Jan 13 '17 at 22:00
  • So the ?oLabel is where it shortens the links to just the label names and the ?o is for dealing with the ones with textual/numerical values if I'm getting it right. But when I try to print them both I get KeyErrors. Last comment contains a pastebin link to my current code, not sure where it's going wrong. If I use [This code](http://pastebin.com/E9CdPyC6) however I get the ones with textual/numerical values. How can I combine these results? – Nick Jan 13 '17 at 22:17
  • If I combine these results I have what I need for my project, any idea? @ChrisUK – Nick Jan 16 '17 at 02:12