I'm using the following query to get the info of a specific Wikidata item.
For example, this one gets the info about the movie Titanic
SELECT ?wd ?wdLabel ?ps ?ps_Label ?wdpqLabel ?pq_Label {
VALUES (?film) {(wd:Q44578)}
?film ?p ?statement .
?statement ?ps ?ps_ .
?wd wikibase:claim ?p.
?wd wikibase:statementProperty ?ps.
OPTIONAL {
?statement ?pq ?pq_ .
?wdpq wikibase:qualifier ?pq .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} ORDER BY ?wd ?statement ?ps_
It works well and I do get the info, but I want to add the items ("Q") beside them.
For example, if the genre is "romance film" I would like to get Q1054574 besides it. And if the actor is Leonardo DiCaprio I would like to get Q38111.
How can I achieve this in this kind of query?
You could add ?ps_ to the SELECT:
SELECT ?wd ?wdLabel ?ps ?ps_Label ?ps_ ?wdpqLabel ?pq_Label
Result: Screenshot
Related
My database structure looks something like this (omitted less-important details):
Hospital{
Name,
Variants[] (references HospitalVariant)
}
HospitalVariant{
DiseaseVariant (references Disease),
Description,
Rating
}
Disease{
Name,
slug
}
Now, I want to fetch all hospitals which treats breast-cancer and I only want to fetch breast-cancer from that hospitals array of diseases.
*[_type="Hospital" && Variants[].DiseaseVariant->slug.current match "breast-cancer"]{
...,
Name,
Variants[DiseaseVariant->slug.current match "breast-cancer"]
}
First part of the query is working correctly i.e. it's fetching the hospitals which treats breast-cancer but in the Diseases array, nothing is being fetched.
If I use a non-referenced field, the query is working correctly. i.e.
*[_type="Hospital" && Variants[].DiseaseVariant->slug.current match "breast-cancer"]{
...,
Name,
Variants[Rating > 95]
}
This is returning the correct results. But when I am using referenced Object (Disease), it's not working correctly.
I'm wondering if it's possible to write a WikiData SparkQL query that can retrieve all entities under a category?
For example: the wikipage of Barak Obama has a bunch of categories including: "African-American Christians", "African-American educators", "African-American feminists", "African-American lawyers"
I'm trying to find a way to select all "humans" what match those categories. The wikidata page of Obama doesnt have any of those categories so I'm not sure how to query this.
Thanks
SELECT * WHERE {
wd:Q76 wdt:P910 ?category .
?link schema:about ?category; schema:isPartOf <https://en.wikipedia.org/>; schema:name ?title .
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "en.wikipedia.org";
wikibase:api "Generator";
mwapi:generator "categorymembers";
mwapi:gcmtitle ?title;
mwapi:gcmprop "ids|title|type";
mwapi:gcmlimit "max".
?member wikibase:apiOutput mwapi:title.
?ns wikibase:apiOutput "#ns".
?item wikibase:apiOutputItem mwapi:item.
}
}
adapted from here
Provided a vector of movies' names, I would like to know their genres querying Wikidata.
Since I am a R user, I have recently discovered WikidataQueryServiceR which has exactly the same example I was looking for:
library(WikidataQueryServiceR)
query_wikidata('SELECT DISTINCT
?genre ?genreLabel
WHERE {
?film wdt:P31 wd:Q11424.
?film rdfs:label "The Cabin in the Woods"#en.
?film wdt:P136 ?genre.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}')
## 5 rows were returned by WDQS
Unfortunately, this query uses a static text, so I would like to replace The Cabin in the Woods by a vector. In order to do, I tried with the following code:
library(WikidataQueryServiceR)
example <- "The Cabin in the Woods" # Single string for testing purposes.
query_wikidata(paste('SELECT DISTINCT ?human ?humanLabel ?sex_or_gender ?sex_or_genderLabel WHERE {
?human wdt:P31 wd:Q5.
?human rdfs:label', example, '#en.
?human wdt:P21 ?sex_or_gender.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
OPTIONAL { ?human wdt:P2561 ?name. }
}', sep = ""))
But that does not work as expected, as I get the following result:
Error in FUN(X[[i]], ...) : Bad Request (HTTP 400).
What am I doing wrong?
Have you tried to output your SPARQL query? —
There is no space after rdfs:label
There are no quotes around The Cabin in the Woods
In your R code, instead of
?human rdfs:label', example, '#en.
line 7 should be:
?human rdfs:label "', example, '"#en.
Although query_wikidata() can accept vector of strings, I'd suggest to use SPARQL 1.1 VALUES instead, in order to avoid too many requests.
library(WikidataQueryServiceR)
example <- c("John Lennon", "Paul McCartney")
values <- paste(sprintf("('%s'#en)", example), collapse=" ")
query <- paste(
'SELECT DISTINCT ?label ?human ?humanLabel ?sexLabel {
VALUES(?label) {', values,
'}
?human wdt:P31 wd:Q5.
?human rdfs:label ?label.
?human wdt:P21 ?sex.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}'
)
query_wikidata(query)
For large number of VALUES, you probably need to use the development verion of WikidataQueryServiceR: it seems that only the development version supports POST requests.
Hi I am trying to get some referenced data from another table,
Data structure:
Table PartDetail
-id
-OperationTypeID(foreign key)
-DateAdded
Table OperationType
-id
-Description
I am trying something like this:
$crud = $this->add('MVCGrid', array('allow_edit'=>false));
$crud->setModel('Model_PartDetail',array('DateAdded'));
But then I want to see the "description" from table OperationType, because on my PartDetail model I declare my relationship like this:
$this->hasOne('OperationType','OperationTypeID','Description')
->mandatory(true)
->caption('Operation Type');
for example in this case I want to see the description from the table OperationType
I tried:
$crud->setModel('Model_PartDetail',array('DateAdded','OperationType'));
but is not working, only works with:
$crud->setModel('Model_PartDetail',array('DateAdded','OperationTypeID'));
but I get only the ID number, not the description.
How this works?
I was able to solved it.
on the model you need to redefine it as
$ref = $this->add('Field_Reference', 'OperationTypeID');
$ref->dereferenced_field='OperationTypeDescription';
$m = $this->add('Model_OperationType');
$m->addField('D'); // <-- actually seems that this line is not working
$ref->setModel($m, 'Description');
And then in the page you can actually added as OperationTypeDescription:
$crud->setModel('Model_PartDetail', array('DateAdded', 'OperationTypeDescription'));
I started using Overbyte components , used to use Indy but blocking issue made me look for something else , so I found ICS , but in this example code:
HTTPAsync
It creates new HTTPCli component for every link inside listbox , but when I change code a bit:
procedure THttpAsyForm.HttpCli1DocData(Sender: TObject; Buffer: Pointer;
Len: Integer);
var
AHttpCli : THttpCli;
begin
if not DataCheckBox.Checked then
Exit;
AHttpCli := Sender as THttpCli;
{ Display a message stating that data is available }
DisplayMemo.Lines.Add('Item ' + IntToStr(AHttpCli.Tag) + ' Data');
{ We could display the data, but it use a huge space in the display }
DisplayMemo.Lines.Add(StrPas(Buffer));
if something then <--- CODE I ADDED
ListBox1.items.add(AHttpCli.URL); <--- CODE I ADDED
{ We could also store the data somewhere (with the help of OnDocBegin }
{ and OnDocEnd events. Or using the RcvdStream property. }
end;
When I put 10 links it sometimes adds duplicates to listbox , or with one link it adds two same links to listbox .
How can I fix it to not show duplicates.My idea was to put it inside tstringlist and check for duplicates and remove them.
Is there any other way.Thanks
You can check to see if it already exists before adding it:
if something then
if ListBox1.Items.IndexOf(AHttpCli.URL) = -1 then
ListBox1.items.add(AHttpCli.URL);