2023. 10. 5. 16:18ㆍArchitecture/KG
지식그래프에서 스키마는
지식그래프의 구조와 의미를 정의하는 역할을 합니다.
스키마에는 다음과 같은 정보가 포함됩니다.
엔티티: 지식그래프에서 표현하는 개체를 나타냅니다.
속성: 엔티티가 가지고 있는 특성을 나타냅니다.
관계: 엔티티 간의 관계를 나타냅니다.
스키마는 지식그래프의 데이터를 이해하고 처리하는 데 필수적입니다.
스키마가 없으면 지식그래프에 저장된 데이터의 의미를 파악하기 어렵습니다.
또한, 스키마를 통해 지식그래프의 구조를 정리하고 효율적으로 관리할 수 있습니다.
스키마의 주요 역할
- 지식그래프의 구조와 의미를 정의: 스키마는 지식그래프에 포함된 엔티티, 속성, 관계의 종류와 의미를 정의합니다.
이를 통해 지식그래프의 데이터를 이해하고 처리할 수 있습니다. - 지식그래프의 데이터를 정리: 스키마를 통해 지식그래프의 데이터를 체계적으로 정리할 수 있습니다.
이를 통해 데이터의 중복을 제거하고, 데이터를 효율적으로 관리할 수 있습니다. - 지식그래프의 확장과 발전을 지원: 스키마를 통해 지식그래프를 확장하고 발전시킬 수 있습니다.
새로운 엔티티, 속성, 관계를 추가하거나 기존의 엔티티, 속성, 관계의 의미를 변경할 수 있습니다.
스키마 예시
영화 지식그래프
영화 지식그래프는 영화, 배우, 감독, 장르와 같은 엔티티와 그 간의 관계를 표현하는 지식그래프 입니다.
엔티티
* 영화
* 배우
* 감독
* 장르
속성
* 영화: 제목, 장르, 개봉일, 평점
* 배우: 이름, 나이, 국적
* 감독: 이름, 출생년도, 출생지
* 장르: 이름, 설명
관계
* 영화 - 감독: 감독이 연출한 영화
* 영화 - 배우: 배우가 출연한 영화
* 영화 - 장르: 영화의 장르
RDF(Resource Description Framework)를 사용하여 작성된 스키마 예시
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix owl: <http://www.w3.org/2002/07/owl#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
# 엔티티 정의
<http://example.com/Person>
a owl:Class ;
rdfs:label "사람" ;
rdfs:comment "사람을 나타내는 클래스".
<http://example.com/Movie>
a owl:Class ;
rdfs:label "영화" ;
rdfs:comment "영화를 나타내는 클래스".
<http://example.com/Place>
a owl:Class ;
rdfs:label "장소" ;
rdfs:comment "장소를 나타내는 클래스".
# 속성 정의
<http://example.com/name>
a owl:DatatypeProperty ;
rdfs:label "이름" ;
rdfs:range xsd:string.
<http://example.com/age>
a owl:DatatypeProperty ;
rdfs:label "나이" ;
rdfs:range xsd:integer.
<http://example.com/gender>
a owl:DatatypeProperty ;
rdfs:label "성별" ;
rdfs:range xsd:string.
<http://example.com/address>
a owl:DatatypeProperty ;
rdfs:label "주소" ;
rdfs:range xsd:string.
<http://example.com/title>
a owl:DatatypeProperty ;
rdfs:label "제목" ;
rdfs:range xsd:string.
<http://example.com/releaseDate>
a owl:DatatypeProperty ;
rdfs:label "개봉일" ;
rdfs:range xsd:date.
<http://example.com/genre>
a owl:DatatypeProperty ;
rdfs:label "장르" ;
rdfs:range xsd:string.
<http://example.com/director>
a owl:DatatypeProperty ;
rdfs:label "감독" ;
rdfs:range xsd:string.
<http://example.com/actor>
a owl:DatatypeProperty ;
rdfs:label "배우" ;
rdfs:range xsd:string.
# 관계 정의
<http://example.com/friend>
a owl:ObjectProperty ;
rdfs:label "친구" ;
rdfs:domain <http://example.com/Person> ;
rdfs:range <http://example.com/Person>.
<http://example.com/spouse>
a owl:ObjectProperty ;
rdfs:label "배우자" ;
rdfs:domain <http://example.com/Person> ;
rdfs:range <http://example.com/Person>.
<http://example.com/family>
a owl:ObjectProperty ;
rdfs:label "가족" ;
rdfs:domain <http://example.com/Person> ;
rdfs:range <http://example.com/Person>.
<http://example.com/genre>
a owl:ObjectProperty ;
rdfs:label "장르" ;
rdfs:domain <http://example.com/Movie> ;
rdfs:range <http://example.com/Movie>.
<http://example.com/director>
a owl:ObjectProperty ;
rdfs:label "감독" ;
rdfs:domain <http://example.com/Movie> ;
rdfs:range <http://example.com/Person>.
<http://example.com/actor>
a owl:ObjectProperty ;
rdfs:label "배우" ;
rdfs:domain <http://example.com/Movie> ;
rdfs:range <http://example.com/Person>
위와 같은 스키마는 어떻게 사용되는가??
해당 스키마를 사용하려면 먼저 parsing을 해야 합니다.
RDF 트리구조를 분석하여 스키마의 엔티티, 속성, 관계를 추출하는 과정 입니다.
<http://example.com/Person>
a owl:Class ;
rdfs:label "사람" ;
rdfs:comment "사람을 나타내는 클래스".
위의 RDF 트리의 경우,
<http://example.com/Person>은 엔티티,
a는 관계,
owl:Class는 엔티티의 종류,
rdfs:label은 엔티티의 이름,
rdfs:comment는 엔티티의 설명을 나타냅니다.
RDF를 parsing을 하면
String rdf = """
<http://example.com/Person>
a owl:Class ;
rdfs:label "사람" ;
rdfs:comment "사람을 나타내는 클래스".
""";
List<RDFNode> nodes = parseRDF(rdf);
for (RDFNode node : nodes) {
System.out.println(node.getSubject() + " " + node.getPredicate() + " " + node.getObject());
}
public static List<RDFNode> parseRDF(String rdf) {
List<RDFNode> nodes = new ArrayList<>();
String[] lines = rdf.split("\n");
for (String line : lines) {
String[] tokens = line.split(" ");
if (tokens.length == 3) {
nodes.add(new RDFNode(tokens[0], tokens[1], tokens[2]));
}
}
return nodes;
}
아래와 같이 출력이 됩니다.
<http://example.com/Person> a owl:Class
<http://example.com/Person> rdfs:label "사람"
<http://example.com/Person> rdfs:comment "사람을 나타내는 클래스"
엔티티는 <http://example.com/Person>이라고 했습니다.
이 엔티티를 추출한 후에 속성과 관계를 추출할 수 있습니다.
public class RDFParser {
private static final String RDF_TYPE = "a";
public static List<String> extractProperties(List<RDFNode> nodes) {
List<String> properties = new ArrayList<>();
for (RDFNode node : nodes) {
if (RDF_TYPE.equals(node.getPredicate())) {
properties.add(node.getObject());
}
}
return properties;
}
public static List<String> extractRelationships(List<RDFNode> nodes) {
List<String> relationships = new ArrayList<>();
for (RDFNode node : nodes) {
if (!RDF_TYPE.equals(node.getPredicate())) {
relationships.add(node.getPredicate());
}
}
return relationships;
}
public static void main(String[] args) {
String rdf = """
<http://example.com/Person>
a owl:Class ;
rdfs:label "사람" ;
rdfs:comment "사람을 나타내는 클래스".
<http://example.com/Person> rdfs:label "사람".
<http://example.com/Person> <http://example.com/friend> <http://example.com/Person>.
""";
List<RDFNode> nodes = parseRDF(rdf);
List<String> properties = extractProperties(nodes);
System.out.println("속성: " + properties);
List<String> relationships = extractRelationships(nodes);
System.out.println("관계: " + relationships);
}
}
결과로 아래와 같이 출력 됩니다.
속성: [rdfs:label]
관계: [a owl:Class rdfs:label]
다시 돌아와서 parsing을 통해 추출한 엔티티와 속성, 관계를 사용하여 지식그래프를 생성 할 수 있습니다.
또한 생성된 지식그래프를 사용하여 다음과 같은 질문에 대한 답을 할 수 있습니다.
"아이유는 누구와 친구입니까?"
SELECT ?friend
FROM <http://example.com/>
WHERE
{
<http://example.com/IU> <http://example.com/friend> ?friend .
}
위 쿼리에 대한 결과는 아래와 같습니다.
?friend
<http://example.com/BTS>
<http://example.com/Jennie>
아이유의 친구는 BTS와 Jennie
결론
RDF의 형태로 스키마를 구성하여 지식그래프의 구조와 의미를 정의하고 해당 스키마 RDF를 파싱해서 질의를 하여 답을 얻어낼수 있다.
NEXT
질의를 하는 SPARQL에 대해서 알아보자!

'Architecture > KG' 카테고리의 다른 글
지식그래프(Knowledge Graph) 관련 정보 (0) | 2022.11.02 |
---|---|
타 회사 지식그래프에 대해 알아보자 (0) | 2022.08.02 |