mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-16 16:51:02 +02:00
Add knowledge classes
This commit is contained in:
parent
a44ab7a2be
commit
a33238e507
6 changed files with 191 additions and 0 deletions
6
trustgraph-base/trustgraph/knowledge/__init__.py
Normal file
6
trustgraph-base/trustgraph/knowledge/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
from . identifier import *
|
||||
from . publication import *
|
||||
from . document import *
|
||||
from . organization import *
|
||||
|
||||
25
trustgraph-base/trustgraph/knowledge/defs.py
Normal file
25
trustgraph-base/trustgraph/knowledge/defs.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
IS_A = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
|
||||
LABEL = 'http://www.w3.org/2000/01/rdf-schema#label'
|
||||
|
||||
DIGITAL_DOCUMENT = 'https://schema.org/DigitalDocument'
|
||||
PUBLICATION_EVENT = 'https://schema.org/PublicationEvent'
|
||||
ORGANIZATION = 'https://schema.org/Organization'
|
||||
|
||||
NAME = 'https://schema.org/name'
|
||||
DESCRIPTION = 'https://schema.org/description'
|
||||
COPYRIGHT_NOTICE = 'https://schema.org/copyrightNotice'
|
||||
COPYRIGHT_HOLDER = 'https://schema.org/copyrightHolder'
|
||||
COPYRIGHT_YEAR = 'https://schema.org/copyrightYear'
|
||||
LICENSE = 'https://schema.org/license'
|
||||
PUBLICATION = 'https://schema.org/publication'
|
||||
START_DATE = 'https://schema.org/startDate'
|
||||
END_DATE = 'https://schema.org/endDate'
|
||||
PUBLISHED_BY = 'https://schema.org/publishedBy'
|
||||
DATE_PUBLISHED = 'https://schema.org/datePublished'
|
||||
PUBLICATION = 'https://schema.org/publication'
|
||||
DATE_PUBLISHED = 'https://schema.org/datePublished'
|
||||
URL = 'https://schema.org/url'
|
||||
IDENTIFIER = 'https://schema.org/identifier'
|
||||
KEYWORD = 'https://schema.org/keywords'
|
||||
|
||||
73
trustgraph-base/trustgraph/knowledge/document.py
Normal file
73
trustgraph-base/trustgraph/knowledge/document.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
|
||||
from . defs import *
|
||||
|
||||
class DigitalDocument:
|
||||
def __init__(
|
||||
self, id, name=None, description=None, copyright_notice=None,
|
||||
copyright_holder=None, copyright_year=None, license=None,
|
||||
identifier=None,
|
||||
publication=None, url=None, keywords=[]
|
||||
):
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.copyright_notice = copyright_notice
|
||||
self.copyright_holder = copyright_holder
|
||||
self.copyright_year = copyright_year
|
||||
self.license = license
|
||||
self.publication = publication
|
||||
self.url = url
|
||||
self.identifier = identifier
|
||||
self.keywords = keywords
|
||||
|
||||
def emit(self, emit):
|
||||
|
||||
emit(Triple(Value(self.id), Value(IS_A), Value(DIGITAL_DOCUMENT)))
|
||||
|
||||
if self.name:
|
||||
emit(Triple(Value(self.id), Value(LABEL), Value(self.name)))
|
||||
emit(Triple(Value(self.id), Value(NAME), Value(self.name)))
|
||||
|
||||
if self.identifier:
|
||||
emit(Triple(Value(id), Value(IDENTIFIER), Value(self.identifier)))
|
||||
|
||||
if self.description:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(DESCRIPTION), Value(self.description)
|
||||
))
|
||||
|
||||
if self.copyright_notice:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(COPYRIGHT_NOTICE),
|
||||
Value(self.copyright_notice)
|
||||
))
|
||||
|
||||
if self.copyright_holder:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(COPYRIGHT_HOLDER),
|
||||
Value(self.copyright_holder)
|
||||
))
|
||||
|
||||
if self.copyright_year:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(COPYRIGHT_YEAR),
|
||||
Value(self.copyright_year)
|
||||
))
|
||||
|
||||
if self.license:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(LICENSE), Value(self.license)
|
||||
))
|
||||
|
||||
if self.keywords:
|
||||
for k in self.keywords:
|
||||
emit(Triple(Value(self.id), Value(KEYWORD), Value(k)))
|
||||
|
||||
if self.publication:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(PUBLICATION, Value(self.publication.id))
|
||||
))
|
||||
self.publication.emit(emit)
|
||||
|
||||
if self.url:
|
||||
emit(Triple(Value(self.id), Value(URL), Value(self.url)))
|
||||
23
trustgraph-base/trustgraph/knowledge/identifier.py
Normal file
23
trustgraph-base/trustgraph/knowledge/identifier.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
|
||||
import uuid
|
||||
import hashlib
|
||||
|
||||
def hash(data):
|
||||
|
||||
if isinstance(data, str):
|
||||
data = data.encode("utf-8")
|
||||
|
||||
# Create a SHA256 hash from the data
|
||||
id = hashlib.sha256(data).hexdigest()
|
||||
|
||||
# Convert into a UUID, 64-byte hash becomes 32-byte UUID
|
||||
id = str(uuid.UUID(id[::2]))
|
||||
|
||||
return id
|
||||
|
||||
def to_uri(pref, id):
|
||||
return f"https://trustgraph.ai/{pref}/{id}"
|
||||
|
||||
PREF_PUBEV = "pubev"
|
||||
PREF_ORG = "org"
|
||||
PREF_DOC = "doc"
|
||||
21
trustgraph-base/trustgraph/knowledge/organization.py
Normal file
21
trustgraph-base/trustgraph/knowledge/organization.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
from . defs import ORGANIZATION, DESCRIPTION
|
||||
|
||||
class Organization:
|
||||
def __init__(self, id, name=None, description=None):
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.description = description
|
||||
|
||||
def emit(self, emit):
|
||||
|
||||
emit(Triple(Value(self.id), Value(IS_A), Value(ORGANIZATION)))
|
||||
|
||||
if self.name:
|
||||
emit(Triple(Value(self.id), Value(LABEL), Value(self.name)))
|
||||
emit(Triple(Value(self.id), Value(NAME), Value(self.name)))
|
||||
|
||||
if self.description:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(DESCRIPTION), Value(self.description)
|
||||
))
|
||||
43
trustgraph-base/trustgraph/knowledge/publication.py
Normal file
43
trustgraph-base/trustgraph/knowledge/publication.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
|
||||
from . defs import *
|
||||
|
||||
class PublicationEvent:
|
||||
def __init__(
|
||||
self, id, organization=None, name=None, description=None,
|
||||
start_date=None, end_date=None,
|
||||
):
|
||||
self.id = id
|
||||
self.organization = organization
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.start_date = start_date
|
||||
self.end_date = end_date
|
||||
|
||||
def emit(self, emit):
|
||||
|
||||
emit(Triple(Value(self.id), Value(IS_A), Value(PUBLICATION_EVENT)))
|
||||
|
||||
if self.name:
|
||||
emit(Triple(Value(self.id), Value(LABEL), Value(self.name)))
|
||||
emit(Triple(Value(self.id), Value(NAME), Value(self.name)))
|
||||
|
||||
if self.description:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(DESCRIPTION), Value(self.description)
|
||||
))
|
||||
|
||||
if self.organization:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(PUBLISHED_BY),
|
||||
Value(self.organization.id)
|
||||
))
|
||||
self.organization.emit(emit)
|
||||
|
||||
if self.start_date:
|
||||
emit(Triple(
|
||||
Value(self.id), Value(START_DATE), Value(self.start_date)
|
||||
))
|
||||
|
||||
if self.end_date:
|
||||
emit(Triple(Value(self.id), Value(END_DATE), Value(self.end_date)))
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue