Coverage for geoparser/modules/resolvers/manual.py: 100%

29 statements  

« 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 

3 

4from geoparser.modules.resolvers import Resolver 

5 

6if t.TYPE_CHECKING: 

7 pass 

8 

9 

10class ManualResolver(Resolver): 

11 """ 

12 A resolution module for manually annotated referents. 

13 

14 This module allows manual referent annotations (links to gazetteer features) 

15 to be integrated into the geoparsing workflow. Each instance is identified by 

16 a label, enabling multiple annotation sets (e.g., from different annotators) 

17 to be stored for the same references. 

18 

19 The annotations are provided at initialization and returned by predict() 

20 in the same order as the input references. The label is stored in the database 

21 config to differentiate between different annotation sets. 

22 """ 

23 

24 NAME = "ManualResolver" 

25 

26 def __init__( 

27 self, 

28 label: str, 

29 texts: List[str], 

30 references: List[List[Tuple[int, int]]], 

31 referents: List[List[Tuple[str, str]]], 

32 ): 

33 """ 

34 Initialize the ManualResolver with a label and referent annotations. 

35 

36 Args: 

37 label: Identifier for this annotation set (e.g., "annotator_A", "expert_1"). 

38 Different labels create separate resolver instances in the database. 

39 texts: List of document text strings corresponding to the annotations. 

40 references: List of lists of (start, end) tuples representing reference positions. 

41 Each inner list corresponds to references in one document. 

42 referents: List of lists of (gazetteer_name, identifier) tuples representing the 

43 resolved referents. Each annotation corresponds to the document and 

44 reference at the same positions in texts and references. 

45 """ 

46 # Only label goes to config and database 

47 super().__init__(label=label) 

48 

49 # Store as instance attributes 

50 self.label = label 

51 self.texts = texts 

52 self.references = references 

53 self.referents = referents 

54 

55 def predict( 

56 self, texts: t.List[str], references: t.List[t.List[t.Tuple[int, int]]] 

57 ) -> t.List[t.List[t.Union[t.Tuple[str, str], None]]]: 

58 """ 

59 Return the manually provided referent annotations for the given references. 

60 

61 This method matches each input text to the corresponding annotation by looking 

62 up the text in the stored texts list, then matches each reference within that 

63 document to find the corresponding referent annotation. For references without 

64 annotations, None is returned. 

65 

66 Args: 

67 texts: List of document text strings 

68 references: List of lists of (start, end) position tuples 

69 

70 Returns: 

71 A list of lists where each element is either a tuple (gazetteer_name, identifier) 

72 for annotated references, or None for references without annotations (which won't 

73 be marked as processed). 

74 """ 

75 results = [] 

76 for text, doc_references in zip(texts, references): 

77 try: 

78 text_idx = self.texts.index(text) 

79 stored_references = self.references[text_idx] 

80 stored_referents = self.referents[text_idx] 

81 

82 doc_results = [] 

83 for reference in doc_references: 

84 try: 

85 reference_idx = stored_references.index(reference) 

86 doc_results.append(stored_referents[reference_idx]) 

87 except ValueError: 

88 # Reference not in stored annotations - return None 

89 # This signals to the service that no annotation is available 

90 doc_results.append(None) 

91 

92 results.append(doc_results) 

93 except ValueError: 

94 # Text not in stored annotations - return None for all references in this document 

95 results.append([None] * len(doc_references)) 

96 

97 return results