|
"""TicTacToe""" |
|
|
|
from typing import List |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
_BASE_FEATURE_NAMES = [ |
|
"top_left_square", |
|
"top_middle_square", |
|
"top_right_square", |
|
"middle_left_square", |
|
"middle_middle_square", |
|
"middle_right_square", |
|
"bottom_left_square", |
|
"bottom_middle_square", |
|
"bottom_right_square", |
|
"x_wins" |
|
] |
|
|
|
DESCRIPTION = "TicTacToe dataset from the UCI ML repository." |
|
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/TicTacToe" |
|
_URLS = ("https://archive.ics.uci.edu/ml/datasets/TicTacToe") |
|
_CITATION = """ |
|
@misc{misc_tic-tac-toe_endgame_101, |
|
author = {Aha,David}, |
|
title = {{Tic-Tac-Toe Endgame}}, |
|
year = {1991}, |
|
howpublished = {UCI Machine Learning Repository}, |
|
note = {{DOI}: \\url{10.24432/C5688J}} |
|
}""" |
|
|
|
|
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/tic_tac_toe/raw/main/tic-tac-toe.data" |
|
} |
|
features_types_per_config = { |
|
"tic_tac_toe": { |
|
"top_left_square": datasets.Value("string"), |
|
"top_middle_square": datasets.Value("string"), |
|
"top_right_square": datasets.Value("string"), |
|
"middle_left_square": datasets.Value("string"), |
|
"middle_middle_square": datasets.Value("string"), |
|
"middle_right_square": datasets.Value("string"), |
|
"bottom_left_square": datasets.Value("string"), |
|
"bottom_middle_square": datasets.Value("string"), |
|
"bottom_right_square": datasets.Value("string"), |
|
"x_wins": datasets.ClassLabel(num_classes=2, names=("no", "yes")) |
|
} |
|
|
|
} |
|
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} |
|
|
|
|
|
class TicTacToeConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(TicTacToeConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class TicTacToe(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "tic_tac_toe" |
|
BUILDER_CONFIGS = [ |
|
TicTacToeConfig(name="tic_tac_toe", |
|
description="TicTacToe for binary classification.") |
|
] |
|
|
|
|
|
def _info(self): |
|
info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE, |
|
features=features_per_config[self.config.name]) |
|
|
|
return info |
|
|
|
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: |
|
downloads = dl_manager.download_and_extract(urls_per_split) |
|
|
|
return [ |
|
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}) |
|
] |
|
|
|
def _generate_examples(self, filepath: str): |
|
data = pandas.read_csv(filepath, header=None) |
|
data.columns = _BASE_FEATURE_NAMES |
|
data.loc[:, "x_wins"] = data.x_wins.apply(lambda x: 1 if x == "positive" else 0) |
|
|
|
for row_id, row in data.iterrows(): |
|
data_row = dict(row) |
|
|
|
yield row_id, data_row |
|
|