|
""" |
|
Copyright 2025-present Rhythm World Team |
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); |
|
you may not use this file except in compliance with the License. |
|
You may obtain a copy of the License at |
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
|
|
|
Unless required by applicable law or agreed to in writing, software |
|
distributed under the License is distributed on an "AS IS" BASIS, |
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
See the License for the specific language governing permissions and |
|
limitations under the License. |
|
""" |
|
|
|
from os import listdir |
|
from pathlib import Path |
|
import datasets |
|
import shutil |
|
import typer |
|
|
|
|
|
DISALLOWED_METADATA = set( |
|
[ |
|
"ChartConverter", |
|
"ChartConvertTool", |
|
"ChartConvertToolVersion", |
|
"smsg", |
|
] |
|
) |
|
|
|
|
|
def parse_chart(chart: str) -> tuple[str, dict[str, str]]: |
|
"""Extract metadata from chart string, ignoring disallowed keys. |
|
|
|
Returns (filtered_chart, metadata_dict).""" |
|
chart_lines = [] |
|
meta: dict[str, str] = {} |
|
prev_blank = False |
|
for line in chart.split("\n"): |
|
if line.startswith("&"): |
|
key, value = line[1:].split("=", 1) |
|
if key in DISALLOWED_METADATA: |
|
continue |
|
else: |
|
meta[key] = value |
|
else: |
|
|
|
if not line.strip(): |
|
if not prev_blank: |
|
chart_lines.append("") |
|
prev_blank = True |
|
else: |
|
chart_lines.append(line) |
|
prev_blank = False |
|
return ("\n".join(chart_lines), meta) |
|
|
|
|
|
def count_subdirs(source_path: Path): |
|
result: dict[str, int] = {} |
|
for entry in listdir(source_path): |
|
first_level = source_path / entry |
|
if not first_level.is_dir(): |
|
continue |
|
count = 0 |
|
for sub in listdir(first_level): |
|
if (first_level / sub).is_dir(): |
|
count += 1 |
|
result[entry] = count |
|
return result |
|
|
|
|
|
def collect_all_rows(source_path: Path): |
|
for entry in listdir(source_path): |
|
first_level = source_path / entry |
|
if not first_level.is_dir(): |
|
continue |
|
for subentry in listdir(first_level): |
|
p = first_level / subentry |
|
with open(p / "maidata.txt", encoding="utf-8") as f: |
|
chart = f.read() |
|
chart, meta = parse_chart(chart) |
|
yield { |
|
"id": int(subentry.split("_")[0]), |
|
"name": meta.get("title", "").replace("[DX]", "").replace("[SD]", ""), |
|
"type": "DX" if subentry.endswith("_DX") else "STD", |
|
"music": str(p / "track.mp3"), |
|
"chart": chart, |
|
} |
|
|
|
|
|
def main( |
|
source_path: Path = typer.Argument( |
|
..., help="Source directory, see README.md for details" |
|
), |
|
output_path: Path = typer.Argument(..., help="Output directory for dataset"), |
|
clear: bool = typer.Option( |
|
False, "--clear", help="Clear output directory if not empty" |
|
), |
|
): |
|
if output_path.exists() and any(output_path.iterdir()): |
|
if not clear: |
|
typer.echo("Error: Output directory is not empty.") |
|
raise typer.Exit(code=1) |
|
else: |
|
for item in output_path.iterdir(): |
|
if item.is_dir(): |
|
shutil.rmtree(item) |
|
else: |
|
item.unlink() |
|
output_path.mkdir(parents=True, exist_ok=True) |
|
|
|
features = datasets.Features( |
|
{ |
|
"id": datasets.Value("int32"), |
|
"name": datasets.Value("string"), |
|
"type": datasets.ClassLabel(names=["STD", "DX"]), |
|
"music": datasets.Audio(), |
|
"chart": datasets.Value("string"), |
|
} |
|
) |
|
|
|
rows = [i for i in collect_all_rows(source_path)] |
|
dataset = datasets.Dataset.from_list( |
|
rows, |
|
features=features, |
|
info=datasets.DatasetInfo( |
|
description="Dataset of Maimai charts & music, in 3simai format.", |
|
homepage="https://huggingface.co/datasets/rhythm-world/maimai-charts-dataset", |
|
version="1.0.0", |
|
license="odc-by", |
|
), |
|
) |
|
dataset.save_to_disk(output_path) |
|
|
|
|
|
app = typer.Typer() |
|
app.command()(main) |
|
|
|
if __name__ == "__main__": |
|
app() |
|
|