ABCDEFGHIJKLMNOPQRSTUVWXYZ
1
第36回SWO研究会-DBpediaシンポジウム-のチュートリアルで利用した「DBpedia JapaneseへのSPARQLクエリ」の事例集です.
2
※研究会の詳細については,こちらをご覧下さい→https://sites.google.com/site/sigswo15/papers/36program
3
※ライセンスは「パブリックドメイン」です.
4
※「コメントのみ」可能としていますので,何かお気づきの点がありましたら,コメントをお願いします.
5
6
基本的なSPARQLクエリ
7
すべてトリプルを取得select *
where {
?s ?p ?o .
}
LIMIT 100
8
「東京都を主語(Subject)に含む」トリプルの述語(?p)と目的語(?o)を取得する
select distinct ?p ?o
where {
<http://ja.dbpedia.org/resource/東京都> ?p ?o .
}
LIMIT 100
9
「ラベルに“大阪”を含む」トリプルの主語(?s)
select distinct ?s where {
?s <http://www.w3.org/2000/01/rdf-schema#label>  "大阪"@ja .
}LIMIT 100
10
「ラベルが“大阪”と一致する」トリプルの主語(?s)につながっている述語(?p)と目的語(?o)
select distinct ?s where {
?s <http://www.w3.org/2000/01/rdf-schema#label> ?o
FILTER(regex(str(?o), "大阪" )) .
}LIMIT 100
11
12
PREFIXの利用
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?p ?o where {
?s rdfs:label "大阪"@ja .
?s ?p ?o.
}LIMIT 100
13
主語が同じ時の省略表現
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?p ?o where {
?s rdfs:label "大阪"@ja ;
?p ?o.
}LIMIT 100
14
いろいろな組み合わせを試す
15
大阪府を主語とするトリプル一覧PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct * where {
dbpedia-ja:大阪府 ?p ?o.
}
16
大阪府が持つプロパティ一覧PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct ?p where {
dbpedia-ja:大阪府 ?p ?o.
}
17
大阪府の隣接都道府県PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX prop-ja: <http://ja.dbpedia.org/property/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>

select distinct * where {
dbpedia-ja:大阪府 prop-ja:隣接都道府県 ?o.
}
18
不要なプロパティをFILTERで除外PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX prop-ja: <http://ja.dbpedia.org/property/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>

select distinct * where {
dbpedia-ja:大阪府 ?p ?o.
FILTER(?p != dbpedia-owl:wikiPageWikiLink) .
}
19
OFFSETの利用PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct ?p ?o
where {
dbpedia-ja:東京都 ?p ?o .
}
LIMIT 10
OFFSET 10
20
21
タイプ一覧の取得PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

select distinct ?o where {
?s rdf:type ?o.
}LIMIT 100
22
都道府県の一覧を取得PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>

select distinct ?s where {
?s rdf:type dbpedia-owl:Place.
?s dcterms:subject category-ja:日本の都道府県.
}LIMIT 100
23
郵便番号でソートPREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>

select distinct ?s ?o where {
?s rdf:type dbpedia-owl:Place;
dcterms:subject category-ja:日本の都道府県;
     dbpedia-owl:postalCode   ?o.

}ORDER BY ?o
24
郵便番号でソートPREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>

select distinct ?s ?o where {
?s rdf:type dbpedia-owl:Place;
dcterms:subject category-ja:日本の都道府県;
dbpedia-owl:postalCode ?o.

}ORDER BY DESC(?o)
25
26
大阪府の隣接都道府県の数PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX prop-ja: <http://ja.dbpedia.org/property/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>

select distinct count(?o) where {
dbpedia-ja:大阪府 prop-ja:隣接都道府県 ?o.
}
27
日本の都道府県を「隣接自治体の数」でソートPREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>

select distinct ?s count(?o) AS ?c where {
?s rdf:type dbpedia-owl:Place;
dcterms:subject category-ja:日本の都道府県;
prop-ja:隣接都道府県 ?o.
}ORDER BY ?c
28
各都道府県で生まれた政治家の数PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>

