Comparators
stickler.comparators
Common comparators for key information evaluation.
This package contains comparators that are shared between the traditional and ANLS Star evaluation systems. These comparators implement a unified interface that works with both systems.
stickler.comparators.BaseComparator
Bases: ABC
Base class for all comparators.
This class defines the interface that all comparators must implement. Comparators are used to compare two values and return a similarity score between 0.0 and 1.0, where 1.0 means the values are identical.
Source code in stickler/comparators/base.py
7 8 9 10 11 12 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 | |
__call__(str1, str2)
Make the comparator callable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
Any
|
First value |
required |
str2
|
Any
|
Second value |
required |
Returns:
| Type | Description |
|---|---|
float
|
Similarity score between 0.0 and 1.0 |
Source code in stickler/comparators/base.py
36 37 38 39 40 41 42 43 44 45 46 | |
__init__(threshold=0.7)
Initialize the comparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Similarity threshold (0.0-1.0) |
0.7
|
Source code in stickler/comparators/base.py
15 16 17 18 19 20 21 | |
__repr__()
Detailed string representation.
Source code in stickler/comparators/base.py
74 75 76 | |
__str__()
String representation for serialization.
Source code in stickler/comparators/base.py
70 71 72 | |
binary_compare(str1, str2)
Compare two values and return a binary result as (tp, fp) tuple.
This method converts the continuous similarity score to a binary decision based on the threshold. If the similarity is greater than or equal to the threshold, it returns (1, 0) indicating true positive. Otherwise, it returns (0, 1) indicating false positive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
Any
|
First value |
required |
str2
|
Any
|
Second value |
required |
Returns:
| Type | Description |
|---|---|
int
|
Tuple of (tp, fp) where tp is 1 if similar, 0 otherwise, |
int
|
and fp is the opposite |
Source code in stickler/comparators/base.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
compare(str1, str2)
abstractmethod
Compare two values and return a similarity score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
Any
|
First value |
required |
str2
|
Any
|
Second value |
required |
Returns:
| Type | Description |
|---|---|
float
|
Similarity score between 0.0 and 1.0 |
Source code in stickler/comparators/base.py
23 24 25 26 27 28 29 30 31 32 33 34 | |
stickler.comparators.ExactComparator
Bases: BaseComparator
Comparator that checks for exact string matching.
This comparator removes whitespace and punctuation before comparison. It returns 1.0 for exact matches and 0.0 otherwise.
Example
comparator = ExactComparator()
# Returns 1.0 (exact match after normalization)
comparator.compare("hello, world!", "hello world")
# Returns 0.0 (different strings)
comparator.compare("hello", "goodbye")
Source code in stickler/comparators/exact.py
9 10 11 12 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 | |
__init__(threshold=1.0, case_sensitive=False)
Initialize the comparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Similarity threshold (default 1.0) |
1.0
|
case_sensitive
|
bool
|
Whether comparison is case sensitive (default False) |
False
|
Source code in stickler/comparators/exact.py
27 28 29 30 31 32 33 34 35 | |
compare(str1, str2)
Compare two values with exact string matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
Any
|
First value |
required |
str2
|
Any
|
Second value |
required |
Returns:
| Type | Description |
|---|---|
float
|
1.0 if the strings match exactly after normalization, 0.0 otherwise |
Source code in stickler/comparators/exact.py
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 | |
stickler.comparators.LevenshteinComparator
Bases: BaseComparator
Comparator using Levenshtein distance for string similarity.
This class implements the Levenshtein distance algorithm for measuring the difference between two strings. It calculates a normalized similarity score between 0 and 1.
Source code in stickler/comparators/levenshtein.py
8 9 10 11 12 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
config
property
Return configuration parameters.
name
property
Return the name of the comparator.
__init__(normalize=True, threshold=0.7)
Initialize the comparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalize
|
bool
|
Whether to normalize input strings (strip whitespace, lowercase) before comparison |
True
|
threshold
|
float
|
Similarity threshold (default 0.7) |
0.7
|
Source code in stickler/comparators/levenshtein.py
16 17 18 19 20 21 22 23 24 25 | |
compare(s1, s2)
Compare two strings using Levenshtein distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s1
|
Any
|
First string or value |
required |
s2
|
Any
|
Second string or value |
required |
Returns:
| Type | Description |
|---|---|
float
|
Similarity score between 0.0 and 1.0, with 1.0 indicating identical |
Raises:
| Type | Description |
|---|---|
TypeError
|
If either input is a dictionary, as dictionaries are not suitable for Levenshtein distance comparison and should be handled through structured models instead. |
Source code in stickler/comparators/levenshtein.py
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 | |
stickler.comparators.NumericComparator
Bases: BaseComparator
Comparator for numeric values with configurable tolerance.
This comparator extracts and compares numeric values from strings or numbers. It supports relative and absolute tolerance for comparison.
Example
# Default exact matching
exact = NumericComparator()
exact.compare("123", "123.0") # Returns 1.0
exact.compare("123", "124") # Returns 0.0
# With tolerance
approx = NumericComparator(relative_tolerance=0.1) # 10% tolerance
approx.compare("100", "109") # Returns 1.0 (within 10%)
approx.compare("100", "111") # Returns 0.0 (beyond 10%)
Source code in stickler/comparators/numeric.py
10 11 12 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
config
property
Return configuration parameters for serialization.
__init__(threshold=1.0, relative_tolerance=0.0, absolute_tolerance=0.0, tolerance=None)
Initialize the comparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Similarity threshold (default 1.0) |
1.0
|
relative_tolerance
|
float
|
Relative tolerance for comparison (default 0.0) |
0.0
|
absolute_tolerance
|
float
|
Absolute tolerance for comparison (default 0.0) |
0.0
|
tolerance
|
Optional[float]
|
Alias for absolute_tolerance (for backward compatibility) |
None
|
Source code in stickler/comparators/numeric.py
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 | |
compare(str1, str2)
Compare two values numerically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
Any
|
First value |
required |
str2
|
Any
|
Second value |
required |
Returns:
| Type | Description |
|---|---|
float
|
1.0 if the numbers match within tolerance, 0.0 otherwise |
Source code in stickler/comparators/numeric.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
stickler.comparators.NumericExactC = NumericComparator
module-attribute
stickler.comparators.FuzzyComparator
Bases: BaseComparator
Comparator for fuzzy string matching.
This comparator uses the rapidfuzz library to calculate similarity between strings using advanced Levenshtein distance calculations. It provides better fuzzy matching than basic Levenshtein for many use cases.
If rapidfuzz is not available, this will raise an ImportError when instantiated.
Source code in stickler/comparators/fuzzy.py
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
config
property
Return configuration parameters.
name
property
Return the name of the comparator.
__init__(method='ratio', normalize=True, threshold=0.7)
Initialize the fuzzy comparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
The fuzzy matching method to use. Options: - "ratio": Standard Levenshtein distance ratio - "partial_ratio": Partial string matching - "token_sort_ratio": Token-based matching with sorting - "token_set_ratio": Token-based matching with set operations |
'ratio'
|
normalize
|
bool
|
Whether to normalize input strings before comparison (strip whitespace, lowercase) |
True
|
threshold
|
float
|
Similarity threshold (default 0.7) |
0.7
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If rapidfuzz library is not available |
Source code in stickler/comparators/fuzzy.py
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 | |
compare(value1, value2)
Compare two strings using fuzzy matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value1
|
Any
|
First string or value |
required |
value2
|
Any
|
Second string or value |
required |
Returns:
| Type | Description |
|---|---|
float
|
Similarity score between 0.0 and 1.0 |
Source code in stickler/comparators/fuzzy.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
stickler.comparators.BERTComparator
Bases: BaseComparator
Comparator that uses BERT embeddings for semantic similarity.
This comparator uses the BERTScore metric to calculate semantic similarity between strings, returning the f1 score as the similarity measure.
Example
comparator = BERTComparator(threshold=0.8)
# Returns similarity score based on semantic similarity
score = comparator.compare("The cat sat on the mat", "A feline was sitting on a rug")
Source code in stickler/comparators/bert.py
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 | |
__init__(threshold=0.7)
Initialize the BERTComparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Similarity threshold (0.0-1.0) |
0.7
|
Source code in stickler/comparators/bert.py
33 34 35 36 37 38 39 40 41 42 43 | |
compare(str1, str2)
Compare two strings using BERT semantic similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
Any
|
First string |
required |
str2
|
Any
|
Second string |
required |
Returns:
| Type | Description |
|---|---|
float
|
Similarity score between 0.0 and 1.0 based on BERTScore f1 |
Source code in stickler/comparators/bert.py
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 | |
stickler.comparators.SemanticComparator
Bases: BaseComparator
Comparator that uses embeddings for semantic similarity.
This comparator uses embeddings from a model (default: Titan) to calculate semantic similarity between strings.
Attributes:
| Name | Type | Description |
|---|---|---|
SIMILARITY_FUNCTIONS |
Dictionary of similarity functions |
|
bc |
BedrockClient instance |
|
model_id |
Model ID to use for embeddings |
|
embedding_function |
Function to generate embeddings |
|
sim_function |
Name of the similarity function to use |
|
similarity_function |
The actual similarity function |
Source code in stickler/comparators/semantic.py
12 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 | |
__init__(model_id='amazon.titan-embed-text-v2:0', sim_function='cosine_similarity', embedding_function=None, threshold=0.7)
Initialize the SemanticComparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Model ID to use for embeddings |
'amazon.titan-embed-text-v2:0'
|
sim_function
|
str
|
Name of the similarity function to use |
'cosine_similarity'
|
embedding_function
|
Optional[Callable]
|
Optional custom embedding function |
None
|
threshold
|
float
|
Similarity threshold (0.0-1.0) |
0.7
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If BedrockClient is not available and no embedding_function is provided |
Source code in stickler/comparators/semantic.py
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 | |
compare(str1, str2)
Compare two strings using semantic similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
str
|
First string |
required |
str2
|
str
|
Second string |
required |
Returns:
| Type | Description |
|---|---|
float
|
Similarity score between 0.0 and 1.0 |
Source code in stickler/comparators/semantic.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
stickler.comparators.LLMComparator
Bases: BaseComparator
Large Language Model-based semantic comparator.
This comparator uses LLMs to perform intelligent semantic comparisons that go beyond simple string matching. It can understand context, handle abbreviations, recognize synonyms, and apply domain-specific comparison logic through custom evaluation guidelines.
The comparator returns binary similarity scores (0.0 or 1.0) based on whether the LLM determines the values are semantically equivalent. It handles edge cases like None values and provides detailed comparison information for debugging.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
Union[Model, str]
|
The LLM model identifier or Model instance. |
eval_guidelines |
str
|
Custom guidelines for comparison logic. |
system_prompt |
str
|
The system prompt used to instruct the LLM. |
prompt_template |
Template
|
Jinja2 template for formatting comparison prompts. |
agent |
Agent
|
The strands Agent instance for LLM interactions. |
threshold |
float
|
Inherited from BaseComparator, used for binary decisions. |
Note
This comparator requires AWS Bedrock access and proper authentication. API calls incur costs and latency, so consider caching for repeated comparisons.
Source code in stickler/comparators/llm.py
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
__init__(model=None, eval_guidelines=None)
Initialize the LLM comparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Union[Model, str]
|
The LLM model to use for comparisons. Can be a model identifier string (e.g., "us.anthropic.claude-3-haiku-20240307-v1:0") or a strands Model instance. Defaults to Claude 3 Haiku. |
None
|
eval_guidelines
|
str
|
Optional custom guidelines to include in the comparison prompt. These guidelines help the LLM understand domain-specific comparison rules (e.g., "Consider abbreviations equivalent"). |
None
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If strands-agents is not installed. |
ValueError
|
If the model parameter is not provided. |
Example
Basic initialization
comparator = LLMComparator()
With custom model and guidelines
comparator = LLMComparator( ... model="us.amazon.nova-lite-v1:0", ... eval_guidelines="Consider street abbreviations equivalent" ... )
Source code in stickler/comparators/llm.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
compare(value1, value2)
Compare two values using LLM-based semantic analysis.
This method converts both values to strings and uses the configured LLM to determine if they are semantically equivalent. The comparison considers context, abbreviations, synonyms, and any provided evaluation guidelines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value1
|
Any
|
First value to compare. Can be any type that converts to string. |
required |
value2
|
Any
|
Second value to compare. Can be any type that converts to string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Binary similarity score: - 1.0 if the LLM determines the values are equivalent - 0.0 if the LLM determines the values are not equivalent - 0.0 if an error occurs during comparison |
Note
- None values: Returns 1.0 if both are None, 0.0 if only one is None
- Error handling: Returns 0.0 for any exceptions during LLM calls
- Cost consideration: Each call incurs API costs and latency
Example
comparator = LLMComparator() comparator.compare("St. John's Street", "Saint John's St") 1.0 comparator.compare("apple", "orange") 0.0 comparator.compare(None, None) 1.0
Source code in stickler/comparators/llm.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
get_comparison_details(value1, value2)
Get detailed information about a comparison operation.
This method provides comprehensive details about the comparison process, including the formatted prompt, LLM response, model information, and final comparison result. Useful for debugging, auditing, and understanding how the LLM made its decision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value1
|
Any
|
First value to compare. Can be any type that converts to string. |
required |
value2
|
Any
|
Second value to compare. Can be any type that converts to string. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary containing comparison details: - 'prompt' (str): The formatted prompt sent to the LLM - 'llm_response' (str): Raw response from the LLM - 'model_id' (Union[Model, str]): The model used (string ID or Model instance) - 'comparison_result' (float): Final similarity score (0.0 or 1.0) On error: - 'error' (str): Error message describing what went wrong - 'comparison_result' (bool): False to indicate failure |
Example
comparator = LLMComparator(eval_guidelines="Consider abbreviations") details = comparator.get_comparison_details("St. John", "Saint John") print(details['llm_response']) 'true' print(details['comparison_result']) 1.0 print('guidelines' in details['prompt']) True
Source code in stickler/comparators/llm.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
stickler.comparators.StructuredModelComparator
Bases: BaseComparator
Comparator for structured model objects.
This comparator is designed to work with StructuredModel instances, leveraging their built-in comparison capabilities.
Source code in stickler/comparators/structured.py
8 9 10 11 12 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 | |
__init__(threshold=0.7, strict_types=False)
Initialize the comparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Similarity threshold (0.0-1.0) |
0.7
|
strict_types
|
bool
|
If True, will raise TypeError when non-StructuredModel objects are compared |
False
|
Source code in stickler/comparators/structured.py
15 16 17 18 19 20 21 22 23 | |
compare(model1, model2)
Compare two structured model instances.
This method uses the built-in compare method of StructuredModel objects if available, otherwise falls back to basic equality comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model1
|
Any
|
First model (ideally a StructuredModel instance) |
required |
model2
|
Any
|
Second model (ideally a StructuredModel instance) |
required |
Returns:
| Type | Description |
|---|---|
float
|
Similarity score between 0.0 and 1.0 |
Raises:
| Type | Description |
|---|---|
TypeError
|
When strict_types=True and comparing non-StructuredModel objects |
Source code in stickler/comparators/structured.py
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 | |