Coverage for src / local_deep_research / advanced_search_system / constraints / base_constraint.py: 53%

37 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-01-11 00:51 +0000

1""" 

2Base constraint classes for query decomposition. 

3""" 

4 

5from dataclasses import dataclass 

6from enum import Enum 

7from typing import Any, Dict 

8 

9 

10class ConstraintType(Enum): 

11 """Types of constraints in queries.""" 

12 

13 PROPERTY = "property" # e.g., "formed during ice age" 

14 NAME_PATTERN = "name_pattern" # e.g., "contains body part" 

15 EVENT = "event" # e.g., "fall between 2000-2021" 

16 STATISTIC = "statistic" # e.g., "84.5x ratio" 

17 TEMPORAL = "temporal" # e.g., "in 2014" 

18 LOCATION = "location" # e.g., "in Colorado" 

19 COMPARISON = "comparison" # e.g., "more than X" 

20 EXISTENCE = "existence" # e.g., "has a viewpoint" 

21 

22 

23@dataclass 

24class Constraint: 

25 """A single constraint extracted from a query.""" 

26 

27 id: str 

28 type: ConstraintType 

29 description: str 

30 value: Any 

31 weight: float = 1.0 # Importance of this constraint 

32 metadata: Dict[str, Any] = None 

33 

34 def __post_init__(self): 

35 """Initialize metadata if not provided.""" 

36 if self.metadata is None: 36 ↛ exitline 36 didn't return from function '__post_init__' because the condition on line 36 was always true

37 self.metadata = {} 

38 

39 def to_search_terms(self) -> str: 

40 """Convert constraint to search terms.""" 

41 if self.type == ConstraintType.PROPERTY: 

42 return self.value 

43 elif self.type == ConstraintType.NAME_PATTERN: 

44 return f"{self.value} name trail mountain" 

45 elif self.type == ConstraintType.EVENT: 

46 return f"{self.value} accident incident" 

47 elif self.type == ConstraintType.STATISTIC: 

48 return f"{self.value} statistics data" 

49 else: 

50 return str(self.value) 

51 

52 def is_critical(self) -> bool: 

53 """Determine if this is a critical constraint that must be satisfied.""" 

54 # Consider NAME_PATTERN constraints as critical regardless of weight 

55 if self.type == ConstraintType.NAME_PATTERN: 

56 return True 

57 # Otherwise use weight to determine criticality 

58 return self.weight > 0.8