select distinct ?pref (count(?s) AS ?c) where {
?pref rdf:type dbpedia-owl:Place.
?pref dcterms:subject category-ja:日本の都道府県.
?s rdf:type dbpedia-owl:Politician;
dbpedia-owl:birthPlace ?pref.
}GROUP BY ?pref
ORDER BY ?c
29
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>

select distinct ?pref ?s where {
?pref rdf:type dbpedia-owl:Place.
?pref dcterms:subject category-ja:日本の都道府県.
?s rdf:type dbpedia-owl:Politician;
dbpedia-owl:birthPlace ?pref.
}ORDER BY ?pref
30
「出生地が大阪府」または「出生地が東京都」の政治家PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>

select distinct ?s ?pref where {
{?s rdf:type dbpedia-owl:Politician;
dbpedia-owl:birthPlace dbpedia-ja:大阪府.}
UNION{?s rdf:type dbpedia-owl:Politician;
dbpedia-owl:birthPlace dbpedia-ja:東京都.}
?s dbpedia-owl:birthPlace ?pref.
}ORDER BY ?pref
31
32
33
34
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbp: <http://dbpedia.org/ontology/>
select distinct * where {
<http://ja.dbpedia.org/resource/大阪府> dbp:wikiPageWikiLink ?o.
}LIMIT 100
35
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
select distinct * where {
?s rdfs:label "大阪府"@ja ;
?p ?o.
FILTER(?p != dbpedia-owl:wikiPageWikiLink) .
}
36
37
ハンズオンでの成果
38
都道府県ごとの、日本のアイドルの人数PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>

select distinct ?pref (count(?s) AS ?c) where {
?pref rdf:type dbpedia-owl:Place.
?pref dcterms:subject category-ja:日本の都道府県.
?s rdf:type dbpedia-owl:Person;
dcterms:subject category-ja:日本のアイドル;
dbpedia-owl:birthPlace ?pref.
}GROUP BY ?pref
ORDER BY ?c
39
40
ウィキペディアに掲載された日本人(nationalityがJapan)の生誕年ごとの人数集計PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>

SELECT ?year COUNT(?s) WHERE {
?s dbpedia-owl:nationality dbpedia-ja:Japan ;
dbpedia-owl:birthDate ?date.
}
GROUP BY (year(?date) AS ?year)
ORDER BY ?year
41
生誕年の新しい順
昔の人が2020年生まれになっている
select distinct ?s ?date where {
?s dbpedia-owl:birthDate ?date.
}
ORDER BY desc(year(?date))
LIMIT 100
42
日本百名山の標高順
select *
where
{
?uri dcterms:subject category-ja:日本百名山.
?uri rdf:type schema:Mountain.
?uri foaf:name ?name.
?uri foaf:depiction ?image.
?uri prop-ja:標高 ?altitude.
}
order by ?altitude
43
戦国時代・安土桃山時代の戦に参加した武将(回数順) PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
SELECT DISTINCT ?person count(?war) AS ?c WHERE {
{?war dcterms:subject category-ja:日本の戦国時代の戦い}
UNION
{?war dcterms:subject category-ja:安土桃山時代の戦い}
?war dbpedia-owl:opponents ?person .
}
GROUP BY ?person
ORDER BY DESC(?c)
44
45
46
国立国会図書館典拠データ検索・提供サービス
http://id.ndl.go.jp/auth/ndla/?query=
「学会」を含む組織の創設年度順列挙
西村@産総研
select distinct * where{
?id <http://xmlns.com/foaf/0.1/name> ?name.
?id a <http://xmlns.com/foaf/0.1/Organization>;
<http://RDVocab.info/ElementsGr2/dateOfEstablishment> ?date;
<http://RDVocab.info/ElementsGr2/corporateHistory> ?history.
filter (regex(str(?name), "学会"))
}
order by ?date
47
テスト tab
tab
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100