How to get all property from an item based on its Q number? - sparql

I want to gather all properties of a item from wikidata.
All queries I see so far assume you know properties you are looking for, but in my case, I'm not.
For example, when querying for Q1798740, I would like a returned value that looks like
[{"item": "Q1798740",
"P31": ["Q1146"],
"P17": ["Q70972"],
...
"P2043":"70 metres"}
]
and that contains all statements from the wikidata page
What query should I perform?

You need only to ask for {wd:Q1798740 ?p ?value} but it would be useful also to get the labels of the properties, which is a bit trickier:
SELECT DISTINCT ?p ?property_label ?value
WHERE
{
wd:Q1798740 ?p ?value .
?property wikibase:directClaim ?p ;
rdfs:label ?property_label .
FILTER(LANG(?property_label)="en")
}

Related

i want to get the names of similar types using sparql queries from dbpedia

I need to find the names of similar types from DBpedia so I'm trying to figure out a query which can return me the names of entities which have same subject type in its dct:subject (example I want to find similar types of white house so i want to write a query for same . I'm considering the dct:subject to find them ). If there is any other approach please mention it
Previously I tried it for rdf:type but the result are not so good and some time it shows time out
I have done my problem by the query mentioned below and now i want to consider dct:subject instead of rdf:type
select distinct ?label ?resource count(distinct ?type) as ?score where {
values ?type { dbo:Thing dbo:Organization yago:WikicatIslam-relatedControversies yago:WikicatIslamistGroups yago:WikicatRussianFederalSecurityServiceDesignatedTerroristOrganizations yago:Abstraction100002137 yago:Act100030358 yago:Cabal108241798 yago:Group100031264 yago:Movement108464601 yago:PoliticalMovement108472335
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)

SPARQL list literals of selected OWL Datatype

How to list all literals of only selected OWL datatype (here :VisibilityKind) in SPARQL? The OWL datatype definition of VisibilityKind is:
DatatypeDefinition( :VisibilityKind
DataOneOf( "public"^^xsd:string "private"^^xsd:string "package"^^xsd:string )
)
With SPARQL, you can get all the literals that have a certain datatype IRI. For instance, assuming you have data with literals such as "public"^^:VisibilityKind, you can write:
SELECT ?literal WHERE {
?s ?p ?literal .
FILTER (datatype(?literal) = :VisibilityKind)
}
But this is certainly not what you want because you probably do not have literals with this datatype IRI. What you would probably like is something that could roughly be formulated as:
SELECT ?literal WHERE {
?s ?p ?literal .
FILTER (isInDatatype(?literal,:VisibilityKind))
}
where isInDatatype checks if the value associated with ?o belongs to the value space of :VisibilityKind. However, such function does not exist in standard SPARQL 1.1. Moreover, even if it existed, it would require the query engine to understand the semantics of OWL 2 and do reasoning. So you would need a SPARQL engine that implements the OWL 2 DL entailment regime and you'd have to hack a bit the query to get what you want:
SELECT ?literal WHERE {
?s ?p ?literal .
[] a :VisibilityKind;
:owl:sameAs ?literal;
FILTER (isLiteral(?literal))
}
If you want to find implementations of SPARQL 1.1 Entailment regimes, you could take a look at the implementation reports collected by the SPARQL working group.
To answer directly, you need to search for all values in the owl:oneOf list:
SELECT ?resource ?value
WHERE {
:VisibilityKind owl:equivalentClass / owl:oneOf / ( rdf:rest*/rdf:first ) ?value .
?resource ?prop ?value .
}
However, there is a caveat - other properties could have the value "public" "private" or "package". To ensure that you're getting only the values of :VisibilityKind, define that datatype as a range of properties that use the datatype:
{ :someProp rdfs:domain :VisibilityKind }
Then use the following query to get all properties that define :VisibilityKind in the range:
SELECT ?resource ?value
WHERE {
?prop rdfs:range :VisibilityKind .
?resource ?prop ?value
}

sparql how to count variable pairs

I have the following query that gets instances of a class and their label/names. I want to count how many total results there are. However, I do not know how to formulate the count statement.
select ?s ?l {
?s a <http://dbpedia.org/ontology/Ship> .
{?s <http://www.w3.org/2000/01/rdf-schema#label> ?l}
union
{?s <http://xmlns.com/foaf/0.1/name> ?l}
}
I have tried
select ?s ?l (count (?s) as ?count) {
?s a <http://dbpedia.org/ontology/Ship> .
{?s <http://www.w3.org/2000/01/rdf-schema#label> ?l}
union
{?s <http://xmlns.com/foaf/0.1/name> ?l}
}
But that gives the counting for each ?s ?l pair, instead I need to know how many of the ?s ?l pairs there are. Or maybe I should not use count at all? As mentioned all I need to know is how many results in total a query returns (regardless of the hard limit that is put by the server, e.g., DBPedia returns a maximum of 50000 results for each query).
Any suggestions please?
Many thanks!
To count the number of matches, use
SELECT (COUNT(*) AS ?count)
WHERE {
?s <http://www.w3.org/2000/01/rdf-schema#label> | <http://xmlns.com/foaf/0.1/name> ?l .
}
Note I'm using the property path "or" (|) to get the union of the properties.

sparql to retrieve the value of a min constraint

How can I retrieve a min constraint on a class' attribute using sparql? I have value min 1000 decimal, and I would like to get 1000
In a hypothetical world that you have such a statement:
Class: X subClassOf: hasObjectProperty min 1 Y
If you write a SPARQL query as:
SELECT *
WHERE {
?s rdfs:subClassOf ?o.
}
You must extract all the refs:subClassOf axioms. However, if you need to precise and know which ones have cardinality restrictions, you need to go further:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix : <http://example.com#>
SELECT *
WHERE {
?s rdfs:subClassOf ?o.
?o ?x ?y.
filter(?s = :X)
}
Among others, you can see the following result:
As you can see, there are 2 relevant items, one is Y and one is the number presented as a non-negative integer. Therefore, one way to get each item is to put a filter for ?x in the SPARQL query and get each one one by one out. For example, filter owl:onClass will give you ?y:
prefix : <http://example.com#>
SELECT *
WHERE {
?s rdfs:subClassOf ?o.
?o owl:onClass ?y.
filter(?s = :X)
Here is the sparql query I used following Artemis' answer
SELECT ?min
WHERE {?s rdfs:subClassOf ?o.
?o owl:minQualifiedCardinality ?min.
FILTER(?s = :value) }
And with jena, I use getLiteral("min").getFloat();

How do I restrict my search in dbpedia using SPARQL to "Persons" only

I am using a query to output the influences of all people listed in wikipedia (where possible). I am using http://dbpedia.org/snorql/. My code so far is:
SELECT *
WHERE {
?p a
<http://dbpedia.org/ontology/Person> .
?p <http://dbpedia.org/ontology/influenced> ?influenced.
}
The problem is that the influenced output includes things like genres and political ideologies. I want to restrict it to only output "people" and "people who were influenced by those people". Thanks in advanced.
Try:
SELECT *
WHERE {
?p a
<http://dbpedia.org/ontology/Person> .
?p <dbpedia-owl:birthYear> ?birthYear.
?p <http://dbpedia.org/ontology/influenced> ?influenced.
?influenced a <http://dbpedia.org/ontology/Person>.
}
EDITED TO ADD BIRTH YEAR