amachine.am_syntagmatics API documentation

# amachine.am_syntagmatics

View Source

     1from abc import ABC, abstractmethod
     2import warnings
     3from dataclasses import dataclass, field
     4from typing import Callable
     5
     6from .am_symbol import Symbol
     7from .am_machine import Machine
     8from .am_cohesion import CohesionKernel
     9
    10TNorm = Callable[[float, float], float]
    11
    12PRODUCT     : TNorm = lambda a, b: a * b
    13MINIMUM     : TNorm = lambda a, b: min(a, b)
    14LUKASIEWICZ : TNorm = lambda a, b: max(0.0, a + b - 1.0)
    15
    16class SyntagmaticRule(ABC):
    17	@abstractmethod
    18	def applies(self, domain ) -> bool:...
    19	def cohesion_scores(self, symbol : Symbol) -> dict[str, float]:...
    20
    21@dataclass
    22class ExplicitCohesionRule(SyntagmaticRule):
    23
    24	machine : Machine
    25	domains  : set[str]
    26	kernel : CohesionKernel
    27
    28	def applies(self,domain) :
    29		return domain in self.domains
    30
    31	def cohesion_score(self, source : Symbol, symbols : set[Symbol] ) -> dict[str, float]:
    32		return self.kernel.cohesion_scores( source, symbols )
    33
    34@dataclass
    35class Syntagmatics:
    36	"""
    37	Defines the syntagmatic structure encompassing selection,
    38	cohesion, variability, and scope constraints.
    39
    40	Also defines a context (e.g., the model topology) dependent stochastic groupoid
    41	"""
    42	rules : list[SyntagmaticRule] = field(default_factory=list)
    43	t_norm : TNorm = field(default=PRODUCT)
    44
    45	def _combine_cohesion_scores(self, source: str, domain: set[str]) -> dict[str, float]:
    46		active  = [r for r in self.rules if r.applies(domain)]
    47		symbols = set.intersection(*(set(r.symbols) for r in active))
    48		scores  = {s: 1.0 for s in symbols}
    49		for rule in active:
    50			for s, w in rule.cohesion_scores(source, symbols).items():
    51				scores[s] = self.t_norm(scores[s], w)
    52		return scores
    53
    54	def selection_probabilities(self, source: str, domain: set[str]) -> dict[str, float]:
    55		return self._normalize(source, self._combine_cohesion_scores(source, domain))

TNorm = typing.Callable[[float, float], float]
def PRODUCT(a, b): View Source

    13PRODUCT     : TNorm = lambda a, b: a * b

def MINIMUM(a, b): View Source

    14MINIMUM     : TNorm = lambda a, b: min(a, b)

def LUKASIEWICZ(a, b): View Source

    15LUKASIEWICZ : TNorm = lambda a, b: max(0.0, a + b - 1.0)

class SyntagmaticRule(abc.ABC): View Source

    17class SyntagmaticRule(ABC):
    18	@abstractmethod
    19	def applies(self, domain ) -> bool:...
    20	def cohesion_scores(self, symbol : Symbol) -> dict[str, float]:...

Helper class that provides a standard way to create an ABC using inheritance.

@abstractmethod
def applies(self, domain) -> bool: View Source

    18	@abstractmethod
    19	def applies(self, domain ) -> bool:...

def cohesion_scores(self, symbol: amachine.am_symbol.Symbol) -> dict[str, float]: View Source

    20	def cohesion_scores(self, symbol : Symbol) -> dict[str, float]:...

@dataclass
class ExplicitCohesionRule(SyntagmaticRule): View Source

    22@dataclass
    23class ExplicitCohesionRule(SyntagmaticRule):
    24
    25	machine : Machine
    26	domains  : set[str]
    27	kernel : CohesionKernel
    28
    29	def applies(self,domain) :
    30		return domain in self.domains
    31
    32	def cohesion_score(self, source : Symbol, symbols : set[Symbol] ) -> dict[str, float]:
    33		return self.kernel.cohesion_scores( source, symbols )

ExplicitCohesionRule( machine: amachine.am_machine.Machine, domains: set[str], kernel: amachine.am_cohesion.CohesionKernel)
machine: amachine.am_machine.Machine
domains: set[str]
kernel: amachine.am_cohesion.CohesionKernel
def applies(self, domain): View Source

    29	def applies(self,domain) :
    30		return domain in self.domains

def cohesion_score( self, source: amachine.am_symbol.Symbol, symbols: set[amachine.am_symbol.Symbol]) -> dict[str, float]: View Source

    32	def cohesion_score(self, source : Symbol, symbols : set[Symbol] ) -> dict[str, float]:
    33		return self.kernel.cohesion_scores( source, symbols )

##### Inherited Members

SyntagmaticRule
  cohesion_scores

@dataclass
class Syntagmatics: View Source

    35@dataclass
    36class Syntagmatics:
    37	"""
    38	Defines the syntagmatic structure encompassing selection,
    39	cohesion, variability, and scope constraints.
    40
    41	Also defines a context (e.g., the model topology) dependent stochastic groupoid
    42	"""
    43	rules : list[SyntagmaticRule] = field(default_factory=list)
    44	t_norm : TNorm = field(default=PRODUCT)
    45
    46	def _combine_cohesion_scores(self, source: str, domain: set[str]) -> dict[str, float]:
    47		active  = [r for r in self.rules if r.applies(domain)]
    48		symbols = set.intersection(*(set(r.symbols) for r in active))
    49		scores  = {s: 1.0 for s in symbols}
    50		for rule in active:
    51			for s, w in rule.cohesion_scores(source, symbols).items():
    52				scores[s] = self.t_norm(scores[s], w)
    53		return scores
    54
    55	def selection_probabilities(self, source: str, domain: set[str]) -> dict[str, float]:
    56		return self._normalize(source, self._combine_cohesion_scores(source, domain))

Defines the syntagmatic structure encompassing selection, cohesion, variability, and scope constraints.

Also defines a context (e.g., the model topology) dependent stochastic groupoid

Syntagmatics( rules: list[SyntagmaticRule] = <factory>, t_norm: Callable[[float, float], float] = <function <lambda>>)
rules: list[SyntagmaticRule]
def t_norm(a, b): View Source

    13PRODUCT     : TNorm = lambda a, b: a * b

def selection_probabilities(self, source: str, domain: set[str]) -> dict[str, float]: View Source

    55	def selection_probabilities(self, source: str, domain: set[str]) -> dict[str, float]:
    56		return self._normalize(source, self._combine_cohesion_scores(source, domain))