Datasets:
Delete loading script
Browse files- guardian_authorship.py +0 -325
guardian_authorship.py
DELETED
|
@@ -1,325 +0,0 @@
|
|
| 1 |
-
# coding=utf-8
|
| 2 |
-
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 3 |
-
#
|
| 4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
-
# you may not use this file except in compliance with the License.
|
| 6 |
-
# You may obtain a copy of the License at
|
| 7 |
-
#
|
| 8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
-
#
|
| 10 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
-
# See the License for the specific language governing permissions and
|
| 14 |
-
# limitations under the License.
|
| 15 |
-
"""This is an authorship attribution dataset based on the work of Stamatatos 2013. """
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
import os
|
| 19 |
-
|
| 20 |
-
import datasets
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
_CITATION = """\
|
| 24 |
-
@article{article,
|
| 25 |
-
author = {Stamatatos, Efstathios},
|
| 26 |
-
year = {2013},
|
| 27 |
-
month = {01},
|
| 28 |
-
pages = {421-439},
|
| 29 |
-
title = {On the robustness of authorship attribution based on character n-gram features},
|
| 30 |
-
volume = {21},
|
| 31 |
-
journal = {Journal of Law and Policy}
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
@inproceedings{stamatatos2017authorship,
|
| 35 |
-
title={Authorship attribution using text distortion},
|
| 36 |
-
author={Stamatatos, Efstathios},
|
| 37 |
-
booktitle={Proc. of the 15th Conf. of the European Chapter of the Association for Computational Linguistics},
|
| 38 |
-
volume={1}
|
| 39 |
-
pages={1138--1149},
|
| 40 |
-
year={2017}
|
| 41 |
-
}
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
_DESCRIPTION = """\
|
| 45 |
-
A dataset cross-topic authorship attribution. The dataset is provided by Stamatatos 2013.
|
| 46 |
-
1- The cross-topic scenarios are based on Table-4 in Stamatatos 2017 (Ex. cross_topic_1 => row 1:P S U&W ).
|
| 47 |
-
2- The cross-genre scenarios are based on Table-5 in the same paper. (Ex. cross_genre_1 => row 1:B P S&U&W).
|
| 48 |
-
|
| 49 |
-
3- The same-topic/genre scenario is created by grouping all the datasts as follows.
|
| 50 |
-
For ex., to use same_topic and split the data 60-40 use:
|
| 51 |
-
train_ds = load_dataset('guardian_authorship', name="cross_topic_<<#>>",
|
| 52 |
-
split='train[:60%]+validation[:60%]+test[:60%]')
|
| 53 |
-
tests_ds = load_dataset('guardian_authorship', name="cross_topic_<<#>>",
|
| 54 |
-
split='train[-40%:]+validation[-40%:]+test[-40%:]')
|
| 55 |
-
|
| 56 |
-
IMPORTANT: train+validation+test[:60%] will generate the wrong splits because the data is imbalanced
|
| 57 |
-
|
| 58 |
-
* See https://huggingface.co/docs/datasets/splits.html for detailed/more examples
|
| 59 |
-
"""
|
| 60 |
-
|
| 61 |
-
_URL = "https://www.dropbox.com/s/lc5mje0owl9shms/Guardian.zip?dl=1"
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Using a specific configuration class is optional, you can also use the base class if you don't need
|
| 65 |
-
# to add specific attributes.
|
| 66 |
-
# here we give an example for three sub-set of the dataset with difference sizes.
|
| 67 |
-
class GuardianAuthorshipConfig(datasets.BuilderConfig):
|
| 68 |
-
"""BuilderConfig for NewDataset"""
|
| 69 |
-
|
| 70 |
-
def __init__(self, train_folder, valid_folder, test_folder, **kwargs):
|
| 71 |
-
"""
|
| 72 |
-
Args:
|
| 73 |
-
Train_folder: Topic/genre used for training
|
| 74 |
-
valid_folder: ~ ~ for validation
|
| 75 |
-
test_folder: ~ ~ for testing
|
| 76 |
-
|
| 77 |
-
**kwargs: keyword arguments forwarded to super.
|
| 78 |
-
"""
|
| 79 |
-
super(GuardianAuthorshipConfig, self).__init__(**kwargs)
|
| 80 |
-
self.train_folder = train_folder
|
| 81 |
-
self.valid_folder = valid_folder
|
| 82 |
-
self.test_folder = test_folder
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
class GuardianAuthorship(datasets.GeneratorBasedBuilder):
|
| 86 |
-
"""dataset for same- and cross-topic authorship attribution"""
|
| 87 |
-
|
| 88 |
-
config_counter = 0
|
| 89 |
-
BUILDER_CONFIG_CLASS = GuardianAuthorshipConfig
|
| 90 |
-
BUILDER_CONFIGS = [
|
| 91 |
-
# cross-topic
|
| 92 |
-
GuardianAuthorshipConfig(
|
| 93 |
-
name=f"cross_topic_{1}",
|
| 94 |
-
version=datasets.Version(f"{1}.0.0", description=f"The Original DS with the cross-topic scenario no.{1}"),
|
| 95 |
-
train_folder="Politics",
|
| 96 |
-
valid_folder="Society",
|
| 97 |
-
test_folder="UK,World",
|
| 98 |
-
),
|
| 99 |
-
GuardianAuthorshipConfig(
|
| 100 |
-
name=f"cross_topic_{2}",
|
| 101 |
-
version=datasets.Version(f"{2}.0.0", description=f"The Original DS with the cross-topic scenario no.{2}"),
|
| 102 |
-
train_folder="Politics",
|
| 103 |
-
valid_folder="UK",
|
| 104 |
-
test_folder="Society,World",
|
| 105 |
-
),
|
| 106 |
-
GuardianAuthorshipConfig(
|
| 107 |
-
name=f"cross_topic_{3}",
|
| 108 |
-
version=datasets.Version(f"{3}.0.0", description=f"The Original DS with the cross-topic scenario no.{3}"),
|
| 109 |
-
train_folder="Politics",
|
| 110 |
-
valid_folder="World",
|
| 111 |
-
test_folder="Society,UK",
|
| 112 |
-
),
|
| 113 |
-
GuardianAuthorshipConfig(
|
| 114 |
-
name=f"cross_topic_{4}",
|
| 115 |
-
version=datasets.Version(f"{4}.0.0", description=f"The Original DS with the cross-topic scenario no.{4}"),
|
| 116 |
-
train_folder="Society",
|
| 117 |
-
valid_folder="Politics",
|
| 118 |
-
test_folder="UK,World",
|
| 119 |
-
),
|
| 120 |
-
GuardianAuthorshipConfig(
|
| 121 |
-
name=f"cross_topic_{5}",
|
| 122 |
-
version=datasets.Version(f"{5}.0.0", description=f"The Original DS with the cross-topic scenario no.{5}"),
|
| 123 |
-
train_folder="Society",
|
| 124 |
-
valid_folder="UK",
|
| 125 |
-
test_folder="Politics,World",
|
| 126 |
-
),
|
| 127 |
-
GuardianAuthorshipConfig(
|
| 128 |
-
name=f"cross_topic_{6}",
|
| 129 |
-
version=datasets.Version(f"{6}.0.0", description=f"The Original DS with the cross-topic scenario no.{6}"),
|
| 130 |
-
train_folder="Society",
|
| 131 |
-
valid_folder="World",
|
| 132 |
-
test_folder="Politics,UK",
|
| 133 |
-
),
|
| 134 |
-
GuardianAuthorshipConfig(
|
| 135 |
-
name=f"cross_topic_{7}",
|
| 136 |
-
version=datasets.Version(f"{7}.0.0", description=f"The Original DS with the cross-topic scenario no.{7}"),
|
| 137 |
-
train_folder="UK",
|
| 138 |
-
valid_folder="Politics",
|
| 139 |
-
test_folder="Society,World",
|
| 140 |
-
),
|
| 141 |
-
GuardianAuthorshipConfig(
|
| 142 |
-
name=f"cross_topic_{8}",
|
| 143 |
-
version=datasets.Version(f"{8}.0.0", description=f"The Original DS with the cross-topic scenario no.{8}"),
|
| 144 |
-
train_folder="UK",
|
| 145 |
-
valid_folder="Society",
|
| 146 |
-
test_folder="Politics,World",
|
| 147 |
-
),
|
| 148 |
-
GuardianAuthorshipConfig(
|
| 149 |
-
name=f"cross_topic_{9}",
|
| 150 |
-
version=datasets.Version(f"{9}.0.0", description=f"The Original DS with the cross-topic scenario no.{9}"),
|
| 151 |
-
train_folder="UK",
|
| 152 |
-
valid_folder="World",
|
| 153 |
-
test_folder="Politics,Society",
|
| 154 |
-
),
|
| 155 |
-
GuardianAuthorshipConfig(
|
| 156 |
-
name=f"cross_topic_{10}",
|
| 157 |
-
version=datasets.Version(
|
| 158 |
-
f"{10}.0.0", description=f"The Original DS with the cross-topic scenario no.{10}"
|
| 159 |
-
),
|
| 160 |
-
train_folder="World",
|
| 161 |
-
valid_folder="Politics",
|
| 162 |
-
test_folder="Society,UK",
|
| 163 |
-
),
|
| 164 |
-
GuardianAuthorshipConfig(
|
| 165 |
-
name=f"cross_topic_{11}",
|
| 166 |
-
version=datasets.Version(
|
| 167 |
-
f"{11}.0.0", description=f"The Original DS with the cross-topic scenario no.{11}"
|
| 168 |
-
),
|
| 169 |
-
train_folder="World",
|
| 170 |
-
valid_folder="Society",
|
| 171 |
-
test_folder="Politics,UK",
|
| 172 |
-
),
|
| 173 |
-
GuardianAuthorshipConfig(
|
| 174 |
-
name=f"cross_topic_{12}",
|
| 175 |
-
version=datasets.Version(
|
| 176 |
-
f"{12}.0.0", description=f"The Original DS with the cross-topic scenario no.{12}"
|
| 177 |
-
),
|
| 178 |
-
train_folder="World",
|
| 179 |
-
valid_folder="UK",
|
| 180 |
-
test_folder="Politics,Society",
|
| 181 |
-
),
|
| 182 |
-
# # cross-genre
|
| 183 |
-
GuardianAuthorshipConfig(
|
| 184 |
-
name=f"cross_genre_{1}",
|
| 185 |
-
version=datasets.Version(f"{1}.0.0", description=f"The Original DS with the cross-genre scenario no.{1}"),
|
| 186 |
-
train_folder="Books",
|
| 187 |
-
valid_folder="Politics",
|
| 188 |
-
test_folder="Society,UK,World",
|
| 189 |
-
),
|
| 190 |
-
GuardianAuthorshipConfig(
|
| 191 |
-
name=f"cross_genre_{2}",
|
| 192 |
-
version=datasets.Version(f"{2}.0.0", description=f"The Original DS with the cross-genre scenario no.{2}"),
|
| 193 |
-
train_folder="Books",
|
| 194 |
-
valid_folder="Society",
|
| 195 |
-
test_folder="Politics,UK,World",
|
| 196 |
-
),
|
| 197 |
-
GuardianAuthorshipConfig(
|
| 198 |
-
name=f"cross_genre_{3}",
|
| 199 |
-
version=datasets.Version(f"{3}.0.0", description=f"The Original DS with the cross-genre scenario no.{3}"),
|
| 200 |
-
train_folder="Books",
|
| 201 |
-
valid_folder="UK",
|
| 202 |
-
test_folder="Politics,Society,World",
|
| 203 |
-
),
|
| 204 |
-
GuardianAuthorshipConfig(
|
| 205 |
-
name=f"cross_genre_{4}",
|
| 206 |
-
version=datasets.Version(f"{4}.0.0", description=f"The Original DS with the cross-genre scenario no.{4}"),
|
| 207 |
-
train_folder="Books",
|
| 208 |
-
valid_folder="World",
|
| 209 |
-
test_folder="Politics,Society,UK",
|
| 210 |
-
),
|
| 211 |
-
]
|
| 212 |
-
|
| 213 |
-
def _info(self):
|
| 214 |
-
# Specifies the datasets.DatasetInfo object
|
| 215 |
-
return datasets.DatasetInfo(
|
| 216 |
-
# This is the description that will appear on the datasets page.
|
| 217 |
-
description=_DESCRIPTION,
|
| 218 |
-
features=datasets.Features(
|
| 219 |
-
{
|
| 220 |
-
# These are the features of your dataset like images, labels ...
|
| 221 |
-
# There are 13 authors in this dataset
|
| 222 |
-
"author": datasets.features.ClassLabel(
|
| 223 |
-
names=[
|
| 224 |
-
"catherinebennett",
|
| 225 |
-
"georgemonbiot",
|
| 226 |
-
"hugoyoung",
|
| 227 |
-
"jonathanfreedland",
|
| 228 |
-
"martinkettle",
|
| 229 |
-
"maryriddell",
|
| 230 |
-
"nickcohen",
|
| 231 |
-
"peterpreston",
|
| 232 |
-
"pollytoynbee",
|
| 233 |
-
"royhattersley",
|
| 234 |
-
"simonhoggart",
|
| 235 |
-
"willhutton",
|
| 236 |
-
"zoewilliams",
|
| 237 |
-
]
|
| 238 |
-
),
|
| 239 |
-
# There are book reviews, and articles on the following four topics
|
| 240 |
-
"topic": datasets.features.ClassLabel(names=["Politics", "Society", "UK", "World", "Books"]),
|
| 241 |
-
"article": datasets.Value("string"),
|
| 242 |
-
}
|
| 243 |
-
),
|
| 244 |
-
# If there's a common (input, target) tuple from the features,
|
| 245 |
-
# specify them here. They'll be used if as_supervised=True in
|
| 246 |
-
# builder.as_dataset.
|
| 247 |
-
supervised_keys=[("article", "author")],
|
| 248 |
-
# Homepage of the dataset for documentation
|
| 249 |
-
homepage="http://www.icsd.aegean.gr/lecturers/stamatatos/papers/JLP2013.pdf",
|
| 250 |
-
citation=_CITATION,
|
| 251 |
-
)
|
| 252 |
-
|
| 253 |
-
def _split_generators(self, dl_manager):
|
| 254 |
-
"""Returns SplitGenerators."""
|
| 255 |
-
# dl_manager is a datasets.download.DownloadManager that can be used to
|
| 256 |
-
# download and extract URLs
|
| 257 |
-
dl_dir = dl_manager.download_and_extract(_URL)
|
| 258 |
-
|
| 259 |
-
# This folder contains the orginal/2013 dataset
|
| 260 |
-
data_dir = os.path.join(dl_dir, "Guardian", "Guardian_original")
|
| 261 |
-
|
| 262 |
-
return [
|
| 263 |
-
datasets.SplitGenerator(
|
| 264 |
-
name=datasets.Split.TRAIN,
|
| 265 |
-
# These kwargs will be passed to _generate_examples
|
| 266 |
-
gen_kwargs={"data_dir": data_dir, "samples_folders": self.config.train_folder, "split": "train"},
|
| 267 |
-
),
|
| 268 |
-
datasets.SplitGenerator(
|
| 269 |
-
name=datasets.Split.TEST,
|
| 270 |
-
# These kwargs will be passed to _generate_examples
|
| 271 |
-
gen_kwargs={"data_dir": data_dir, "samples_folders": self.config.test_folder, "split": "test"},
|
| 272 |
-
),
|
| 273 |
-
datasets.SplitGenerator(
|
| 274 |
-
name=datasets.Split.VALIDATION,
|
| 275 |
-
# These kwargs will be passed to _generate_examples
|
| 276 |
-
gen_kwargs={"data_dir": data_dir, "samples_folders": self.config.valid_folder, "split": "valid"},
|
| 277 |
-
),
|
| 278 |
-
]
|
| 279 |
-
|
| 280 |
-
def _generate_examples(self, data_dir, samples_folders, split):
|
| 281 |
-
"""Yields examples."""
|
| 282 |
-
# Yields (key, example) tuples from the dataset
|
| 283 |
-
|
| 284 |
-
# Training and validation are on 1 topic/genre, while testing is on multiple topics
|
| 285 |
-
# We convert the sample folders into list (from string)
|
| 286 |
-
if samples_folders.count(",") == 0:
|
| 287 |
-
samples_folders = [samples_folders]
|
| 288 |
-
else:
|
| 289 |
-
samples_folders = samples_folders.split(",")
|
| 290 |
-
|
| 291 |
-
# the dataset is structured as:
|
| 292 |
-
# |-Topic1
|
| 293 |
-
# |---author 1
|
| 294 |
-
# |------- article-1
|
| 295 |
-
# |------- article-2
|
| 296 |
-
# |---author 2
|
| 297 |
-
# |------- article-1
|
| 298 |
-
# |------- article-2
|
| 299 |
-
# |-Topic2
|
| 300 |
-
# ...
|
| 301 |
-
|
| 302 |
-
for topic in samples_folders:
|
| 303 |
-
full_path = os.path.join(data_dir, topic)
|
| 304 |
-
|
| 305 |
-
for author in os.listdir(full_path):
|
| 306 |
-
|
| 307 |
-
list_articles = os.listdir(os.path.join(full_path, author))
|
| 308 |
-
if len(list_articles) == 0:
|
| 309 |
-
# Some authors have no articles on certain topics
|
| 310 |
-
continue
|
| 311 |
-
|
| 312 |
-
for id_, article in enumerate(list_articles):
|
| 313 |
-
path_2_author = os.path.join(full_path, author)
|
| 314 |
-
path_2_article = os.path.join(path_2_author, article)
|
| 315 |
-
|
| 316 |
-
with open(path_2_article, "r", encoding="utf8", errors="ignore") as f:
|
| 317 |
-
art = f.readlines()
|
| 318 |
-
|
| 319 |
-
# The whole article is stored as one line. We access the 1st element of the list
|
| 320 |
-
# to store it as string, not as a list
|
| 321 |
-
yield f"{topic}_{author}_{id_}", {
|
| 322 |
-
"article": art[0],
|
| 323 |
-
"author": author,
|
| 324 |
-
"topic": topic,
|
| 325 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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