You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用owlready2/rdflib执行古兰经本体SPARQL查询无响应的替代方案

古兰经本体SPARQL查询迁移至Google Colab后执行超时问题

我针对古兰经本体编写了一条复杂SPARQL查询,该查询在Apache Jena Fuseki中可正常返回正确结果,但迁移至Google Colab后,分别使用owlready2和rdflib执行时,程序始终无法终止。以下是两种库的实现代码,同时可获取完整的quran_data_full.owl本体文件,现寻求可行的查询执行替代方案。

使用owlready2的代码

from owlready2 import *
onto_path.append("/gdrive/MyDrive/Quran Corpus")
go = get_ontology("/gdrive/MyDrive/Quran Corpus/quran_data_full.owl").load()
obo = get_namespace("/gdrive/MyDrive/Quran Corpus/")
query = """
PREFIX qu: <http://purl.oclc.org/NET/ssnx/qu/qu#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX qur: <http://quranontology.com/Resource/>


SELECT  distinct ?prophetLabel
WHERE {
?person rdfs:subClassOf* qur:Person.   
?prophet rdf:type  ?person.  
?prophet  rdfs:label ?prophetLabel.
?verse rdf:type qur:Verse.
?prophet qur:MentionedIn ?verse.
?verse2 rdf:type qur:Verse.  
?word rdf:type qur:Word. 
?letter rdf:type qur:PronounRefDifferentVerse. 
?word qur:IsPartOf ?verse2.
?letter qur:IsPartOf ?word.
?letter  qur:ReferToVerse ?verse.
?verse2 rdfs:label ?textSimple2.
?verse rdfs:label ?textSimple.
FILTER ((REGEX(STR(?prophetLabel), "[ء-ي]+" ,"i"))).
FILTER (REGEX(STR(?textSimple2), "برا بوالديه" ,"i")).
  } 
"""
d = list(default_world.sparql(query))
print(d)

使用rdflib的代码

import rdflib

parsingGraph = rdflib.Graph()
parsingGraph.parse ('/gdrive/MyDrive/Quran Corpus/quran_data_full.owl', format='application/rdf+xml')
quran_data_full = rdflib.Namespace('/gdrive/MyDrive/Quran Corpus/quran_data_full.owl')
parsingGraph.bind('quran_data_full', quran_data_full)
query = """
PREFIX qu: <http://purl.oclc.org/NET/ssnx/qu/qu#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX qur: <http://quranontology.com/Resource/>


SELECT  distinct ?prophetLabel
WHERE {
?person rdfs:subClassOf* qur:Person.   
?prophet rdf:type  ?person.  
?prophet  rdfs:label ?prophetLabel.
?verse rdf:type qur:Verse.
?prophet qur:MentionedIn ?verse.
?verse2 rdf:type qur:Verse.  
?word rdf:type qur:Word. 
?letter rdf:type qur:PronounRefDifferentVerse. 
?word qur:IsPartOf ?verse2.
?letter qur:IsPartOf ?word.
?letter  qur:ReferToVerse ?verse.
?verse2 rdfs:label ?textSimple2.
?verse rdfs:label ?textSimple.
FILTER ((REGEX(STR(?prophetLabel), "[ء-ي]+" ,"i"))).
FILTER (REGEX(STR(?textSimple2), "برا بوالديه" ,"i")).
  } 
"""
result = parsingGraph.query(query)
for row in result:
    print(row.prophetLabel)

Apache Jena Fuseki中的查询输出

Fuseki查询输出


内容的提问来源于stack exchange,提问作者Reem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 06:10:37