elcc / util /croissant.py
bboldt's picture
Initial public commit
a5f760c
raw
history blame
2.7 kB
import json
from pathlib import Path
from textwrap import dedent
import datetime
import mlcroissant as mlc # type: ignore
metadata = mlc.Metadata(
name="ELCC",
description="ELCC is a collection of emergent language corpora with accompanying metadata and analyses.",
license="https://creativecommons.org/licenses/by/4.0/",
url="https://huggingface.co/datasets/bboldt/elcc/",
date_published=datetime.datetime.now(datetime.UTC),
cite_as=dedent(
"""\
@misc{boldt2024elcc,
title = "{ELCC}: the Emergent Language Corpus Collection",
author = "Brendon Boldt and David Mortensen",
year = 2024,
url = "",
}"""
),
version="0.1.0",
keywords=["emergent communication", "emergent language", "corpus"],
)
def insert_corpora(metadata: mlc.Metadata) -> None:
paths = list(Path("data").glob("*/data/*/corpus.json"))
dist: list[dict] = [
mlc.FileObject(
id="hf-repo",
name="hf-repo",
content_url="https://huggingface.co/datasets/bboldt/elcc/",
encoding_format="git+https",
sha256="main",
)
]
metadata.distribution = dist
record_sets: list[dict] = []
metadata.record_sets = record_sets
for path in paths:
comps = str(path).split("/")
name = f"{comps[-4]}/{comps[-2]}"
dist.append(
mlc.FileObject(
id=str(path),
name=str(path),
content_url=str(path),
encoding_format="text/plain",
contained_in=["hf-repo"],
)
)
record_sets.append(
mlc.RecordSet(
id=name,
name=name,
fields=[
mlc.Field(
id=f"{name}/content",
name="content",
data_types=mlc.DataType.TEXT,
source=mlc.Source(
file_object=str(path),
extract=mlc.Extract(file_property="content"),
),
),
],
)
)
def main() -> None:
insert_corpora(metadata)
print(metadata.issues.report())
with open("croissant.json", "w") as fo:
d = metadata.to_json()
d["datePublished"] = str(d["datePublished"])
json.dump(d, fo, indent=2)
def test() -> None:
dataset = mlc.Dataset(jsonld="croissant.json")
records = dataset.records(record_set="babyai-sr/GoToObj")
for i, rec in enumerate(records):
print(rec)
if i > 10:
break
if __name__ == "__main__":
main()