Metrics
rankify.metrics.metrics
BaseMetric
Base class for evaluation metrics.
Attributes:
| Name | Type | Description |
|---|---|---|
metric_name |
str
|
Name of the metric. |
config |
dict
|
Configuration dictionary. |
dataset_name |
str
|
Name of the dataset used for evaluation. |
Methods:
| Name | Description |
|---|---|
calculate_metric |
Computes the evaluation metric. |
get_dataset_answer |
Extracts ground-truth answers from dataset. |
Source code in rankify/metrics/metrics.py
__init__(config)
Initializes the base metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration dictionary. |
required |
calculate_metric(data)
Calculates the metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Data object containing predictions and ground truth. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Metric scores and individual scores for each instance. |
get_dataset_answer(data)
Extracts ground-truth answers from dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Data object containing documents. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
List of ground-truth answers. |
Source code in rankify/metrics/metrics.py
ExactMatch
Bases: BaseMetric
Computes Exact Match (EM) Score.
The Exact Match metric checks whether the predicted answer exactly matches one of the ground-truth answers.
Source code in rankify/metrics/metrics.py
calculate_em(prediction, golden_answers)
Computes Exact Match score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction
|
str
|
The predicted answer. |
required |
golden_answers
|
list
|
List of reference answers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
1.0 if there is an exact match, else 0.0. |
Source code in rankify/metrics/metrics.py
calculate_metric(data)
Computes the Exact Match (EM) score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Data object containing predictions and ground-truth answers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
{"exact_match": score} |
|
list |
List of EM scores per prediction. |
Source code in rankify/metrics/metrics.py
F1Score
Bases: BaseMetric
Computes F1 Score, Precision, and Recall.
The F1 Score is the harmonic mean of precision and recall, commonly used in QA evaluation.
Source code in rankify/metrics/metrics.py
token_level_scores(prediction, ground_truths)
Computes F1, Precision, and Recall scores for a prediction-ground-truth pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction
|
str
|
The predicted answer. |
required |
ground_truths
|
list
|
List of reference answers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
{"f1": score, "precision": score, "recall": score} |
Source code in rankify/metrics/metrics.py
PrecisionScore
Bases: F1Score
Computes Precision Score.
Precision is the fraction of retrieved documents that are relevant.
Source code in rankify/metrics/metrics.py
RecallScore
Bases: F1Score
Computes Recall Score.
Recall is the fraction of relevant documents that are retrieved.
Source code in rankify/metrics/metrics.py
ContainsMatch
Bases: BaseMetric
Computes Contains Match metric.
This metric checks whether any reference answer is contained within the predicted answer.
Source code in rankify/metrics/metrics.py
BLEUScore
Bases: BaseMetric
Computes BLEU Score for evaluating text generation.
BLEU (Bilingual Evaluation Understudy) measures the similarity between machine-generated text and reference translations by analyzing overlapping n-grams.
References
- Papineni et al. (2002): BLEU: A Method for Automatic Evaluation of Machine Translation.
Attributes:
| Name | Type | Description |
|---|---|---|
metric_name |
str
|
The metric name ( |
max_order |
int
|
Maximum n-gram order to consider (default: |
smooth |
bool
|
Whether to apply smoothing to prevent zero precision (default: |
Methods:
| Name | Description |
|---|---|
compute_bleu |
Computes BLEU score. |
_get_ngrams |
Extracts n-grams from text. |
calculate_metric |
Computes BLEU score for generated text. |
Source code in rankify/metrics/metrics.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 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 | |
__init__(config)
Initializes the BLEU Score metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Configuration dictionary containing:
- |
required |
Source code in rankify/metrics/metrics.py
compute_bleu(reference_corpus, translation_corpus)
Computes BLEU Score by comparing generated translations with reference translations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference_corpus
|
List[List[List[str]]]
|
A list of reference translations, where each reference is a list of tokenized reference sentences. |
required |
translation_corpus
|
List[List[str]]
|
A list of model-generated translations, tokenized into words. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
BLEU score (0 to 1 range, higher is better). |
Source code in rankify/metrics/metrics.py
calculate_metric(data)
Computes BLEU Score for generated text compared to reference answers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
A data object containing:
- |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
|
Source code in rankify/metrics/metrics.py
Metrics
Computes Retrieval & Generation Metrics.
Attributes:
| Name | Type | Description |
|---|---|---|
documents |
list
|
The list of Document instances. |
config |
dict
|
Configuration dictionary. |
Methods:
| Name | Description |
|---|---|
top_k_accuracy |
Computes Top-K Accuracy. |
calculate_retrieval_metrics |
Computes retrieval performance at multiple K. |
calculate_generation_metrics |
Computes generation evaluation scores. |
Source code in rankify/metrics/metrics.py
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 | |
__init__(documents)
Initializes the Metrics class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
list
|
List of Document instances. |
required |
top_k_accuracy(k, use_reordered=False)
Computes Top-K retrieval accuracy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The K value (e.g., Top-5, Top-10). |
required |
use_reordered
|
bool
|
Whether to use reranked contexts. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
Top-K accuracy (percentage). |
Source code in rankify/metrics/metrics.py
calculate_retrieval_metrics(ks=[1, 5, 10, 20, 50, 100], use_reordered=False)
Computes Top-K retrieval metrics for multiple values of K.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ks
|
list
|
List of K values (default: |
[1, 5, 10, 20, 50, 100]
|
use_reordered
|
bool
|
Whether to use reranked results. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dictionary with Top-K accuracy scores. |
Source code in rankify/metrics/metrics.py
calculate_generation_metrics(predictions)
Computes generation evaluation metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
list
|
List of model predictions. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dictionary with Exact Match, F1, Precision, Recall, Contains Match scores. |
Source code in rankify/metrics/metrics.py
generate_trec_format(use_reordered=False)
Converts Documents into TREC format for evaluation.
Returns:
| Name | Type | Description |
|---|---|---|
str |
String formatted in TREC format. |
Source code in rankify/metrics/metrics.py
calculate_trec_metrics(ndcg_cuts=[10], map_cuts=[100], mrr_cuts=[10], qrel='dl19', use_reordered=False)
Computes NDCG, MAP, and MRR using Pyserini's trec_eval command-line tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ndcg_cuts
|
list
|
List of NDCG@k values (default: |
[10]
|
map_cuts
|
list
|
List of MAP@k values (default: |
[100]
|
mrr_cuts
|
list
|
List of MRR@k values (default: |
[10]
|
qrel
|
str
|
The dataset key (default: |
'dl19'
|
use_reordered
|
bool
|
Whether to use reranked contexts. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dictionary containing NDCG@k, MAP@k, and MRR@k scores. |
Source code in rankify/metrics/metrics.py
run_trec_eval(command)
Runs trec_eval and extracts the metric score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
Extracted metric score. |
Source code in rankify/metrics/metrics.py
parse_trec_output(output)
Parses the output from trec_eval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
str
|
Raw text output from |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
Extracted metric score. |
Source code in rankify/metrics/metrics.py
normalize_answer(s)
Normalizes an answer string by removing punctuation, articles, and extra whitespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The answer string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
Normalized answer string. |