SPARQL output from two different subclasses - sparql

I would like to include the National Party and the EU Party Group of the Speaker in the output, as well as their committee membership at the time of the respective speech.
Data Model
LinkedOpenData I am working with
When I just include the institution (here: ?party), it duplicates the speech for all the positions the speaker has ever had during his/her time in Parliament, rather than the current Committee Position. Furthermore, I have only managed to either get the National Party or the EU Party Group so far (see code below) but not both.
I would be glad for your help.
SELECT ?name ?gender ?partyname ?countryname ?date ?speechnr ?text
WHERE {
?speech lpv:spokenText ?text.
?speech lpv:spokenAs ?function.
?function lpv:institution ?party.
?party rdf:type lpv:NationalParty.
?party rdfs:label ?partyname.
?speech lpv:docno ?speechnr.
?speech dcterms:date ?date.
?speech lpv:speaker ?speaker.
?speaker lpv:name ?name.
?speaker lpv:gender ?gender.
?speaker lpv:countryOfRepresentation ?country.
?country rdfs:label ?countryname.
FILTER ( ?date >= "1999-07-20"^^xsd:date && ?date <= "2004-07-19"^^xsd:date )
FILTER(langMatches(lang(?text), "en"))
FILTER(CONTAINS(?text, 'domestic abuse') || CONTAINS(?text, 'domestic violence') || CONTAINS(?text, 'intimate partner violence') || CONTAINS(?text, 'rape') || CONTAINS(?text, 'sexual violence') || CONTAINS(?text, 'sexual assault') || CONTAINS(?text, 'gender base violence') || CONTAINS(?text, 'gender-based violence') || CONTAINS(?text, 'violence against women') || CONTAINS(?text, 'femicide') || CONTAINS(?text, 'witch-hunting death') || CONTAINS(?text, 'femicide-suicide') || CONTAINS(?text, 'excess female mortality'))
} ORDER BY ?date ?speechnr

Related

SPARQL: UNION drops values, OPTIONAL needs separate variables

How can I get all the labels and aliases for a given country, in both English and the official language(s) of this country, as a single column, from wikidata?
I initially tried the following but UNION seems to drop the labels in the official language:
Attempt #1:
SELECT
?country_iso
(GROUP_CONCAT(DISTINCT(?label) ; separator = ", ") AS ?labels)
WHERE
{
VALUES ?country { wd:Q878 } # FOR TESTING PURPOSES, TO REMOVE ONCE ISSUE RESOLVED.
# country sovereign state unincorporated territory
VALUES ?any_kind_of_country { wd:Q6256 wd:Q3624078 wd:Q783733 }
?country wdt:P31 ?any_kind_of_country . hint:Prior hint:runFirst true .
?country wdt:P297 ?country_iso ; # ISO 3166-1 alpha-2 code
wdt:P37 ?official_lang .
?official_lang wdt:P424 ?wm_lang_code .
{ ?country rdfs:label ?label . FILTER(LANG(?label) = "en") }
UNION
{ ?country rdfs:label ?label . FILTER(LANG(?label) = ?wm_lang_code) }
UNION
{ ?country skos:altLabel ?label . FILTER(LANG(?label) = "en") }
UNION
{ ?country skos:altLabel ?label . FILTER(LANG(?label) = ?wm_lang_code) }
}
GROUP BY ?country_iso
ORDER BY ?country_iso
| country_iso | labels |
|-------------|------------------------------------------------------------------------------------------------------------------|
| AE | United Arab Emirates, ae, 🇦🇪, Emirates, the Emirates, the U.A.E., the UAE, the United Arab Emirates, U.A.E., UAE |
If I use two separate variables ?label and ?altLabel and OPTIONAL, then I seem to get all the labels and aliases, but:
a. I'm not convinced || is right there, although it seems to produce the right results,
b. two columns (?labels and ?altLabels) isn't ideal in my case. (And if I only use one, then only rdfs:label gets returned.)
Attempt #2
SELECT
?country_iso
(GROUP_CONCAT(DISTINCT(?label) ; separator = ", ") AS ?labels)
(GROUP_CONCAT(DISTINCT(?altLabel) ; separator = ", ") AS ?altLabels)
WHERE
{
VALUES ?country { wd:Q878 }
# country sovereign state unincorporated territory
VALUES ?any_kind_of_country { wd:Q6256 wd:Q3624078 wd:Q783733 }
?country wdt:P31 ?any_kind_of_country . hint:Prior hint:runFirst true .
?country wdt:P297 ?country_iso ; # ISO 3166-1 alpha-2 code
wdt:P37 ?official_lang .
?official_lang wdt:P424 ?wm_lang_code .
OPTIONAL { ?country rdfs:label ?label . FILTER(LANG(?label) = "en" || LANG(?label) = ?wm_lang_code) }
OPTIONAL { ?country skos:altLabel ?altLabel . FILTER(LANG(?altLabel) = "en" || LANG(?altLabel) = ?wm_lang_code) }
}
GROUP BY ?country_iso
ORDER BY ?country_iso
| country_iso | labels | altLabels |
|-------------|------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------|
| AE | United Arab Emirates, الإمارات العربية المتحدة | الإمارات, دولة الإمارات العربية المتحدة, ae, 🇦🇪, Emirates, the Emirates, the U.A.E., the UAE, the United Arab Emirates, U.A.E., UAE |
What am I missing? 🤔 Thank you very much in advance for your help! 🙏🏻😊
Disclaimer: relatively new to SPARQL (or rather, very rusty).
Thanks to #UninformedUser (see comments) I was able to come up with this query, yielding 206 results:
SELECT
?country_iso
(GROUP_CONCAT(DISTINCT(?label) ; separator = " | ") AS ?labels)
WHERE
{
# country sovereign state unincorporated territory
VALUES ?any_kind_of_country { wd:Q6256 wd:Q3624078 wd:Q783733 }
?country wdt:P31 ?any_kind_of_country . hint:Prior hint:runFirst true .
?country wdt:P297 ?country_iso ; # ISO 3166-1 alpha-2 code
wdt:P37 ?official_lang .
?official_lang wdt:P424 ?wm_lang_code .
?country rdfs:label|skos:altLabel ?label . FILTER(LANG(?label) = "en" || LANG(?label) = ?wm_lang_code)
}
GROUP BY ?country_iso
ORDER BY ?country_iso
P.S.: An extension of this work, also fetching emoji flags is that query.

