Files

127 lines
4.2 KiB
Python

#
# This file contains the classes that represent our basic data model,
# we use pydantic to define the data model so we can easily cache it
# in a simple sqlite database.
#
import enum
import typing
import pydantic
import hashlib
class Language(enum.Enum):
''' Represents the language of a concept. '''
DE = 'de'
EN = 'en'
class ConceptType(enum.Enum):
''' Represents the type of a concept. '''
FACULTY = 'faculty'
INSTITUTE = 'institute'
CHAIR = 'chair'
class Concept(pydantic.BaseModel):
''' A container for concepts with singular, plural and abbreviations. '''
language: Language
singular: str
plural: str
abbreviations: typing.List[str] = pydantic.Field(default=[])
type: ConceptType
def __hash__(self):
return hash(self.singular)
class Resource(pydantic.BaseModel):
url: str
pagedepth: int
text: str
class Link(Resource):
''' Represents a link with a URL, a text on a page. '''
wrapper: bool = pydantic.Field(default=False)
class Page(Resource):
''' Represents a page with a URL, text and links. '''
links: typing.List[Link]
language: Language = pydantic.Field(default=Language.DE, exclude=True)
headings: typing.List[str] = pydantic.Field(default=[], exclude=True)
found_link_text_flag: bool
ignore_flag: bool = pydantic.Field(default=False)
class AggregatorPage(Page):
''' Represents a page that is used as an aggregator. '''
parent: str = pydantic.Field(default='')
parent_link: str = pydantic.Field(default='')
concept: Concept
children: typing.List[Link] = pydantic.Field(default=[])
class EntityDetail(pydantic.BaseModel):
''' Represents a detailed entity '''
name: str = pydantic.Field(default='')
url: typing.List[str]
content: typing.List[str]
hash: typing.List[str]
found: bool = pydantic.Field(default=False)
def mark(self, value: bool):
if isinstance(value, bool):
self.found = value
class Target(Resource):
''' Represents a target with a URL, text and a hash. '''
hash: str
def matches(self, other: 'Target') -> bool:
''' Checks if the content matches this content. '''
if self.url == other.url:
return True
if self.hash == other.hash:
return self.text == other.text
return False
@classmethod
def create(cls, url: str, text: str, pagedepth: int):
''' Creates a target from a resource. '''
hash = hashlib.sha256()
hash.update(text.encode('utf-8'))
return cls(url = url, pagedepth = pagedepth, text = text, hash = hash.hexdigest())
class Entity(pydantic.BaseModel):
''' Base class for an entity. '''
targets: typing.List[Target]
name: str = pydantic.Field(default='')
found: bool = pydantic.Field(default=False)
def equals(self, other: 'Entity') -> bool:
''' Checks if all targets match a target. '''
for target in self.targets:
if not any(target.matches(other_target) for other_target in other.targets):
return False
for other_target in other.targets:
if not any(other_target.matches(target) for target in self.targets):
return False
return True
def matches(self, other: 'Entity') -> bool:
''' Checks if any target matches the targets. '''
for target in self.targets:
if any(target.matches(other_target) for other_target in other.targets):
return True
return False
@classmethod
def create(cls, name: str, targets: typing.List[Target]):
''' Creates an entity from a name and a list of targets. '''
return cls(name = name, targets = targets)
class ChairEntity(Entity):
''' Represents a chair entity. '''
pass
class InsituteEntity(Entity):
''' Represents an institute entity. '''
pass
class FacultyEntity(Entity):
''' Represents a faculty entity. '''
chairs: typing.List['ChairEntity'] = pydantic.Field(default=[])
institutes: typing.List['InsituteEntity'] = pydantic.Field(default=[])