使用SPARQL查询从Turtle文件中提取三元组数据。

huangapple go评论60阅读模式
英文:

Fetching triples using SPARQL query from turtle file

问题

我是新手使用SPARQL,目前在尝试从一个Turtle文件中提取三元组时遇到困难。

我正在尝试提取所有类别及其超类、标签和同义词。我运行的查询如下。

但是,此查询会过滤掉不存在'hasExactSynonym'的三元组。

以下是输出:

预期输出是:
英文:

I am new to SPARQL and currently struglling to fetch triples from a turtle file.

###  https://ontology/1001
<https://ontology/1001> rdf:type owl:Class ;
                                             rdfs:subClassOf <https://ontology/748>;
                                             <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> "Injury, neuronal" ,
                                                                                                           "Neurotrauma" ;
                                             rdfs:label "Nervous system injury" .
                                             


###  https://ontology/10021
<https://ontology/10021> rdf:type owl:Class ;
                                              rdfs:subClassOf <https://ontology/2034> ;
                                              rdfs:label "C3 glomerulopathy" .
                                              

I am trying to extract all classes with their superclasses, labels and Synonym. The query which I am running is below.

query_id = """

        prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>
        prefix obo: <http://purl.obolibrary.org/obo/>
        prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

        SELECT distinct ?cid ?label ?class ?synonyms
        WHERE {
            ?cid rdfs:label ?label .
            ?cid rdfs:subClassOf ?class .
            ?cid oboInOwl:hasExactSynonym ?synonyms .
        }

"""

However, this query is filtering the triple where 'hasExactSynonym' doesn't exists.

Following is the output:

cid    label                   class  synonyms
1001   Nervous system injury   748    Injury, neuronal , Neurotrauma

The expected output is:

cid    label                   class  synonyms
1001   Nervous system injury   748    Injury, neuronal , Neurotrauma
10021  C3 glomerulopathy       2034  

答案1

得分: 3

你可以使用 OPTIONAL 来使同义词变成可选的:

WHERE {
  ?cid rdfs:label ?label .
  ?cid rdfs:subClassOf ?class .
  OPTIONAL { ?cid oboInOwl:hasExactSynonym ?synonyms . }
}
英文:

You can use OPTIONAL to make the synonyms optional:

<!-- language: lang-none -->

WHERE {
  ?cid rdfs:label ?label .
  ?cid rdfs:subClassOf ?class .
  OPTIONAL { ?cid oboInOwl:hasExactSynonym ?synonyms . }
}

huangapple
  • 本文由 发表于 2023年2月6日 20:11:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定