在 R 中加载本体

问题描述 投票:0回答:1

我想使用 bert 映射算法查找特定本体中存在列表中的哪些值。

第一步是加载本体以便在流程中使用它。

由于我是这方面的新手,所以我要求chatgpt创建一个示例,我花了这个。然而,当我检查 rdflib 时,没有

Graph()
命令。

这是例子:

install.packages(c("textTinyR", "rdflib"))
library(textTinyR)
library(rdflib)
uco_ontology <- "C:/Users/User/Desktop/uco_1_5.owl"  # Replace with the path to your UCO ontology file
graph <- rdflib::Graph()
rdflib::parse(graph, file = uco_ontology)

有什么方法可以将本体加载到R中吗? 从这里您可以下载请求片段的本体(猫头鹰)文件

r owl rdflib
1个回答
0
投票

有几种方法可以解析您拥有的 .owl 文件,包的 vignette 可能包含更多信息

require(jsonld)
require(rdflib)
require(xml2)
require(jsonlite)
require(textTinyR)

# the expected format based on the example .rdf file
doc <- system.file("extdata/example.rdf", package="redland")
tmp_doc = readLines(doc, warn = F)
tmp_doc
# [1] "<?xml version=\"1.0\"?>"                                                  "  <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""    
# [3] "  xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"                         "    <rdf:Description rdf:about=\"http://www.johnsmith.com/\">"           
# [5] "    <dc:title>John Smith's Home Page</dc:title>"                          "    <dc:creator>John Smith</dc:creator>"                                 
# [7] "    <dc:description>The generic home page of John Smith</dc:description>" "  </rdf:Description> "                                                   
# [9] "</rdf:RDF>"  

# get the raw Github version of your mentioned .owl weblink
url_pth = 'https://raw.githubusercontent.com/Ebiquity/Unified-Cybersecurity-Ontology/master/uco_1_5.owl'

# The following does not work as it returns 0 'triples'

# read with xml2 and convert to json 
dat_xml = xml2::read_xml(url_pth) |>
  xml2::as_list() |>
  jsonlite::toJSON()

# use format 'jsonld'
rdf = rdflib::rdf_parse(doc = dat_xml, format = "jsonld")
rdf
# Total of 0 triples, stored in hashes
# -------------------------------

# The following approach returns parsed data

# download the .owl to a temporary file
tmp_xml <- tempfile(fileext = '.xml')
download.file(url = url_pth, destfile = tmp_xml)

# then each line in the file is a separate item in the vector, trims item both sides and concatenate using an empty space (new line works too)
dat_xml = textTinyR::read_rows(input_file = tmp_xml) |>
  sapply(function(x) {
    x = trimws(x, which = 'both')
    x
  }) |>
  paste(collapse = ' ')

# it returns output 'triples', I'm not sure if the total number is correct you must have to verify that, as there are many 'librdf errors'
rdf_out = rdflib::rdf_parse(doc = dat_xml, format = "guess")
# librdf error {��V - Using property attribute 'ontologyIRI' without a namespace is forbidden.
# librdf error {��V - Using property attribute 'versionIRI' without a namespace is forbidden.
# librdf error V - Using an attribute 'name' without a namespace is forbidden.
# ....

rdf_out
# Total of 57 triples, stored in hashes
# -------------------------------
# _:r1710140791r63392r12 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r17 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r25 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r18 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r32 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r30 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r29 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r13 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r24 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# _:r1710140791r63392r15 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
# 
# ... with 47 more triples

使用 Pytorch 包(https://krr-oxford.github.io/DeepOnto/)使用“bert map”可能会有更好的运气。如果需要 R,您可以使用“网状”R 包在 R 中使用它

© www.soinside.com 2019 - 2024. All rights reserved.