Dataset
rankify.dataset.dataset
DownloadManger
Source code in rankify/utils/dataset/download.py
Question
Represents a question with automatic validation.
Attributes:
| Name | Type | Description |
|---|---|---|
question |
str
|
The text of the question. |
Source code in rankify/dataset/dataset.py
__init__(question)
Initializes a Question instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The text of the question. |
required |
Example
Source code in rankify/dataset/dataset.py
check_question(question)
classmethod
Ensures the question ends with a question mark.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The text of the question. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The question with a question mark at the end if it was missing. |
Example
Source code in rankify/dataset/dataset.py
__str__()
Returns a string representation of the Question instance.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted question. |
Example
Source code in rankify/dataset/dataset.py
Answer
Represents answers to a question.
Attributes:
| Name | Type | Description |
|---|---|---|
answers |
list[str]
|
A list of possible answers. |
Source code in rankify/dataset/dataset.py
__init__(answers=None)
Initializes an Answer instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
answers
|
list[str] or str
|
A list of possible answers. Defaults to None. |
None
|
Source code in rankify/dataset/dataset.py
__str__()
Returns a string representation of the Answer instance.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted answers. |
Source code in rankify/dataset/dataset.py
Context
Represents a context with metadata such as score and title.
Attributes:
| Name | Type | Description |
|---|---|---|
score |
float
|
The relevance score of the context. |
has_answer |
bool
|
Whether the context contains an answer. |
id |
int
|
The identifier of the context. |
title |
str
|
The title of the context. |
text |
str
|
The text of the context. |
Source code in rankify/dataset/dataset.py
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 | |
__init__(score=None, has_answer=None, id=None, title=None, text=None)
Initializes a Context instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score
|
float
|
The relevance score. |
None
|
has_answer
|
bool
|
Whether the context contains an answer. |
None
|
id
|
int
|
The identifier of the context. |
None
|
title
|
str
|
The title of the context. |
None
|
text
|
str
|
The text of the context. |
None
|
Example
Source code in rankify/dataset/dataset.py
to_dict(save_text=False)
Converts the Context instance to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_text
|
bool
|
Whether to include text in the output dictionary. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Optional[object]]
|
The context data. |
Example
Source code in rankify/dataset/dataset.py
__str__()
Returns a string representation of the Context instance.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted context. |
Example
Source code in rankify/dataset/dataset.py
Document
Represents a document consisting of a question, answers, and contexts.
Attributes:
| Name | Type | Description |
|---|---|---|
question |
Question
|
The question associated with the document. |
answers |
Answer
|
The answers to the question. |
contexts |
list[Context]
|
A list of related contexts. |
reorder_contexts |
list[Context] or None
|
A reordered list of contexts based on relevance. |
Source code in rankify/dataset/dataset.py
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 | |
__init__(question, answers, contexts=None, id=None)
Initializes a Document instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
Question
|
The question associated with the document. |
required |
answers
|
Answer
|
The answers to the question. |
required |
contexts
|
list[Context]
|
A list of contexts related to the question. |
None
|
Example
q = Question("What is the capital of France?")
a = Answer(["Paris"])
c1 = Context(score=0.9, has_answer=True, id=1, title="Paris", text="The capital of France is Paris.")
c2 = Context(score=0.5, has_answer=False, id=2, title="Berlin", text="Berlin is the capital of Germany.")
d = Document(question=q, answers=a, contexts=[c1, c2])
print(d)
Source code in rankify/dataset/dataset.py
from_dict(data, n_docs=100)
classmethod
Creates a Document instance from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
A dictionary containing the question, answers, and contexts. |
required |
n_docs
|
int
|
The number of contexts to include. Defaults to 100. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
Document |
Document
|
A new Document instance. |
Example
data = {
"question": "What is the capital of France?",
"answers": ["Paris"],
"ctxs": [
{"score": 0.9, "has_answer": True, "id": 1, "title": "Paris", "text": "The capital of France is Paris."},
{"score": 0.5, "has_answer": False, "id": 2, "title": "Berlin", "text": "Berlin is the capital of Germany."}
]
}
d = Document.from_dict(data)
print(d.question)
Source code in rankify/dataset/dataset.py
to_dict()
Converts the document into a dictionary representation.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Optional[object]]
|
A dictionary containing the question, answers, and contexts. |
Source code in rankify/dataset/dataset.py
__str__()
Returns a string representation of the Document instance.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted document information. |
Source code in rankify/dataset/dataset.py
Dataset
Represents a dataset for information retrieval.
Attributes:
| Name | Type | Description |
|---|---|---|
retriever |
str
|
The name of the retriever used to obtain the dataset. |
dataset_name |
str
|
The name of the dataset. |
n_docs |
int
|
The number of documents to include. |
Source code in rankify/dataset/dataset.py
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 | |
__init__(retriever, dataset_name, n_docs=1000)
Initializes a Dataset instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retriever
|
str
|
The name of the retriever used to obtain the dataset. |
required |
dataset_name
|
str
|
The name of the dataset. |
required |
n_docs
|
int
|
The number of documents to include. Defaults to 1000. |
1000
|
Example
Source code in rankify/dataset/dataset.py
update_contexts_from_passages()
Updates the text and title fields of contexts in all documents using the passages TSV file. If the TSV file is not already cached, it will be downloaded.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset has not been loaded before calling this method. |
Example
Source code in rankify/dataset/dataset.py
download(force_download=True)
Downloads the dataset and loads it into memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force_download
|
bool
|
Whether to force downloading the dataset even if it already exists locally. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
List[Document]
|
list[Document]: A list of Document instances loaded from the dataset. |
Example
Source code in rankify/dataset/dataset.py
load_dataset(filepath, n_docs=100)
classmethod
Loads the dataset from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
The path to the JSON file containing the dataset. |
required |
n_docs
|
int
|
The number of documents to load. Defaults to 100. |
100
|
Returns:
| Type | Description |
|---|---|
List[Document]
|
list[Document]: A list of Document instances loaded from the JSON file. |
Example
Source code in rankify/dataset/dataset.py
load_dataset_qa(filepath)
classmethod
Loads a QA dataset from a JSON or JSONL file. The dataset contains only questions, optionally answers, and optionally IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
The path to the JSON or JSONL file containing the dataset. |
required |
Returns:
| Type | Description |
|---|---|
List[Document]
|
List[Document]: A list of Document objects, each containing:
- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file format is not supported (only .json and .jsonl are allowed) or if required fields are missing. |
Example
Source code in rankify/dataset/dataset.py
save_dataset(output_path, save_reranked=False, save_text=False)
Saves the re-ranked documents in DPR format to a JSON or JSONL file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
str
|
The path to the output file (must be .json or .jsonl). |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
Source code in rankify/dataset/dataset.py
save_documents(documents, output_path, save_reranked=False, save_text=False)
staticmethod
Saves a list of Document objects in DPR format to a JSON or JSONL file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
List[Document]
|
A list of Document objects containing questions, answers, and contexts. |
required |
output_path
|
str
|
The path to save the DPR-formatted output (must be .json or .jsonl). |
required |
save_reranked
|
bool
|
Whether to save re-ranked contexts. Defaults to False. |
False
|
save_text
|
bool
|
Whether to save the full text of the contexts. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
documents = [
Document(
question=Question("What is the capital of France?"),
answers=Answer(["Paris"]),
contexts=[
Context(score=0.9, has_answer=True, id=1, title="Paris", text="The capital of France is Paris."),
Context(score=0.5, has_answer=False, id=2, title="Berlin", text="Berlin is the capital of Germany."),
],
)
]
Dataset.save_documents(documents, "output.json", save_reranked=True, save_text=True)
Source code in rankify/dataset/dataset.py
__len__()
Returns the number of documents in the dataset.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of documents in the dataset. |
__getitem__(idx)
Retrieves the document at the specified index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
The index of the document to retrieve. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Document |
Document
|
The Document instance at the specified index. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset has not been loaded. |
Example
Source code in rankify/dataset/dataset.py
available_dataset(file=None)
staticmethod
Prints information about available datasets.