type_utils.py 2.7 KB
from typing import Any, Sequence, TypedDict, NotRequired

type TokenList = list[str]
type IntLabelList = list[int | None]
type StrLabelList = list[str | None]
type SimpleLabelList = IntLabelList | StrLabelList
type NestedIntLabelList = list[IntLabelList]
type NestedStrLabelList = list[StrLabelList]
type NestedLabelList = NestedIntLabelList | NestedStrLabelList
type AnyLabelList = SimpleLabelList | NestedLabelList

class LabelDict(TypedDict):
    segs:                   NotRequired[StrLabelList]
    nps:                    NotRequired[list[bool]]
    lemmas:                 NotRequired[StrLabelList]
    lemma_cases:            NotRequired[StrLabelList]
    lemma_rules:            NotRequired[StrLabelList]
    tags:                   NotRequired[StrLabelList]
    heads:                  NotRequired[IntLabelList]
    adjacency_matrix:       NotRequired[NestedIntLabelList]
    deprels:                NotRequired[StrLabelList]
    graph_heads:            NotRequired[NestedIntLabelList]
    graph_adjacency_matrix: NotRequired[NestedIntLabelList]
    graph_deprels:          NotRequired[NestedStrLabelList]
    spines:                 NotRequired[StrLabelList]
    anchors:                NotRequired[StrLabelList]
    anchor_hs:              NotRequired[StrLabelList]
    ne_iobs:                NotRequired[StrLabelList]

class DatasetInstance(TypedDict):
    tokens:                 NotRequired[TokenList]
    segs:                   NotRequired[SimpleLabelList] # str/int before/after column casting
    lemmas:                 NotRequired[StrLabelList]
    lemma_cases:            NotRequired[SimpleLabelList]
    lemma_rules:            NotRequired[SimpleLabelList]
    tags:                   NotRequired[IntLabelList]
    heads:                  NotRequired[IntLabelList]
    adjacency_matrix:       NotRequired[NestedIntLabelList]
    deprels:                NotRequired[IntLabelList]
    graph_heads:            NotRequired[NestedIntLabelList]
    graph_adjacency_matrix: NotRequired[NestedIntLabelList]
    graph_deprels:          NotRequired[NestedIntLabelList]
    nonterminals:           NotRequired[Any]
    spines:                 NotRequired[SimpleLabelList]
    anchors:                NotRequired[SimpleLabelList]
    anchor_hs:              NotRequired[SimpleLabelList]
    ne_iobs:                NotRequired[IntLabelList]

type EvalResults = list[tuple[str, dict[str, Any]]]
type TreeDict = dict[str, Any]
#type HydraAnalysis = dict[str, Any]

class HydraAnalysis(TypedDict):
    sentence:  str
    tokens:    TokenList 
    metadata:  dict[str, Any]
    logits:    NotRequired[Any]
    labels:    NotRequired[LabelDict]
    tree:      NotRequired[Any]
    json:      NotRequired[dict[str, Any] | None]
    conllu:    NotRequired[str]