| """Loading script""" | |
| import datasets | |
| import json | |
| _DESCRIPTION = "Magic the Gathering card text dataset" | |
| """We have two .json files with the train and validation cards. The | |
| json is basically a list of dictionaries with the following keys: | |
| "card_name": string | |
| "type_line": string | |
| "oracle_text": string | |
| """ | |
| _FILES = { | |
| "train": "mtg_text/train_cards.json", | |
| "validation": "mtg_text/val_cards.json", | |
| } | |
| class MTGCardText(datasets.GeneratorBasedBuilder): | |
| """BuilderConfig for MTGCardText""" | |
| def __init__(self, **kwargs): | |
| """BuilderConfig for MTGCardText. | |
| Args: | |
| **kwargs: keyword arguments forwarded to super. | |
| """ | |
| super(MTGCardText, self).__init__(**kwargs) | |
| def _info(self): | |
| return datasets.DatasetInfo( | |
| description=_DESCRIPTION, | |
| features=datasets.Features({ | |
| "card_name": datasets.Value("string"), | |
| "type_line": datasets.Value("string"), | |
| "oracle_text": datasets.Value("string") | |
| }), | |
| supervised_keys=None, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| """Returns SplitGenerators. | |
| Args: | |
| dl_manager: DownloadManager object used to download and extract the | |
| dataset files. | |
| Returns: | |
| `dict<str, SplitGenerator>`. | |
| """ | |
| downloaded_files = dl_manager.download_and_extract(_FILES) | |
| return [ | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TRAIN, | |
| gen_kwargs={ | |
| "filepath": downloaded_files["train"], | |
| }, | |
| ), | |
| datasets.SplitGenerator( | |
| name=datasets.Split.VALIDATION, | |
| gen_kwargs={ | |
| "filepath": downloaded_files["validation"], | |
| }, | |
| ), | |
| ] | |
| def _generate_examples(self, filepath): | |
| """Yields examples. | |
| Args: | |
| filepath: a string | |
| Yields: | |
| dictionaries containing "card_name", "type_line" and "oracle_text" | |
| """ | |
| with open(filepath, encoding="utf-8") as f: | |
| data = json.load(f) | |
| for id_, row in enumerate(data): | |
| yield id_, { | |
| "card_name": row["card_name"], | |
| "type_line": row["type_line"], | |
| "oracle_text": row["oracle_text"] | |
| } | |