Skip to content

EntityRecognizer

Abstract Base class for recognizing entities in a batch of text. Used in the recon.insights module for understanding the kinds of examples your model is having the most trouble with.

Source code in recon/recognizer.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class EntityRecognizer:
    """Abstract Base class for recognizing entities in a batch of text.
    Used in the `recon.insights` module for understanding the kinds
    of examples your model is having the most trouble with.
    """

    @property
    def labels(self) -> List[str]:
        """Return List of String Labels

        Raises:
            NotImplementedError: Not Implemented, override

        Returns:
            List[str]: List of labels the model can predict
        """
        raise NotImplementedError

    def predict(self, texts: Iterable[str]) -> Iterator[Example]:
        """Run model inference on a batch of raw texts.

        Args:
            texts (Iterable[str]): Raw text examples

        Raises:
            NotImplementedError: Not implemented, override

        Returns:
            Iterator[Example]: Iterator of Examples
        """
        raise NotImplementedError

    def _evaluate(self, __examples: List[Example]) -> Scores:
        raise NotImplementedError

    def evaluate(self, data: List[Example], verbose: bool = True) -> Scores:
        """Evaluate recognizer performance on dataset and print metrics

        Args:
            data (List[Example]): Examples to evaluate on
            verbose (bool, optional): Print results or not. Defaults to True.

        Returns:
            Scorer: spaCy scorer object
        """
        sc = self._evaluate(data)

        msg = Printer(no_print=not verbose)
        msg.divider("Recognizer Results")
        result = [
            ("Precision", f"{sc.ents_p:.3f}"),
            ("Recall", f"{sc.ents_r:.3f}"),
            ("F-Score", f"{sc.ents_f:.3f}"),
        ]
        msg.table(result)

        table_data = []
        for label, scores in sorted(sc.ents_per_type.items(), key=lambda tup: tup[0]):
            table_data.append(
                (
                    label,
                    f"{scores['p']:.3f}",
                    f"{scores['r']:.3f}",
                    f"{scores['f']:.3f}",
                )
            )
        header = ("Label", "Precision", "Recall", "F-Score")
        msg.table(table_data, header=header, divider=True)
        return sc

labels: List[str] property

Return List of String Labels

Raises:

Type Description
NotImplementedError

Not Implemented, override

Returns:

Type Description
List[str]

List[str]: List of labels the model can predict

evaluate(data, verbose=True)

Evaluate recognizer performance on dataset and print metrics

Parameters:

Name Type Description Default
data List[Example]

Examples to evaluate on

required
verbose bool

Print results or not. Defaults to True.

True

Returns:

Name Type Description
Scorer Scores

spaCy scorer object

Source code in recon/recognizer.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def evaluate(self, data: List[Example], verbose: bool = True) -> Scores:
    """Evaluate recognizer performance on dataset and print metrics

    Args:
        data (List[Example]): Examples to evaluate on
        verbose (bool, optional): Print results or not. Defaults to True.

    Returns:
        Scorer: spaCy scorer object
    """
    sc = self._evaluate(data)

    msg = Printer(no_print=not verbose)
    msg.divider("Recognizer Results")
    result = [
        ("Precision", f"{sc.ents_p:.3f}"),
        ("Recall", f"{sc.ents_r:.3f}"),
        ("F-Score", f"{sc.ents_f:.3f}"),
    ]
    msg.table(result)

    table_data = []
    for label, scores in sorted(sc.ents_per_type.items(), key=lambda tup: tup[0]):
        table_data.append(
            (
                label,
                f"{scores['p']:.3f}",
                f"{scores['r']:.3f}",
                f"{scores['f']:.3f}",
            )
        )
    header = ("Label", "Precision", "Recall", "F-Score")
    msg.table(table_data, header=header, divider=True)
    return sc

predict(texts)

Run model inference on a batch of raw texts.

Parameters:

Name Type Description Default
texts Iterable[str]

Raw text examples

required

Raises:

Type Description
NotImplementedError

Not implemented, override

Returns:

Type Description
Iterator[Example]

Iterator[Example]: Iterator of Examples

Source code in recon/recognizer.py
31
32
33
34
35
36
37
38
39
40
41
42
43
def predict(self, texts: Iterable[str]) -> Iterator[Example]:
    """Run model inference on a batch of raw texts.

    Args:
        texts (Iterable[str]): Raw text examples

    Raises:
        NotImplementedError: Not implemented, override

    Returns:
        Iterator[Example]: Iterator of Examples
    """
    raise NotImplementedError