Wikidata: Get all living musicians with certain birth date

I am trying to query wikidata to find musicians that share the same birthday, for example as in this snippet where these people share the same birthday (2nd April) and were born after 1900
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT distinct ?name (year(?date) as ?year) WHERE {
?entityS wdt:P569 ?date .
?entityS wdt:P1477 ?name.
FILTER (datatype(?date) = xsd:dateTime)
FILTER (month(?date) = 4) # date
FILTER (day(?date) = 2) # month
FILTER (?date > "1900-01-01"^^xsd:dateTime)
}
(run)
I want to filter by occupation musician i.e. ?professions {wd:Q177220 wd:Q639669} however I am not having much success in applying this filter.
I have tried specifying VALUES ?professions {wd:Q177220 wd:Q639669}, to no effect.
SELECT distinct ?name (year(?date) as ?year) WHERE {
VALUES ?professions {wd:Q177220 wd:Q639669}
?entityS wdt:P569 ?date .
?entityS wdt:P1477 ?name.
?entityS wdt:P106 ?professions
FILTER (datatype(?date) = xsd:dateTime)
FILTER (month(?date) = 4) # date
FILTER (day(?date) = 2) # month
FILTER (?date > "1900-01-01"^^xsd:dateTime)
}

Best way to filter DBpedia results and return a specific results using SPARQL

I have a little problem...
From a list on wikipedia Page Ids, i want to return:
pageid
Abstract ( English)
Thumbnail
type
Only for company OR university page
I have this simple SPARQL query:
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
select ?pageid ?abstract ?thumbnail ?company ?type
where {
?resource dbpedia-owl:wikiPageID ?pageid;
dbpedia-owl:abstract ?abstract
. OPTIONAL {
?resource dbpedia-owl:thumbnail ?thumbnail
}
. OPTIONAL {
?resource dbpedia-owl:type ?type
}
. OPTIONAL {
?resource ?company dbpedia-owl:Company
}
FILTER(
?pageid = 14617 || ?pageid = 26989 || ?pageid = 31776 ||
?pageid = 256913 || ?pageid = 342924 || ?pageid = 1785141 ||
?pageid = 3057187 || ?pageid = 7529378 || ?pageid = 18978754
)
FILTER(langMatches(lang(?abstract),"en"))
}
I have this result: SPARQL Result
I managed to return the data that I wanted except that I cannot filter only that companies and universities.
is there any idea on how to remove pages that are not companies or universities in the dbpedia query results?
#AKSW has provided a query that does what you want, but I think it would be better with some more detailed explanation of why. So, here is that query, with a bunch of whitespace and some inline comments to help clarify --
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?pageid ?abstract ?thumbnail ?type
WHERE
{
# the next couple lines limit rdf:type values
VALUES ?type
{ dbo:Company dbo:University }
# the next few lines limit dbo:wikiPageID values
VALUES ?pageid
{ 14617 26989 31776
256913 342924 1785141
3057187 7529378 18978754 }
# the next few lines get values for the 3 predicates
# you required for each subject
?resource dbo:wikiPageID ?pageid ;
rdf:type ?type ;
dbo:abstract ?abstract .
# the next line gets thumbnails, if they exist
OPTIONAL { ?resource dbo:thumbnail ?thumbnail }
# the next line limits the solutions you receive to those
# with an abstract langtagged as "en"
FILTER ( langMatches ( lang ( ?abstract ), "en" ) )
}

