Module nafparserpy.layers.terms

Expand source code
from dataclasses import dataclass, field
from typing import List

from nafparserpy.layers.utils import AttributeGetter, IdrefGetter, create_node, ExternalReferenceHolder
from nafparserpy.layers.elements import Component, Span, ExternalReferences, Sentiment


@dataclass
class Term(AttributeGetter, IdrefGetter, ExternalReferenceHolder):
    """Represents a term """
    id: str
    span: Span
    """span of covered idrefs"""
    components: List[Component] = field(default_factory=list)
    """optional list of morphemes in term"""
    external_references: ExternalReferences = ExternalReferences([])
    """optional ExternalReferences"""
    sentiment: Sentiment = Sentiment.create_none()
    """optional sentiment"""
    attrs: dict = field(default_factory=dict)
    """optional attributes ('type', 'lemma', 'pos', 'morphofeat', 'netype', 'case', 'head', 'component_of',
    'compound_type')"""

    def __post_init__(self):
        """Copy compulsory attributes to `attrs` field"""
        self.attrs.update({'id': self.id})

    def node(self):
        """Create etree node from object"""
        children = [self.span]
        if not self.sentiment.is_none():
            children.append(self.sentiment)
        if self.external_references.items:
            children.append(self.external_references)
        if self.components:
            children.extend(self.components)
        return create_node('term', None, children, self.attrs)

    @staticmethod
    def object(node):
        """Create object from etree node"""
        return Term(node.get('id'),
                    Span.object(node.find('span')),
                    [Component.object(n) for n in node.findall('component')],
                    ExternalReferences(ExternalReferences.object(node.find('externalReferences'))),
                    Sentiment.object(node.find('sentiment')),
                    node.attrib)

    @staticmethod
    def create(term_id, target_ids, term_attrs):
        """creates a basic term with id, attributes and target ids"""
        return Term(term_id, Span.create(target_ids), external_references=ExternalReferences([]), attrs=term_attrs)


@dataclass
class Terms:
    """Terms layer class"""
    items: List[Term]
    """list of terms"""

    def node(self):
        """Create etree node from object"""
        return create_node('terms', None, self.items, {})

    @staticmethod
    def object(node):
        """Create list of `Term` objects from etree node"""
        return [Term.object(n) for n in node]

Classes

class Term (id: str, span: Span, components: List[Component] = <factory>, external_references: ExternalReferences = ExternalReferences(items=[]), sentiment: Sentiment = AttributeLayer(layer='sentiment', attrs={}), attrs: dict = <factory>)

Represents a term

Expand source code
@dataclass
class Term(AttributeGetter, IdrefGetter, ExternalReferenceHolder):
    """Represents a term """
    id: str
    span: Span
    """span of covered idrefs"""
    components: List[Component] = field(default_factory=list)
    """optional list of morphemes in term"""
    external_references: ExternalReferences = ExternalReferences([])
    """optional ExternalReferences"""
    sentiment: Sentiment = Sentiment.create_none()
    """optional sentiment"""
    attrs: dict = field(default_factory=dict)
    """optional attributes ('type', 'lemma', 'pos', 'morphofeat', 'netype', 'case', 'head', 'component_of',
    'compound_type')"""

    def __post_init__(self):
        """Copy compulsory attributes to `attrs` field"""
        self.attrs.update({'id': self.id})

    def node(self):
        """Create etree node from object"""
        children = [self.span]
        if not self.sentiment.is_none():
            children.append(self.sentiment)
        if self.external_references.items:
            children.append(self.external_references)
        if self.components:
            children.extend(self.components)
        return create_node('term', None, children, self.attrs)

    @staticmethod
    def object(node):
        """Create object from etree node"""
        return Term(node.get('id'),
                    Span.object(node.find('span')),
                    [Component.object(n) for n in node.findall('component')],
                    ExternalReferences(ExternalReferences.object(node.find('externalReferences'))),
                    Sentiment.object(node.find('sentiment')),
                    node.attrib)

    @staticmethod
    def create(term_id, target_ids, term_attrs):
        """creates a basic term with id, attributes and target ids"""
        return Term(term_id, Span.create(target_ids), external_references=ExternalReferences([]), attrs=term_attrs)

Ancestors

Class variables

var attrs : dict

optional attributes ('type', 'lemma', 'pos', 'morphofeat', 'netype', 'case', 'head', 'component_of', 'compound_type')

var components : List[Component]

optional list of morphemes in term

var external_referencesExternalReferences

optional ExternalReferences

var id : str
var sentimentSentiment

optional sentiment

var spanSpan

span of covered idrefs

Static methods

def create(term_id, target_ids, term_attrs)

creates a basic term with id, attributes and target ids

Expand source code
@staticmethod
def create(term_id, target_ids, term_attrs):
    """creates a basic term with id, attributes and target ids"""
    return Term(term_id, Span.create(target_ids), external_references=ExternalReferences([]), attrs=term_attrs)
def object(node)

Create object from etree node

Expand source code
@staticmethod
def object(node):
    """Create object from etree node"""
    return Term(node.get('id'),
                Span.object(node.find('span')),
                [Component.object(n) for n in node.findall('component')],
                ExternalReferences(ExternalReferences.object(node.find('externalReferences'))),
                Sentiment.object(node.find('sentiment')),
                node.attrib)

Methods

def node(self)

Create etree node from object

Expand source code
def node(self):
    """Create etree node from object"""
    children = [self.span]
    if not self.sentiment.is_none():
        children.append(self.sentiment)
    if self.external_references.items:
        children.append(self.external_references)
    if self.components:
        children.extend(self.components)
    return create_node('term', None, children, self.attrs)

Inherited members

class Terms (items: List[Term])

Terms layer class

Expand source code
@dataclass
class Terms:
    """Terms layer class"""
    items: List[Term]
    """list of terms"""

    def node(self):
        """Create etree node from object"""
        return create_node('terms', None, self.items, {})

    @staticmethod
    def object(node):
        """Create list of `Term` objects from etree node"""
        return [Term.object(n) for n in node]

Class variables

var items : List[Term]

list of terms

Static methods

def object(node)

Create list of Term objects from etree node

Expand source code
@staticmethod
def object(node):
    """Create list of `Term` objects from etree node"""
    return [Term.object(n) for n in node]

Methods

def node(self)

Create etree node from object

Expand source code
def node(self):
    """Create etree node from object"""
    return create_node('terms', None, self.items, {})