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.DateComparator
Bases: BaseComparator
Deterministic date comparator with year/range awareness.
See docs/docs/Guides/Comparators/date-comparator.md for the full
behavior reference, configuration matrix, and corner cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Forwarded to :class: |
1.0
|
tolerance
|
Optional[Union[timedelta, int, float]]
|
Optional window for Tier 1 single-vs-single
comparisons only (range and partial-year branches ignore it).
Accepts a |
None
|
dayfirst
|
Optional[bool]
|
How to interpret ambiguous numeric dates like
|
None
|
allow_partial_year
|
bool
|
If |
False
|
range_mode
|
RangeMode
|
How range comparisons are scored. One of
|
'graded'
|
precision_mode
|
PrecisionMode
|
How month/day resolution mismatches are scored
(
|
'exact'
|
Source code in stickler/comparators/date.py
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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
config
property
Round-trippable config for JSON-schema export.
Only non-default values are emitted, and an all-default instance
returns None — matching NumericComparator.config and
keeping a redundant x-aws-stickler-comparator-config block out
of every exported schema (the exporter keys off truthiness).
Tolerance is exported as days (an int when the timedelta is a whole number of days, otherwise a float) so it can survive a JSON round-trip.
compare(str1, str2)
Score two date values per the tier system documented above.
Source code in stickler/comparators/date.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
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
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 | |
__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
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 | |
compare(str1, str2)
Compare two values using semantic similarity.
If embedding generation fails, this logs the model ID, embedding function, input lengths, similarity function, and exception type before falling back to raw equality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str1
|
str
|
First value |
required |
str2
|
str
|
Second value |
required |
Returns:
| Type | Description |
|---|---|
float
|
Similarity score between 0.0 and 1.0 |
Source code in stickler/comparators/semantic.py
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 | |
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 | |
stickler.comparators.BBoxIoUComparator
Bases: BaseComparator
Comparator for bounding boxes using Intersection over Union.
Compares two bounding boxes and returns their IoU as a similarity score between 0.0 and 1.0.
Bounding box formats accepted
- Two-point: [[x1, y1], [x2, y2]]
- Flat: [x1, y1, x2, y2]
Coordinates must be finite numbers; non-finite values (NaN, inf) are
treated as malformed input and score 0.0. Booleans are accepted as
coordinates (bool is a subclass of int: True == 1, False
== 0), so guard upstream if that is not intended. Note that a zero-area
box (a point, e.g. [[5, 5], [5, 5]]) has no area to intersect, so it
scores IoU 0.0 even against an identical point — relevant when annotating
point locations rather than regions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
IoU threshold for binary match classification (default: 0.5). |
0.5
|
Example
from stickler.comparators.bbox import BBoxIoUComparator cmp = BBoxIoUComparator(threshold=0.5) cmp.compare([[0, 0], [10, 10]], [[0, 0], [10, 10]]) 1.0 cmp.compare([[0, 0], [5, 5]], [[5, 5], [10, 10]]) 0.0
Source code in stickler/comparators/bbox.py
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 | |
compare(bbox1, bbox2)
Compare two bounding boxes and return their IoU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox1
|
Any
|
First bounding box (prediction). |
required |
bbox2
|
Any
|
Second bounding box (ground truth). |
required |
Returns:
| Type | Description |
|---|---|
float
|
IoU score between 0.0 and 1.0. |
Source code in stickler/comparators/bbox.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |