MergeDatasets
Merges multiple anime or manga datasets into a single dataset.
This script provides functionality to merge and preprocess multiple anime or manga datasets from various sources. It handles data cleaning, deduplication, and consolidation of information across datasets.
Key features
- Loads datasets from CSV files and Hugging Face datasets library
- Cleans and preprocesses text fields like titles and synopses
- Merges datasets based on common identifiers while handling duplicates
- Consolidates information from multiple sources while preserving data quality
- Removes inappropriate content based on genres/demographics
- Saves the final merged dataset with progress tracking
The script can be run from the command line with a required --type argument specifying either 'anime' or 'manga'.
Example:
The merged dataset will be saved to model/merged_[type]_dataset.csv
file_formatter
module-attribute
¶
file_handler
module-attribute
¶
file_handler = RotatingFileHandler('./logs/merge_datasets.log', maxBytes=10 * 1024 * 1024, backupCount=10, encoding='utf-8')
stream_formatter
module-attribute
¶
add_additional_info
¶
add_additional_info(merged: DataFrame, additional_df: DataFrame, description_col: str, name_columns: list[str], new_synopsis_col: str) -> DataFrame
Add additional synopsis information from supplementary dataset.
PARAMETER | DESCRIPTION |
---|---|
merged
|
Main DataFrame to update with additional info
TYPE:
|
additional_df
|
DataFrame containing additional descriptions
TYPE:
|
description_col
|
Name of column containing descriptions
TYPE:
|
name_columns
|
List of columns to use for matching titles
TYPE:
|
new_synopsis_col
|
Name for new column to store additional synopses
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Updated DataFrame with additional synopsis information |
Matches entries between datasets and adds non-duplicate synopsis information. Uses tqdm for progress tracking during updates.
Source code in src/merge_datasets.py
clean_synopsis
¶
Clean synopsis text by removing entries containing unwanted phrases.
PARAMETER | DESCRIPTION |
---|---|
df
|
DataFrame containing the synopsis column
TYPE:
|
synopsis_col
|
Name of the column containing synopsis text
TYPE:
|
unwanted_phrases
|
List of phrases that indicate invalid synopsis content
TYPE:
|
The function modifies the DataFrame in-place, setting matching synopses to empty strings.
Source code in src/merge_datasets.py
consolidate_titles
¶
Consolidate multiple title columns into a single title column.
PARAMETER | DESCRIPTION |
---|---|
df
|
DataFrame containing multiple title columns
TYPE:
|
title_columns
|
List of column names containing titles to consolidate
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Series
|
pd.Series: Consolidated titles, using first non-null value found across columns |
The function prioritizes existing 'title' column if present, then fills missing values from other title columns in order. Empty strings and 'unknown title' are treated as null.
Source code in src/merge_datasets.py
find_additional_info
¶
find_additional_info(row: Series, additional_df: DataFrame, description_col: str, name_columns: list) -> Optional[str]
Find matching description information from additional dataset.
PARAMETER | DESCRIPTION |
---|---|
row
|
Series containing title information to match
TYPE:
|
additional_df
|
DataFrame containing additional descriptions
TYPE:
|
description_col
|
Name of column containing descriptions
TYPE:
|
name_columns
|
List of column names to use for matching titles
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[str]
|
str | None: Matching description if found, None otherwise |
Attempts to match titles across multiple name columns and returns first matching description.
Source code in src/merge_datasets.py
main
¶
Main function to parse command-line arguments and merge datasets.
This function serves as the entry point for the dataset merging process. It parses command-line arguments to determine whether to merge anime or manga datasets, then calls the appropriate merging function.
The function expects a single command-line argument --type which must be either 'anime' or 'manga'. It will: 1. Parse the command-line arguments 2. Log the specified dataset type 3. Call merge_anime_datasets() or merge_manga_datasets() based on the type 4. Log an error if an invalid type is specified
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in src/merge_datasets.py
merge_anime_datasets
¶
Merge multiple anime datasets into a single comprehensive dataset.
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Merged and cleaned anime dataset |
Performs the following operations
- Loads multiple anime datasets from files and Hugging Face
- Cleans and standardizes text fields
- Removes adult content and kids' content
- Merges datasets based on IDs and titles
- Consolidates synopsis information
- Removes duplicates
- Saves final dataset to CSV with progress tracking
RAISES | DESCRIPTION |
---|---|
Exception
|
If any error occurs during merging process |
Source code in src/merge_datasets.py
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 |
|
merge_manga_datasets
¶
Merge multiple manga datasets into a single comprehensive dataset.
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Merged and cleaned manga dataset |
Performs the following operations
- Loads manga datasets from multiple CSV files
- Removes adult content
- Cleans and standardizes text fields
- Merges datasets based on IDs and titles
- Consolidates synopsis information
- Removes duplicates
- Saves final dataset to CSV with progress tracking
RAISES | DESCRIPTION |
---|---|
Exception
|
If any error occurs during merging process |
Source code in src/merge_datasets.py
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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 |
|
parse_args
¶
Parse command line arguments for dataset type selection.
RETURNS | DESCRIPTION |
---|---|
Namespace
|
argparse.Namespace: Parsed arguments containing: type (str): Either 'anime' or 'manga' to specify dataset type to merge |
Source code in src/merge_datasets.py
preprocess_name
¶
Preprocess a name string for consistent matching.
PARAMETER | DESCRIPTION |
---|---|
name
|
Input name value of any type
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
Preprocessed name in lowercase with whitespace stripped Returns empty string if input is null/NaN
TYPE:
|
Used to standardize names across datasets before matching/merging.
Source code in src/merge_datasets.py
preprocess_synopsis_columns
¶
Preprocess text in synopsis columns for consistency.
PARAMETER | DESCRIPTION |
---|---|
df
|
DataFrame containing synopsis columns
TYPE:
|
synopsis_cols
|
List of column names containing synopsis text
TYPE:
|
Applies common text preprocessing to each synopsis column in-place. Uses common.preprocess_text() for standardization. Logs warning if specified column not found.
Source code in src/merge_datasets.py
remove_duplicate_infos
¶
Remove duplicate synopsis/description entries across columns.
PARAMETER | DESCRIPTION |
---|---|
df
|
DataFrame containing synopsis columns
TYPE:
|
info_cols
|
List of column names containing synopsis information
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: DataFrame with duplicate synopses removed |
Keeps first occurrence of each unique synopsis and sets duplicates to NA. Processes row-by-row to maintain data integrity.
Source code in src/merge_datasets.py
remove_numbered_list_synopsis
¶
Remove synopsis entries that are formatted as numbered lists.
PARAMETER | DESCRIPTION |
---|---|
df
|
DataFrame containing the synopsis columns
TYPE:
|
synopsis_cols
|
List of column names containing synopsis text
TYPE:
|
The function modifies the DataFrame in-place, setting numbered list synopses to empty strings. Uses regex pattern matching to identify numbered list formats.