File size: 10,285 Bytes
e844be7 |
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# 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.
"""The Corpus of Late Modern English Texts, version 3.1 (CLMET3.1) has been created by Hendrik De Smet,
Susanne Flach, Hans-Jürgen Diller and Jukka Tyrkkö, as an offshoot of a bigger project developing a database
of text descriptors (Diller, De Smet & Tyrkkö 2011). CLMET3.1 is a principled collection of public domain
texts drawn from various online archiving projects. """
import os
import xml.etree.ElementTree as ET
import datasets
from bs4 import BeautifulSoup
_CITATION = """@article{de2015corpus,
title={Corpus of Late Modern English texts (version 3.1)},
author={De Smet, Hendrik and Flach, Susanne and Tyrkk{\"o}, Jukka and Diller, Hans-J{\"u}rgen},
year={2015}
}
"""
_DESCRIPTION = """The Corpus of Late Modern English Texts, version 3.1 (CLMET3.1) has been created by Hendrik De Smet,
Susanne Flach, Hans-Jürgen Diller and Jukka Tyrkkö, as an offshoot of a bigger project developing a database of text
descriptors (Diller, De Smet & Tyrkkö 2011). CLMET3.1 is a principled collection of public domain texts drawn from
various online archiving projects. This dataset can be used for part-of-speech tagging, NER and text classification
"""
_HOMEPAGE = "http://fedora.clarin-d.uni-saarland.de/clmet/clmet.html"
_LICENSE = "Creative Commons Attribution Non Commercial Share Alike 4.0 International"
_DATASETNAME = "clmet"
_URLS = {
_DATASETNAME: "http://fedora.clarin-d.uni-saarland.de/clmet/clmet3_1.zip",
}
_POS_LIST = [
"CC",
"CD",
"DT",
"EX",
"FW",
"IN",
"JJ",
"JJR",
"JJS",
"MD",
"NN",
"NNS",
"NP",
"NPS",
"PDT",
"POS",
"PP",
"PP$",
"RB",
"RBR",
"RBS",
"RP",
"SENT",
"SYM",
"TO",
"UH",
"VB",
"VBD",
"VBG",
"VBN",
"VBZ",
"VBP",
"WDT",
"WP",
"WP$",
"WRB",
"XX0",
"CURR",
"PUN",
"LQUO",
"RQUO",
"BRL",
"BRR",
"LS",
]
_POS_LOOKUP = {tag: idx for idx, tag in enumerate(_POS_LIST)}
_CLASS_LIST = [
"ADJ",
"ADV",
"ART",
"CONJ",
"INTJ",
"PREP",
"PRON",
"PUNC",
"SUBST",
"SYM",
"UNC",
"VERB",
"QUOT"
]
_CLASS_LOOKUP = {tag: idx for idx, tag in enumerate(_CLASS_LIST)}
logger = datasets.utils.logging.get_logger(__name__)
class CLMET_3_1(datasets.GeneratorBasedBuilder):
""""""
VERSION = datasets.Version("3.1.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="plain",
version=VERSION,
description="This format contains text as single string and the classifications",
),
datasets.BuilderConfig(
name="class",
version=VERSION,
description="This format contains the text as a list of tokens, annotated according to the simplified Oxford wordclass tags",
),
datasets.BuilderConfig(
name="pos",
version=VERSION,
description="This format contains the text as a list of tokens, annotated according to the Penn Treebank POS tags",
),
]
DEFAULT_CONFIG_NAME = "plain"
def _info(self):
if self.config.name == "plain":
features = datasets.Features(
{
"text": datasets.Value("string"),
"genre": datasets.Value("string"),
"subgenre": datasets.Value("string"),
"year": datasets.Value("string"),
"quarter_cent": datasets.Value("string"),
"decade": datasets.Value("string"),
"title": datasets.Value("string"),
"author": datasets.Value("string"),
"notes": datasets.Value("string"),
"comments": datasets.Value("string"),
"period": datasets.Value("string"),
"id": datasets.Value("string"),
}
)
elif self.config.name == "class":
logger.warn(f"CLASS tags are as follows: {_CLASS_LIST}")
features = datasets.Features(
{
"text": datasets.Sequence(datasets.Value("string")),
"pos_tags": datasets.Sequence(datasets.Value("int32")),
"genre": datasets.Value("string"),
"subgenre": datasets.Value("string"),
"year": datasets.Value("string"),
"quarter_cent": datasets.Value("string"),
"decade": datasets.Value("string"),
"title": datasets.Value("string"),
"author": datasets.Value("string"),
"notes": datasets.Value("string"),
"comments": datasets.Value("string"),
"period": datasets.Value("string"),
"id": datasets.Value("string"),
}
)
elif self.config.name == "pos":
logger.warn(f"POS tags are as follows: {_POS_LIST}")
features = datasets.Features(
{
"text": datasets.Sequence(datasets.Value("string")),
"pos_tags": datasets.Sequence(datasets.Value("int32")),
"genre": datasets.Value("string"),
"subgenre": datasets.Value("string"),
"year": datasets.Value("string"),
"quarter_cent": datasets.Value("string"),
"decade": datasets.Value("string"),
"title": datasets.Value("string"),
"author": datasets.Value("string"),
"notes": datasets.Value("string"),
"comments": datasets.Value("string"),
"period": datasets.Value("string"),
"id": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
urls = _URLS[_DATASETNAME]
data_dir = dl_manager.download_and_extract(urls)
data_dir = os.path.join(data_dir, "clmet", "corpus", "txt")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"data_dir": data_dir,
"split": "train",
},
),
]
def parse_pos_text(self, content_parts, pos_type):
tokens = []
pos_tags = []
unknown_tag = False
malformed_token = False
for content_part in content_parts:
text = content_part.text.strip()
for text_part in text.split():
try:
token, pos_tag = text_part.split("_")
pos_tag = pos_tag.replace("\n", "").strip().upper()
if pos_type == "pos":
pos_tag_idx = _POS_LOOKUP.get(pos_tag,-1)
else:
pos_tag_idx = _CLASS_LOOKUP.get(pos_tag,-1)
if pos_tag_idx==-1:
unknown_tag = True
tokens.append(token)
pos_tags.append(pos_tag_idx)
except Exception as e:
malformed_token = True
return tokens, pos_tags, unknown_tag, malformed_token
def parse_file(self, file, pos_type):
with open(file, "r", encoding="utf-8") as fp:
soup = BeautifulSoup(fp, features="html.parser")
id = soup.id.text
period = soup.period.text
quarter_cent = soup.quartcent.text
decade = soup.decade.text
year = soup.year.text
genre = soup.genre.text
subgenre = soup.subgenre.text
title = soup.title.text
notes = soup.notes.text
comments = soup.comments.text
author = soup.author.text
data_point = {
"id": id,
"period": period,
"genre": genre,
"subgenre": subgenre,
"decade": decade,
"quarter_cent": quarter_cent,
"title": title,
"notes": notes if notes else "",
"comments": comments if comments else "",
"author": author,
"year": year,
}
content_parts = soup.find("text").find_all("p")
if pos_type in ["pos", "class"]:
content = self.parse_pos_text(content_parts, pos_type)
if content[2]:
logger.warn(f'Unknown tag in sample {id}')
if content[3]:
logger.warn(f'Malformed token in sample {id}')
data_point["text"] = content[0]
data_point["pos_tags"] = content[1]
else:
content = []
for content_part in content_parts:
content.append(content_part.text)
content = " ".join(content)
data_point["text"] = content
return (id, data_point)
def _generate_examples(self, data_dir, split):
final_data_dir = os.path.join(data_dir, self.config.name)
for file in os.listdir(final_data_dir):
id, data = self.parse_file(
os.path.join(final_data_dir, file), self.config.name
)
yield id, data
|