Coverage for geoparser/modules/recognizers/manual.py: 100%
19 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 18:08 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 18:08 +0000
1import typing as t
2from typing import List, Tuple
4from geoparser.modules.recognizers import Recognizer
6if t.TYPE_CHECKING:
7 pass
10class ManualRecognizer(Recognizer):
11 """
12 A recognition module for manually annotated references.
14 This module allows manual reference annotations to be integrated into the
15 geoparsing workflow. Each instance is identified by a label, enabling
16 multiple annotation sets (e.g., from different annotators) to be stored
17 for the same documents.
19 The annotations are provided at initialization and returned by predict()
20 in the same order as the input documents. The label is stored in the database
21 config to differentiate between different annotation sets.
22 """
24 NAME = "ManualRecognizer"
26 def __init__(
27 self, label: str, texts: List[str], references: List[List[Tuple[int, int]]]
28 ):
29 """
30 Initialize the ManualRecognizer with a label and reference annotations.
32 Args:
33 label: Identifier for this annotation set (e.g., "annotator_A", "expert_1").
34 Different labels create separate recognizer instances in the database.
35 texts: List of document text strings corresponding to the annotations.
36 references: List of reference annotations, where each element is a list of
37 (start, end) tuples representing reference spans. Each annotation
38 set corresponds to the document text at the same position in texts.
39 """
40 # Only label goes to config and database
41 super().__init__(label=label)
43 # Store as instance attributes
44 self.label = label
45 self.texts = texts
46 self.references = references
48 def predict(
49 self, texts: t.List[str]
50 ) -> t.List[t.Union[t.List[t.Tuple[int, int]], None]]:
51 """
52 Return the manually provided reference annotations for the given documents.
54 This method matches each input text to the corresponding annotation by looking
55 up the text in the stored texts list. For texts that don't have annotations,
56 None is returned to indicate that no annotation is available (as opposed to
57 an empty list which would indicate that the document was annotated as having
58 no references).
60 Args:
61 texts: List of document text strings to annotate
63 Returns:
64 A list where each element is either a list of (start, end) tuples representing
65 reference spans for annotated documents, or None for documents without annotations
66 (which won't be marked as processed).
67 """
68 results = []
69 for text in texts:
70 try:
71 idx = self.texts.index(text)
72 results.append(self.references[idx])
73 except ValueError:
74 # Text not in stored annotations - return None
75 # This signals to the service that no annotation is available
76 results.append(None)
78 return results