File size: 6,407 Bytes
dc98da6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
149
150
151
152
153
154
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# 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.
"""Identifying character archetypes from movie scripts"""

from __future__ import absolute_import, division, print_function

import csv
import os

import datasets


_CITATION = """\
"""

_DESCRIPTION = """\
The character types identification dataset consists of movie
scripts annotated with character archetypes (Hero, Villain, Mentor, etc.).
"""

_URLS = {
    "full_text": "https://drive.google.com/uc?export=download&id=1pivLkYl6l6_jJlQkHGsvziEn82GBapWc",
    # "repo": "https://github.com/ghomasHudson/character-type-identification/archive/master.zip",
    "repo": "https://github.com/ghomasHudson/character-type-identification/archive/refs/heads/master.zip"
}


class CharacterTypeID(datasets.GeneratorBasedBuilder):
    """Character Type Identification"""

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            citation=_CITATION,
            features=datasets.Features(
                {
                    "document": {
                        "id": datasets.Value("string"),
                        "url": datasets.Value("string"),
                        "file_size": datasets.Value("int32"),
                        "word_count": datasets.Value("int32"),
                        "start": datasets.Value("string"),
                        "end": datasets.Value("string"),
                        "summary": {
                            "text": datasets.Value("string"),
                            "url": datasets.Value("string"),
                            "title": datasets.Value("string"),
                        },
                        "text": datasets.Value("string"),
                    },
                    "character_name": datasets.Value("string"),
                    "unit_quality_score": datasets.Value("float32"),
                    "character_type": datasets.ClassLabel(names=[
                        "Hero",
                        "Villain/Antagonist",
                        "Spouse/Partner/Lover of Hero",
                        "Spouse/Partner/Lover of Villain",
                        "Sidekick of Hero",
                        "Sidekick of Villain",
                        "Supporting role character of Hero",
                        "Supporting role character of Villain",
                        "Mentor",
                        "No Applicable Type"
                    ])
                }
            ),
            homepage="https://github.com/ghomasHudson/character-type-identification",
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""

        dl_dir = dl_manager.download_and_extract(_URLS)
        dl_dir["repo"] = os.path.join(dl_dir["repo"], "character-type-identification-master")

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"repo_dir": dl_dir["repo"], "full_text_dir": dl_dir["full_text"], "split": "train"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"repo_dir": dl_dir["repo"], "full_text_dir": dl_dir["full_text"], "split": "test"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"repo_dir": dl_dir["repo"], "full_text_dir": dl_dir["full_text"], "split": "valid"},
            ),
        ]

    def _generate_examples(self, repo_dir, full_text_dir, split):
        """Yields examples."""
        documents = {}
        with open(os.path.join(repo_dir, "documents.csv"), encoding="utf-8") as f:
            reader = csv.DictReader(f)
            for row in reader:
                if row["set"] != split:
                    continue
                documents[row["document_id"]] = row

        summaries = {}
        with open(os.path.join(repo_dir, "summaries.csv"), encoding="utf-8") as f:
            reader = csv.DictReader(f)
            for row in reader:
                if row["set"] != split:
                    continue
                summaries[row["document_id"]] = row

        char_fn = "character_labels.csv"
        if split == "test":
            char_fn = "character_labels_gold.csv"
        with open(os.path.join(repo_dir, char_fn), encoding="utf-8") as f:
            reader = csv.DictReader(f)
            for id_, row in enumerate(reader):
                if row["set"] != split:
                    continue
                document_id = row["document_id"]
                if document_id not in documents.keys():
                    print("NO KEY")
                document = documents[document_id]
                summary = summaries[document_id]
                full_text = open(os.path.join(full_text_dir, document_id + ".txt"), encoding="latin-1").read()
                res = {
                    "document": {
                        "id": document["document_id"],
                        "url": document["script_url"],
                        "file_size": document["script_file_size"],
                        "word_count": document["script_word_count"],
                        "start": document["script_start"],
                        "end": document["script_end"],
                        "summary": {
                            "text": summary["summary"],
                            "url": document["wiki_url"],
                            "title": document["wiki_title"],
                        },
                        "text": full_text,
                    },
                    "character_name": row["character_name"],
                    "unit_quality_score": row["unit_quality_score"],
                    "character_type": row["character_type"]
                }
                yield id_, res