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
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 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 | |
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
15 16 17 18 19 20 21 22 23 24 | |
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
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 | |
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 | |
__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
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 | |
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
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 | |
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
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 | |
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
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 | |
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
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__(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
32 33 34 35 36 37 38 39 40 41 42 | |
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
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 | |
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
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 | |
__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
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 | |
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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
stickler.comparators.LLMComparator
Bases: BaseComparator
Comparator that uses LLM to determine semantic equivalence.
This comparator uses an LLM to determine if two values are semantically equivalent, returning 1.0 if True and 0.0 if False.
Attributes:
| Name | Type | Description |
|---|---|---|
prompt |
Prompt template to use for comparison |
|
model_id |
Model ID to use for LLM |
|
temp |
Temperature for LLM inference |
|
system_prompt |
System prompt for the LLM |
Source code in stickler/comparators/llm.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 67 68 69 70 71 72 73 74 75 | |
__init__(prompt, model_id, temp=0.5, threshold=0.5)
Initialize the LLMComparator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
Prompt template to use for comparison |
required |
model_id
|
str
|
Model ID to use for LLM |
required |
temp
|
float
|
Temperature for LLM inference |
0.5
|
threshold
|
float
|
Similarity threshold (0.0-1.0) |
0.5
|
Source code in stickler/comparators/llm.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
compare(str1, str2)
Compare two values using LLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
Any
|
First value |
required |
str2
|
Any
|
Second value |
required |
Returns:
| Type | Description |
|---|---|
float
|
1.0 if LLM determines values are equivalent, 0.0 otherwise |
Source code in stickler/comparators/llm.py
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 | |
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 | |