Skip to content

Barrier Analyzer

Bases: ABC

Class for identifying language barriers.

Source code in src/limes/analyzers/interfaces.py
class BarrierAnalyzer(ABC):
    """
    Class for identifying language barriers.
    """

    def __init__(self, simplify_explanations: bool = True):
        """
        Parameters
        ----------
        simplify_explanations : bool, optional
            If True, use simplified barrier descriptions; otherwise use
            standard descriptions. Defaults to True.
        """
        self._description_style = (
            BarrierDescriptionStyle.SIMPLIFIED
            if simplify_explanations
            else BarrierDescriptionStyle.STANDARD
        )

    def __call__(self, sentence: DocumentProtocol) -> list[Barrier]:
        """
        Automatically identify and run all public detection functions of the
        Analyzer. Overwrite this with concrete registration of all relevant
        callables for minor overhead reduction.
        """
        # Get all methods of the class that match the required signature.
        methods = self._get_methods_with_signature()
        if len(methods) == 0:
            raise AttributeError(
                "The class did not identify any detection functions to be used."
            )

        barriers = []
        for method in methods:
            barriers.extend(method(sentence))
        return barriers

    def _make_barrier(
        self,
        template: BarrierTemplate,
        affected_tokens: list[TokenProtocol] | None = None,
    ) -> Barrier:
        """
        Materialize a Barrier instance with the configured description style.
        """
        return template.to_barrier(
            description_style=self._description_style,
            affected_tokens=affected_tokens,
        )

    def _get_methods_with_signature(self):
        """
        Identify functions contained in the given class that follow the detector
        function format.
        """
        # Get all methods of the class.
        methods = inspect.getmembers(self, predicate=inspect.ismethod)

        # Filter methods that match barrier detection function signature.
        filtered_methods = []
        for name, method in methods:
            # Only evaluate classes that have a name that indicates relevance.
            if not name.startswith("detect"):
                continue
            sig = inspect.signature(method)
            # Skip methods where the function parameters don't match.
            if not len(sig.parameters) == 1 or "sentence" not in sig.parameters:
                continue
            # Skip methods where return type does not match.
            return_type = sig.return_annotation
            if not return_type == list[Barrier]:
                continue

            filtered_methods.append(method)
        return filtered_methods

    @property
    @abstractmethod
    def supported_barriers(self) -> list[Barrier]:
        """
        A list of all types of barriers that this analyzer can detect.
        """
        ...

supported_barriers abstractmethod property

A list of all types of barriers that this analyzer can detect.

__init__(simplify_explanations=True)

Parameters:

Name Type Description Default
simplify_explanations bool

If True, use simplified barrier descriptions; otherwise use standard descriptions. Defaults to True.

True
Source code in src/limes/analyzers/interfaces.py
def __init__(self, simplify_explanations: bool = True):
    """
    Parameters
    ----------
    simplify_explanations : bool, optional
        If True, use simplified barrier descriptions; otherwise use
        standard descriptions. Defaults to True.
    """
    self._description_style = (
        BarrierDescriptionStyle.SIMPLIFIED
        if simplify_explanations
        else BarrierDescriptionStyle.STANDARD
    )

__call__(sentence)

Automatically identify and run all public detection functions of the Analyzer. Overwrite this with concrete registration of all relevant callables for minor overhead reduction.

Source code in src/limes/analyzers/interfaces.py
def __call__(self, sentence: DocumentProtocol) -> list[Barrier]:
    """
    Automatically identify and run all public detection functions of the
    Analyzer. Overwrite this with concrete registration of all relevant
    callables for minor overhead reduction.
    """
    # Get all methods of the class that match the required signature.
    methods = self._get_methods_with_signature()
    if len(methods) == 0:
        raise AttributeError(
            "The class did not identify any detection functions to be used."
        )

    barriers = []
    for method in methods:
        barriers.extend(method(sentence))
    return barriers