当前位置:网站首页>SPARQL基础入门练习
SPARQL基础入门练习
2022-06-27 07:56:00 【Zjhao666】
1、前言
知识图谱:Freebase,服务器:virtuoso
SPARQL基本语法表

一个SPARQL样例
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?x
WHERE {
FILTER (?x != ns:m.0160w)
FILTER (!isLiteral(?x) OR lang(?x) = '' OR langMatches(lang(?x), 'en'))
ns:m.0160w ns:location.country.currency_used ?x .
}大致意思是
- PREFIX ns: <http://rdf.freebase.com/ns/>——PREFIX声明缩写,后面的ns表示http://rdf.freebase.com/ns/。例如,ns:m.0160w在被解释器执行时,等价于http://rdf.freebase.com/ns/m.0160w。
- SELECT DISTINCT ?x——搜索变量x(?x),且搜索结果要求去重(DISTINCT)
- WHERE {}——这一子句接在SELECT之后,{}内容表示搜索SELECT的具体筛选条件。
- FILTER (?x != ns:m.0160w)——要求变量x不是ns:m.0160w
- FILTER (!isLiteral(?x) OR lang(?x) = '' OR langMatches(lang(?x), 'en'))——这一子句有点复杂,是三个子句的组合,分别表示要求:
- 或,变量x不是文本(!isLiteral)
- 或,x的语言是空字符串
- 或,x的语言是英文(en)
- ns:m.0160w ns:location.country.currency_used ?x .——这实际上是一个三元组。表示变量x上存在一条边,是由实体ns:m.0160w指出,通过关系ns:location.country.currency_used,指向变量x。值得注意的是,凡是这样的三元组子句,后面都要跟一个英文句号.(见红点),不然解释器会报错。
在virtuoso服务器执行后,设置格式为html,结果如下。

此外,值得注意的是,对于
SELECT DISTINCT ?x而言,若写为
SELECT DISTINCT ?x1 ?x2 ?x3则返回满足后续(where子句里的)约束条件的(x1, x2, x3)组合。
2、基本操作(由简单到复杂)
2.1 无条件查询
最简单的形式
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT ?e1 ?r ?e2
where {?e1 ?r ?e2}这表示,不加任何限制地,查询图谱内所有的三元组。返回结果自然就有很多:

可以看到返回结果的顶上,还写上了查询语句里我们自己命名的e1,r,e2。
2.2 已知单个三元组的条件查询
加上一个三元组的约束
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
}即要求变量e1和r,满足三元组(?e1 ?r ns:m.0160w)。结果如下:

2.3 嵌套查询
当然,我们还可以再嵌套其他的三元组(嵌套查询),比如
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
?e1 ns:freebase.user_profile.hometown ?e2 .
}显然,这里我们就加了两个三元组约束。结果的条数自然就少得多了。

2.4 过滤参数FILTER
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
FILTER(?r=ns:freebase.user_profile.hometown)
}即过滤出(即,只要这部分)变量r是ns:freebase.user_profile.hometown的情况。结果如下:

2.5 可选的约束OPTIONAL
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r ?e2
WHERE {
?e1 ?r ns:m.0160w .
OPTIONAL { ?e1 ns:freebase.user_profile.hometown ?e2 }
}这样的话,第二个三元组约束就是可选满足的了,即若条件(?e1,ns:freebase.user_profile.hometown,?)成立,再查询?处的?e2;若不成立,就变量e2空着。部分结果显示如下:

可见结果和2.2已知单个三元组的条件查询,的结果几乎是一致的——只是最后多了e2一列。
2.6 任满足其一即可的UNION
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r ?e2
WHERE {
{ ?e1 ?r ns:m.0160w } UNION { ?e1 ns:freebase.user_profile.hometown ?e2 }
}可见,满足三元组条件1(?e1 ?r ns:m.0160w)或条件2(?e1 ns:freebase.user_profile.hometown ?e2)中任意一个,就返回。部分结果如下:

