forked from zbw/sparql-queries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_concept_by_stw_labels.rq
91 lines (90 loc) · 2.94 KB
/
search_concept_by_stw_labels.rq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# Search a concept by all preferred and alternate labels
# from stw and other linked concepts
# (rank by calculated total score)
#
# Requires a full-text index, as provided by Jena
# (WDQS currently does not support full text search, see
# https://phabricator.wikimedia.org/T141813 -
# mwapi fulltext search seems to work differently)
#
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema: <http://schema.org/>
PREFIX text: <http://jena.apache.org/text#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
#
select distinct ?wd ?wdLabel (group_concat(distinct ?classLabel ;
separator='; ') as ?classes) ?addStw ('add' as ?addStwLabel)
#?labels
#(str(sum(?score)) as ?totalScore)
where {
# get all name variants for search
service <https://zbw.eu/beta/sparql/stw/query> {
select ?stwId (group_concat(distinct ?label) as ?labels)
where {
values ( ?stwId ) {
( "13516-0" )
}
bind(uri(concat('http://zbw.eu/stw/descriptor/', ?stwId)) as ?stw) .
{
?stw skos:prefLabel|skos:altLabel|skos:hiddenLabel ?labelLang
} union {
?stw skos:exactMatch/(skos:prefLabel|skos:altLabel|skos:hiddenLabel) ?labelLang
}
bind(str(?labelLang) as ?label)
}
group by ?stwId
}
# full text search, limit number of results to 20
# (query strings and index without language tags)
(?wd ?score) text:query (?labels 20) .
bind(strafter(str(?wd), str(wd:)) as ?wdId)
#
# add class information, if available
optional {
?wd wdt:P31 ?class .
#
# add class label
?class rdfs:label ?classLabelLang .
filter(lang(?classLabelLang) = 'en')
bind(str(?classLabelLang) as ?classLabel)
}
# exclude certain classes
filter(!bound(?class) || ?class not in (
wd:Q4167410, # disambiguation page
wd:Q11266439, # template
wd:Q4167836, # category
wd:Q4830453, # business enterprise
wd:Q7725634, # literary work
wd:Q732577, # publication
wd:Q571, # book
wd:Q23927052, # conference paper
wd:Q5633421, # scientific journal
wd:Q13442814, # scientific article
wd:Q7366, # song
wd:Q134556, # single
wd:Q482994, # album
wd:Q11424 # film
))
#
# add item labels
optional {
?wd rdfs:label ?wdLabelDe .
filter(lang(?wdLabelDe) = 'de')
}
optional {
?wd rdfs:label ?wdLabelEn .
filter(lang(?wdLabelEn) = 'en')
}
bind(concat(if(bound(?wdLabelDe), str(?wdLabelDe), ''), ' | ', if(bound(?wdLabelEn), str(?wdLabelEn), '')) as ?wdLabel)
#
# Quickstatements link
bind(concat(?wdId, '|P3911|"', ?stwId, '"') as ?qs2Input)
bind(uri(concat('https://tools.wmflabs.org/quickstatements/#v1=', ?qs2Input)) as ?addStw)
}
group by ?wd ?wdId ?wdLabel ?addStw ?labels
order by desc(sum(?score))
limit 20