how to extract URI with sparql query - sparql

I need to extract the URIs of a specific resource from DBpedia.
Example, my specific resource is "France", I extracted its label but I don't know how to get its URI.
Here is my query so far :
SELECT ?label
WHERE{
res:France rdfs:label ?label .
FILTER (lang(?label) = 'fr') .}
Thanks for your help !

You need to filter your resource to be able to extract the URI. Try this:
SELECT distinct *
WHERE{
?uri rdfs:label ?label
filter (?uri=res:France && lang(?label) = 'fr')
}

Related

Group Concat using sparql query

Can some one help me how to use group_concat in sparql query. When i am using in the database it is pulling out iri from the data. How to pull the the labels for the objects present in the database using group_concat.
You will usually have IRIs with a label. Now, to return labels only, a query like this will work:
SELECT ?item ?label
WHERE {
?item rdfs:label ?label
}
Note that rdfs:label is conventionally used for labels, but any name can be used - it depends on the data that you have.
Now, imagine we want to find a GROUP_CONCAT of children's names, grouped by mother.
First, the query for the children and labels looks like this:
SELECT ?mother ?child ?label
WHERE {
?mother :hasChild ?child .
?child rdfs:label ?label .
}
Notice that ?mother and ?child will be bound to IRIs here, and ?label to a string.
Now for the GROUP_CONCAT, you will just need a query like:
SELECT ?mother (GROUP_CONCAT(?label; SEPARATOR=", ") AS ?concat)
WHERE {
?mother :hasChild ?child .
?child rdfs:label ?label .
}
GROUP BY ?mother

SPARQL query - multiple OPTIONALS

I want to retrieve the entities of the classes along with some information - if exists (e.g. label, comment, image)
I run the following query:
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?entity ?label ?comment ?image
WHERE {
?entity a ?class .
OPTIONAL {?entity rdfs:label ?label } .
OPTIONAL {?entity schema:image ?image} .
OPTIONAL {?entity rdfs:comment ?comment} .
OPTIONAL {?entity rdfs:label ?label} .
FILTER (langMatches(lang(?comment), "EN") && langMatches(lang(?label), "EN")) .
}
Although, some triples contain this extra information I do not receive them
(even if I re-write and include all the optional statements in one statement)
How could I compose such a query?

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)

How to express equals relation in sparql?

I am trying to use sparql to find all French labels of words that are related to a given English word.
The query I made is something like the following:
SELECT ?extword ?word ?label where {
?word rdfs:label "shark"#eng.
{?extword rdf:type ?word}
UNION
{?word rdf:type ?extword}.
?extword rdfs:label ?label filter(lang(?label)="fra")
}
While it does give me the French labels of the extword as desired, it does not give the French labels of word itself.
What I need is to insert a clause to express the relation that extword is exactly word, like this:
SELECT ?extword ?word ?label where {
?word rdfs:label "shark"#eng.
{?extword = ?word}
UNION
{?extword rdf:type ?word}
UNION
{?word rdf:type ?extword}.
?extword rdfs:label ?label filter(lang(?label)="fra")
}
It turns out to be syntax error. Is it possible to also include the French labels of word itself, without doing another query?
To be clearer, the result I expect is the result from my first query combined with result from the following query:
SELECT ?word ?label where {
?word rdfs:label "shark"#eng.
?word rdfs:label ?label filter(lang(?label)="fra")
}

Filtering results of SPARQL queries

I want to retrieve all 20th century italian novelists' name.
I've written this:
SELECT ?label
WHERE{
?novelist a yago:ItalianNovelists.
?novelist rdfs:label ?label.
FILTER (langMatches(lang(?label), "EN"))
}
ORDER BY ?label
How can i filter the results?
There are several options. Here I'd suggest two:
Use the pattern ?novelist dct:subject dbc:20th-century_novelists, if you can rely on that classification.
SELECT ?label
WHERE{
?novelist a yago:ItalianNovelists;
rdfs:label ?label;
dct:subject dbc:20th-century_novelists.
FILTER (langMatches(lang(?label), "EN"))
}
ORDER BY ?label
Define birth range or a life-span range. Example birth range:
SELECT ?label
WHERE{
?novelist a yago:ItalianNovelists;
rdfs:label ?label;
dbo:birthDate ?date.
BIND (YEAR(?date) AS ?year)
FILTER (langMatches(lang(?label), "EN"))
FILTER (?year > 1882 && ?year < 1972)
}
ORDER BY ?label
With option 2 you will get more results but depending on the range, they might include novelists who haven't published anything in 20th century.
A third option would be the filter by the year of publishing. However, I wouldn't recommend it. First, it will give only results for those for whom such information is currently available in DBpedia, and that subset would most likely be smaller than the one from the first option. Second, depending on how you would define a 20 century novelist, the query results will omit those who wrote a novel in 20 century, which was published in 21.
The language tag is in lowercase, so just replace "EN: with "en". Also, I find the following to be just as effective as using langMatches
FILTER (lang(?label) = "en")