How to build case-insensitive SPARQL filter?

I am trying to build some queries with SPARQL searching by the label field without case sensivity.
I start from this query which works fine:
SELECT distinct ?concepto, ?objeto, ?label WHERE {?concepto rdfs:comment ?objeto. ?concepto rdfs:label ?label. FILTER (lang(?label) = 'es' && ?label='Francisco de Quevedo'#es && (lang(?objeto) = 'es' || lang(?objeto) = 'en'))} LIMIT 100
But if I try to add a FILTER constraint to make the query case insensitive, it doesn't work:
SELECT distinct ?concepto, ?objeto, ?label WHERE {?concepto rdfs:comment ?objeto. ?concepto rdfs:label ?label. FILTER (lang(?label) = 'es' && lcase(str(?label))='francisco de quevedo'#es && (lang(?objeto) = 'es' || lang(?objeto) = 'en'))} LIMIT 100
Diferent options to do queries searching by labels searching general concepts (i.e: Galaxy, Mountain, Francisco de Quevedo). I need to identify only one object that contains this topics and I don't know previously if the label is registered in lower or upper case.
Next code doesn't work (it's a heavy query, and take all the objects that CONTAIN tha word Galaxy:
SELECT distinct ?concepto ?objeto ?label WHERE {?concepto rdfs:comment ?objeto. ?concepto rdfs:label ?label FILTER (lang(?label) = 'es' && (lang(?objeto) = 'es' || lang(?objeto) = 'en') && regex(?label, "Galaxia","i"))} LIMIT 100
This other code works fine, I obtain the correct object but I need to know exactly how it is write (upper or lower case):
SELECT distinct ?concepto ?objeto ?label WHERE { ?concepto rdfs:comment ?objeto. ?concepto rdfs:label ?label FILTER (lang(?label) = 'es' && (lang(?objeto) = 'es' || lang(?objeto) = 'en') && ?label = "Galaxia"#es)} LIMIT 1000
And the last one is ok with the performance but obtain lots of results, I need to obtain only the object Galaxy searching by label in Spanish:
SELECT distinct ?concepto ?objeto ?label WHERE {?concepto rdfs:comment ?objeto. ?concepto rdfs:label ?label. ?label bif:contains '"Galaxia"' FILTER (lang(?label) = 'es' && (lang(?objeto) = 'es' || lang(?objeto) = 'en'))} LIMIT 100
applying STR(?label) results in the language tag being removed so remove the #es from the string your comparing to.

SparqlResultSet giving {"The remote server returned an error: (400) Bad Request."}

This is the code and the query that i am using..
SparqlRemoteEndpoint endpointeds = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org");
SparqlResultSet resultss = endpointeds.QueryWithResultSet("
PREFIX dbp-ont: <http://dbpedia.org/ontology/>
SELECT ?location, str(?label)
{ ?location dbp-ont:country <http://dbpedia.org/resource/Italy> .
?location a ?type filter ( ?type = dbp-ont:ArchitecturalStructure ||
?type=dbp-ont:Place || ?type=dbp-ont:NaturalPlace || ?type=dbp-ont:Mountain ||
?type=dbp-ont:Volcano || ?type=dbp-ont:Village || ?type=dbp-ont:AdministrativeRegion
|| ?type=dbp-ont:Island || ?type=dbp-ont:Continent) .
?location foaf:name ?label filter (lang(?label)=" + "en" + ").} order by ?location limit 10");
I know that the query work well because i have already tried it in Virtuoso, i dont know why it is giving me this error,
working with visual studio 2012 thanks.
See the same question, with answer, at http://answers.semanticweb.com/questions/23254/sparqlresultset-giving-the-remote-server-returned-an-error-400-bad-request