File size: 5,557 Bytes
da9f614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa19090
da9f614
fa19090
da9f614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb94867
da9f614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import ast

import datasets as ds
import pandas as pd

_DESCRIPTION = """\
CAMERA (CyberAgent Multimodal Evaluation for Ad Text GeneRAtion) is the Japanese ad text generation dataset.
"""

_CITATION = """\
@misc{mita2024striking,
    title={Striking Gold in Advertising: Standardization and Exploration of Ad Text Generation},
    author={Masato Mita and Soichiro Murakami and Akihiko Kato and Peinan Zhang},
    year={2024},
    eprint={2309.12030},
    archivePrefix={arXiv},
    primaryClass={id='cs.CL' full_name='Computation and Language' is_active=True alt_name='cmp-lg' in_archive='cs' is_general=False description='Covers natural language processing. Roughly includes material in ACM Subject Class I.2.7. Note that work on artificial languages (programming languages, logics, formal systems) that does not explicitly address natural-language issues broadly construed (natural-language processing, computational linguistics, speech, text retrieval, etc.) is not appropriate for this area.'}
}
"""

_HOMEPAGE = "https://github.com/CyberAgentAILab/camera"

_LICENSE = """\
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
"""

_URLS = {
    "without-lp-images": "https://storage.googleapis.com/camera-public/camera-v2.2-minimal.tar.gz",
    "with-lp-images": "https://storage.googleapis.com/camera-public/camera-v2.2.tar.gz",
}

_DESCRIPTION = {
    "without-lp-images": "The CAMERA dataset w/o LP images (ver.2.2.0)",
    "with-lp-images": "The CAMERA dataset w/ LP images (ver.2.2.0)",
}

_VERSION = ds.Version("2.2.0", "")


class CameraConfig(ds.BuilderConfig):
    def __init__(self, name: str, version: ds.Version = _VERSION, **kwargs):
        super().__init__(
            name=name,
            description=_DESCRIPTION[name],
            version=version,
            **kwargs,
        )


class CameraDataset(ds.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [CameraConfig(name="with-lp-images")]

    DEFAULT_CONFIG_NAME = "with-lp-images"

    def _info(self) -> ds.DatasetInfo:
        features = ds.Features(
            {
                "asset_id": ds.Value("int64"),
                "kw": ds.Value("string"),
                "lp_meta_description": ds.Value("string"),
                "title_org": ds.Value("string"),
                "title_ne1": ds.Value("string"),
                "title_ne2": ds.Value("string"),
                "title_ne3": ds.Value("string"),
                "domain": ds.Value("string"),
                "parsed_full_text_annotation": ds.Sequence(
                    {
                        "text": ds.Value("string"),
                        "xmax": ds.Value("int64"),
                        "xmin": ds.Value("int64"),
                        "ymax": ds.Value("int64"),
                        "ymin": ds.Value("int64"),
                    }
                ),
            }
        )

        if self.config.name == "with-lp-images":
            features["lp_image"] = ds.Image()

        return ds.DatasetInfo(
            description=_DESCRIPTION,
            citation=_CITATION,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            features=features,
        )

    def _split_generators(self, dl_manager: ds.DownloadManager):
        base_dir = dl_manager.download_and_extract(_URLS[self.config.name])
        lp_image_dir: str | None = None

        if self.config.name == "without-lp-images":
            data_dir = f"{base_dir}/camera-v2.2-minimal"
        elif self.config.name == "with-lp-images":
            data_dir = f"{base_dir}/camera-v2.2"
            lp_image_dir = f"{data_dir}/lp-screenshot"
        else:
            raise ValueError(f"Invalid config name: {self.config.name}")

        return [
            ds.SplitGenerator(
                name=ds.Split.TRAIN,
                gen_kwargs={
                    "file": f"{data_dir}/train.csv",
                    "lp_image_dir": lp_image_dir,
                },
            ),
            ds.SplitGenerator(
                name=ds.Split.VALIDATION,
                gen_kwargs={
                    "file": f"{data_dir}/dev.csv",
                    "lp_image_dir": lp_image_dir,
                },
            ),
            ds.SplitGenerator(
                name=ds.Split.TEST,
                gen_kwargs={
                    "file": f"{data_dir}/test.csv",
                    "lp_image_dir": lp_image_dir,
                },
            ),
        ]

    def _generate_examples(self, file: str, lp_image_dir: str | None = None):
        df = pd.read_csv(file)
        for i, data_dict in enumerate(df.to_dict("records")):
            asset_id = data_dict["asset_id"]
            example_dict = {
                "asset_id": asset_id,
                "kw": data_dict["kw"],
                "lp_meta_description": data_dict["lp_meta_description"],
                "title_org": data_dict["title_org"],
                "title_ne1": data_dict.get("title_ne1", ""),
                "title_ne2": data_dict.get("title_ne2", ""),
                "title_ne3": data_dict.get("title_ne3", ""),
                "domain": data_dict.get("domain", ""),
                "parsed_full_text_annotation": ast.literal_eval(
                    data_dict["parsed_full_text_annotation"]
                ),
            }

            if self.config.name == "with-lp-images" and lp_image_dir is not None:
                file_name = f"screen-1200-{asset_id}.png"
                example_dict["lp_image"] = f"{lp_image_dir}/{file_name}"

            yield i, example_dict