条件1 UNION 条件2,和, 条件1 OPTIONAL 条件2 的区别就在于:
- UNION刻画“或”关系,要求条件1,2当中至少满足其中一个条件
- 条件1 OPTIONAL 条件2,仅要求必须满足条件1;至于条件2,若满足,也顺带执行查询。
2.7 统计计数 COUNT
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT COUNT DISTINCT ?e1 ?r ?e2
WHERE {
{ ?e1 ?r ns:m.0160w } UNION { ?e1 ns:freebase.user_profile.hometown ?e2 }
}此处与DISTINCT配合使用,即统计满足条件的无重复的e1,r,e2组合数量,结果如下:

2.8 语言限定
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
?e1 ns:freebase.user_profile.hometown ?e2 .
FILTER(lang(?e1)!='zh')
}结合FILTER实现,即过滤出(只要这部分)语言不是中文(zh)的变量e1和r。

可见结果跟2.3嵌套查询是一致的。
3、参考
参考博客:
边栏推荐
- L'enquête en aveugle a montré que les femmes étaient meilleures que les hommes.
- js中输入三个值,并且由小到大输出
- JS to judge the odd and even function and find the function of circular area
- c的时间函数算效率
- No matter how good LCD and OLED display technologies are, they cannot replace this ancient display nixie tube
- 【12. 最大连续不重复子序列】
- JS print 99 multiplication table
- 参考 | 升级 Win11 移动热点开不了或者开了连不上
- Error in idea connection database
- C how to call line and rows when updating the database
猜你喜欢

JS to judge the odd and even function and find the function of circular area
![[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst](/img/09/cd12c276392d3465dce1909d0f86a6.png)
[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst

JS use the switch statement to output the corresponding English day of the week according to 1-7

【批处理DOS-CMD命令-汇总和小结】-输出/显示命令——echo

Implementation of game hexagon map

The IPO of Yefeng pharmaceutical was terminated: Yu Feng, the actual controller who had planned to raise 540million yuan, made P2P investment

JS use switch to output whether the result is qualified
![[batch dos-cmd command - summary and summary] - output / display command - echo](/img/44/cddad70eeb756db15c19100c25c61a.png)
[batch dos-cmd command - summary and summary] - output / display command - echo

认识O(NlogN)的排序

语音信号处理-概念(二):幅度谱(短时傅里叶变换谱/STFT spectrum)、梅尔谱(Mel spectrum)【语音的深度学习主要用幅度谱、梅尔谱】【用librosa或torchaudio提取】
随机推荐
Mobile security tools -jad
2、项目使用的QT组件
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage
JS example print the number and sum of multiples of all 7 between 1-100
Coal crusher
JS to determine whether the result is qualified, the range is 0-100, otherwise re-enter
Binary tree structure and heap structure foundation
Speech signal feature extraction process: input speech signal - framing, pre emphasis, windowing, fft- > STFT spectrum (including amplitude and phase) - square the complex number - > amplitude spectru
语音合成:Tacotron详解【端到端语音合成模型】【与传统语音合成相比,它没有复杂的语音学和声学特征模块,而是仅用<文本序列,语音声谱>配对数据集对神经网络进行训练,因此简化了很多流程】
1-4 进制表示与转换
【批处理DOS-CMD命令-汇总和小结】-环境变量、路径变量、搜索文件位置相关指令——set、path、where,cmd命令的路径参数中有空格怎么办
The first part of the construction of the defense system of attack and defense exercise is the introduction and the four stages of Defense
Mapping of Taobao virtual product store opening tutorial
第6届蓝桥杯
无论LCD和OLED显示技术有多好,都无法替代这个古老的显示数码管
【论文阅读】Intrinsically semi-supervised methods
How to view program running time (timer) in JS
参考 | Win11 开启热点之后电脑不能上网
Common operation and Principle Exploration of stream
Mysql-8 download, installation and configuration tutorial under Windows