セマンティック・ウェブ

  • ウェブ上のデータをうまく使いましょうというのがセマンティック・ウェブ
  • RDFはその一仕様らしい
  • SPARQLは横断的検索のための検索言語(SPARQL Protocol and RDF Query Language:SQLみたいな)
  • Rには、rrdfというパッケージがある(インストールしたけれど、まだうまく使えない)
library(rrdf)
m1 = load.rdf("sample.rdf")
summarize.rdf(test)
  • これで主語・述語・目的語の三つ組が格納されているようだが…
  • SPARQLというパッケージもある
# 指定したURLにクエリを投げて取り込め
d <- SPARQL(url="http://services.data.gov.uk/reference/sparql",query="SELECT * WHERE { ?s ?p ?o . } LIMIT 10",ns=c('time','<http://www.w3.org/2006/time#>'))
# できるオブジェクトはデータフレーム
is.data.frame(d$results)
  • 中味はこんな感じで、URLの羅列(ちなみにこのURLにブラウザでアクセスすると『アクセス権限なし』と言われる)
> d$results
                                                                                       s
1  <http://api.talis.com/stores/govuk-reference/items/#42fc92705693512993552e234f4b4050>
2  <http://api.talis.com/stores/govuk-reference/items/#d051475f5b2d6124e890acdb0ded459c>
3  <http://api.talis.com/stores/govuk-reference/items/#e111f838133e74bf85f18c76cae9013f>
4  <http://api.talis.com/stores/govuk-reference/items/#6bd61c3ae427f723e81a5a888c332a9a>
5  <http://api.talis.com/stores/govuk-reference/items/#d311b3687e36c127a178fd1b51a7e3c8>
6  <http://api.talis.com/stores/govuk-reference/items/#576e8b7aaca8e539bfb5259aa26270cb>
7  <http://api.talis.com/stores/govuk-reference/items/#1e5335788faac60c5323d72d4b659935>
8  <http://api.talis.com/stores/govuk-reference/items/#bd2e1bf22d3b1ec878996ca18f78fe14>
9  <http://api.talis.com/stores/govuk-reference/items/#b1b32b181fdc387f93c4d8eb9fc54392>
10 <http://api.talis.com/stores/govuk-reference/items/#5cc0b48992effabfd41c37b0bdb66ba5>
                                                   p
1                                  time:hasBeginning
2                                  time:hasBeginning
3                                  time:hasBeginning
4  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
5                                  time:hasBeginning
6                                  time:hasBeginning
7  <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
8                                  time:hasBeginning
9                                  time:hasBeginning
10                                 time:hasBeginning
                                                                         o
1  <http://reference.data.gov.uk/id/gregorian-instant/1983-02-24T00:00:00>
2  <http://reference.data.gov.uk/id/gregorian-instant/2010-05-06T00:00:00>
3  <http://reference.data.gov.uk/id/gregorian-instant/1976-03-04T00:00:00>
4                                                            time:Interval
5  <http://reference.data.gov.uk/id/gregorian-instant/2010-05-06T00:00:00>
6  <http://reference.data.gov.uk/id/gregorian-instant/2010-05-06T00:00:00>
7                                                            time:Interval
8  <http://reference.data.gov.uk/id/gregorian-instant/2010-05-06T00:00:00>
9  <http://reference.data.gov.uk/id/gregorian-instant/1986-05-08T00:00:00>
10 <http://reference.data.gov.uk/id/gregorian-instant/2010-05-06T00:00:00>
> is.data.frame(d$results)
[1] TRUE
> 
||< 
# たぶん、上で、自分用に作ったコネクションに問い合わせ文を投げて、endpointに結果を格納し
# draw a pie chart from data from the Linked Open Piracy data set
endpoint <- "http://semanticweb.cs.vu.nl/lop/sparql/"
q <-"SELECT *WHERE {
?event sem:hasPlace ?place .
?place eez:inPiracyRegion ?region .
}"
# resにその結果から特定の書式で取り出す
prefix <- c("lop","http://semanticweb.cs.vu.nl/poseidon/ns/instances/","eez","http://semanticweb.cs.vu.nl/poseidon/ns/eez/")
res <- SPARQL(endpoint,q,prefix)$results
# Rで描く
pie(sort(table(res$region)),col=rainbow(12))

# draw a stacked bar chart from data from the Linked Open Piracy data set
# 別のクエリを投げる
q <-
"SELECT *
WHERE {
?event sem:eventType ?event_type .
?event sem:hasPlace ?place .
?place eez:inPiracyRegion ?region .
}"
# endpointに結果を返して
res <- SPARQL(endpoint,q,ns=prefix)$results
# Rで処理
restable <- table(res$event_type,res$region)
par(mar=c(4,10,1,1))
barplot(restable,col=rainbow(10),horiz=TRUE,las=1,cex.names=0.8)
legend("topright",rownames(restable),
cex=0.8,bty="n",fill=rainbow(10))