Datasets:

Modalities:
Image
Text
Formats:
parquet
Languages:
Danish
ArXiv:
DOI:
Libraries:
Datasets
Dask
License:
File size: 1,298 Bytes
566156e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import Self

from datasets import Dataset

from dynaword.git_utilities import (
    get_current_revision,
)

logger = logging.getLogger(__name__)


def calculate_average_document_length(
    dataset: Dataset, text_column: str = "text"
) -> float:
    texts = sum(len(t) for t in dataset[text_column])
    return texts / len(dataset)


@dataclass()
class DescriptiveStatsOverview:
    number_of_samples: int
    average_document_length: float
    number_of_tokens: int

    @classmethod
    def from_disk(cls, path: Path):
        with path.open("r") as f:
            data = json.load(f)
        if "revision" in data:
            data.pop("revision")
        obj = cls(**data)
        return obj

    def to_disk(self, path: Path):
        data = self.__dict__
        data["revision"] = get_current_revision()
        with path.with_suffix(".json").open("w") as f:
            json.dump(self.__dict__, f, indent=2)

    @classmethod
    def from_dataset(cls, dataset: Dataset) -> Self:
        return cls(
            number_of_samples=len(dataset),
            average_document_length=calculate_average_document_length(dataset),
            number_of_tokens=sum(dataset["token_count"]),
        )