File size: 2,699 Bytes
a5f760c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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()