티스토리 뷰
rdflib은 파이썬을 사용해 RDF 데이터를 구축할 수 있는 라이브러리다.
기존에 많이 사용하는 DCTERMS, RDF, RDFS, SKOS 등은 이미 네임스페이스, 즉, URI체계가 내부에 정의되어 있어서 라이브러리를 통해 불러와주면 사용할 수 있다.
DCTERMS는 dct, dcterms 등 prefix를 다양하게 쓰는 경우가 있어, 임의로 prefix를 수정하려면 'bind'를 사용하면 된다.
from rdflib import Namespace, Literal, URIRef
from rdflib.graph import Graph
from rdflib.namespace import DCTERMS
# RDF 그래프 생성
g = Graph()
# DCTERMS 네임스페이스의 접두사를 수정하여 'mydcterms'로 지정
g.bind('mydcterms', DCTERMS)
# 데이터 추가
subject_uri = URIRef("http://example.org/resource/1")
g.add((subject_uri, DCTERMS.title, Literal("Example Resource")))
g.add((subject_uri, DCTERMS.creator, URIRef("http://example.org/creator/1")))
# 그래프 내용 출력
print(g.serialize(format='turtle').decode())
내가 헷갈렸던 부분은 rdflib에서 schema.org를 지원하지 않는 줄 알았기 때문에 아래와 같이 임의로 지정을 해주었다.
그런데, 자꾸 schema가 아니라 schema1으로 변환되는 문제가 생겼다.
SCHEMA = Namespace("http://schema.org/") #네임스페이스
g = Graph()
g.bind("schema",SCHEMA) # prefix 추가
# turtle 출력 시
>> @prefix schema1: <http://schema.org/>
rdflib 깃헙에서 코드를 이리저리 찾아본 결과 "SDO"가 schema.org의 약어임을 알아냈다.
내가 원하는 작업을 해주려면, 아래와 같이 코드를 변경해 적용하였다.
from rdflib.namespace import SDO
g = Graph() #트리플스토어 생성
g.bind("schema", SDO)
# 데이터 추가
subject_uri = URIRef("http://example.org/resource/1")
g.add((subject_uri, SDO.title, Literal("Example Resource"))) # SDO를 사용해야함
관련된 네임스페이스와 바인딩의 추가내용은 아래 공식 홈페이지를 참고하면 된다.
https://rdflib.readthedocs.io/en/stable/namespaces_and_bindings.html
Namespaces and Bindings — rdflib 7.0.0 documentation
Namespaces and Bindings RDFLib provides several short-cuts to working with many URIs in the same namespace. The rdflib.namespace defines the rdflib.namespace.Namespace class which lets you easily create URIs in a namespace: from rdflib import Namespace EX
rdflib.readthedocs.io
'지식그래프(Knowledge Graph)' 카테고리의 다른 글
[스크랩] 지식그래프 임베딩 (Knowledge Graph Embedding, KGE) (3) | 2024.05.23 |
---|---|
[스크랩] Going Meta - RAG with Knowledge Graphs (with Neo4j) (0) | 2024.05.20 |
[SPARQL] URI를 '/'로 구분하고 마지막 문자열만 추출 (0) | 2023.08.11 |
[SPARQL] GROUP_CONCAT 여러 행을 단일 행으로 출력 (1) | 2023.08.11 |
[SPARQL] 클래스(rdf:type)별 인스턴스 수량 (0) | 2023.07.06 |
- Total
- Today
- Yesterday
- polars
- cursorai
- vscode
- MongoDB
- vervel
- LLM
- pdfmathtranslate
- deepseek
- TextRank
- python
- 지식그래프
- ChatGPT
- rdflib
- python'
- Encoding
- knowledgegraph
- rdffox
- PEFT
- Claude
- PostgreSQL
- 지식그래프임베딩
- psycopg
- Vue3
- SPARQL
- pandas
- 키워드추출
- writerow
- geospy
- Postgis
- hadoop
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |