Coverage for geoparser/db/models/reference.py: 100%

37 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 18:08 +0000

1import typing as t 

2import uuid 

3from typing import Optional 

4 

5from sqlalchemy import UUID, Column, ForeignKey, String 

6from sqlmodel import Field, Relationship, SQLModel 

7 

8from geoparser.db.models.base import GeoparserModel 

9 

10if t.TYPE_CHECKING: 

11 from geoparser.db.models.document import Document 

12 from geoparser.db.models.recognizer import Recognizer 

13 from geoparser.db.models.referent import Referent 

14 from geoparser.db.models.resolution import Resolution 

15 from geoparser.gazetteer.feature import Feature 

16 

17 

18class ReferenceBase(GeoparserModel): 

19 """Base model for reference data.""" 

20 

21 start: int # Start position of the reference in the document text 

22 end: int # End position of the reference in the document text 

23 text: t.Optional[str] = None # The actual text of the reference 

24 

25 

26class Reference(ReferenceBase, table=True): 

27 """ 

28 Represents a reference (place name) identified in a document. 

29 

30 A reference is a place name found in text, defined by its start and end positions. 

31 It can have multiple potential referent interpretations (resolved referents). 

32 """ 

33 

34 id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) 

35 document_id: uuid.UUID = Field( 

36 sa_column=Column( 

37 UUID, ForeignKey("document.id", ondelete="CASCADE"), nullable=False 

38 ) 

39 ) 

40 recognizer_id: str = Field( 

41 sa_column=Column( 

42 String, ForeignKey("recognizer.id", ondelete="CASCADE"), nullable=False 

43 ) 

44 ) 

45 document: "Document" = Relationship(back_populates="references") 

46 recognizer: "Recognizer" = Relationship( 

47 back_populates="references", sa_relationship_kwargs={"lazy": "joined"} 

48 ) 

49 referents: list["Referent"] = Relationship( 

50 back_populates="reference", 

51 sa_relationship_kwargs={ 

52 "cascade": "all, delete-orphan", 

53 "passive_deletes": True, 

54 "lazy": "joined", # Enable eager loading 

55 }, 

56 ) 

57 resolutions: list["Resolution"] = Relationship( 

58 back_populates="reference", 

59 sa_relationship_kwargs={ 

60 "cascade": "all, delete-orphan", 

61 "passive_deletes": True, 

62 }, 

63 ) 

64 

65 _resolver_id: Optional[str] = None 

66 

67 def _set_resolver_context(self, resolver_id: str = None): 

68 """ 

69 Internal method to set the viewing context for referents. 

70 

71 Args: 

72 resolver_id: ID of the resolver to use for filtering referents 

73 """ 

74 self._resolver_id = resolver_id 

75 

76 @property 

77 def location(self) -> Optional["Feature"]: 

78 """ 

79 Return the referent feature from the resolver configured in the context. 

80 

81 Returns: 

82 Referent feature object from the configured resolver, or None if no resolver 

83 is configured or no matching referent is found 

84 """ 

85 # Get resolver_id from context 

86 if self._resolver_id is None: 

87 return None 

88 

89 # Find referent from the configured resolver 

90 for referent in self.referents: 

91 if referent.resolver_id == self._resolver_id: 

92 return referent.feature 

93 

94 return None 

95 

96 def __str__(self) -> str: 

97 """ 

98 Return a string representation of the reference. 

99 

100 Returns: 

101 String with reference indicator and text content 

102 """ 

103 return f'Reference("{self.text}")' 

104 

105 def __repr__(self) -> str: 

106 """ 

107 Return a developer representation of the reference. 

108 

109 Returns: 

110 Same as __str__ method 

111 """ 

112 return self.__str__() 

113 

114 

115class ReferenceCreate(ReferenceBase): 

116 """ 

117 Model for creating a new reference. 

118 

119 Includes the document_id and recognizer_id to associate the reference. 

120 """ 

121 

122 document_id: uuid.UUID 

123 recognizer_id: str 

124 

125 

126class ReferenceUpdate(SQLModel): 

127 """Model for updating an existing reference.""" 

128 

129 id: uuid.UUID 

130 document_id: t.Optional[uuid.UUID] = None 

131 recognizer_id: t.Optional[str] = None 

132 start: t.Optional[int] = None 

133 end: t.Optional[int] = None