Commit
·
cd925f1
1
Parent(s):
579641b
Delete loading script
Browse files- hansard_speech.py +0 -161
hansard_speech.py
DELETED
|
@@ -1,161 +0,0 @@
|
|
| 1 |
-
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 2 |
-
#
|
| 3 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
-
# you may not use this file except in compliance with the License.
|
| 5 |
-
# You may obtain a copy of the License at
|
| 6 |
-
#
|
| 7 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
-
#
|
| 9 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
-
# See the License for the specific language governing permissions and
|
| 13 |
-
# limitations under the License.
|
| 14 |
-
"""
|
| 15 |
-
A dataset containing every speech in the House of Commons from May 1979-July 2020.
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
import json
|
| 19 |
-
import os
|
| 20 |
-
import time
|
| 21 |
-
import pandas as pd
|
| 22 |
-
from datetime import datetime
|
| 23 |
-
import datasets
|
| 24 |
-
|
| 25 |
-
_CITATION = """@misc{odell, evan_2021,
|
| 26 |
-
title={Hansard Speeches 1979-2021: Version 3.1.0},
|
| 27 |
-
DOI={10.5281/zenodo.4843485},
|
| 28 |
-
abstractNote={<p>Full details are available at <a href="https://evanodell.com/projects/datasets/hansard-data">https://evanodell.com/projects/datasets/hansard-data</a></p> <p><strong>Version 3.1.0 contains the following changes:</strong></p> <p>- Coverage up to the end of April 2021</p>},
|
| 29 |
-
note={This release is an update of previously released datasets. See full documentation for details.},
|
| 30 |
-
publisher={Zenodo},
|
| 31 |
-
author={Odell, Evan},
|
| 32 |
-
year={2021},
|
| 33 |
-
month={May} }
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
-
_DESCRIPTION = """
|
| 37 |
-
A dataset containing every speech in the House of Commons from May 1979-July 2020.
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
-
_HOMEPAGE = "https://evanodell.com/projects/datasets/hansard-data/"
|
| 41 |
-
|
| 42 |
-
_LICENSE = "Creative Commons Attribution 4.0 International License"
|
| 43 |
-
|
| 44 |
-
_URLS = {
|
| 45 |
-
"csv": "https://zenodo.org/record/4843485/files/hansard-speeches-v310.csv.zip?download=1",
|
| 46 |
-
"json": "https://zenodo.org/record/4843485/files/parliamentary_posts.json?download=1",
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
fields = [
|
| 50 |
-
"id",
|
| 51 |
-
"speech",
|
| 52 |
-
"display_as",
|
| 53 |
-
"party",
|
| 54 |
-
"constituency",
|
| 55 |
-
"mnis_id",
|
| 56 |
-
"date",
|
| 57 |
-
"time",
|
| 58 |
-
"colnum",
|
| 59 |
-
"speech_class",
|
| 60 |
-
"major_heading",
|
| 61 |
-
"minor_heading",
|
| 62 |
-
"oral_heading",
|
| 63 |
-
"year",
|
| 64 |
-
"hansard_membership_id",
|
| 65 |
-
"speakerid",
|
| 66 |
-
"person_id",
|
| 67 |
-
"speakername",
|
| 68 |
-
"url",
|
| 69 |
-
"parliamentary_posts",
|
| 70 |
-
"opposition_posts",
|
| 71 |
-
"government_posts",
|
| 72 |
-
]
|
| 73 |
-
|
| 74 |
-
logger = datasets.utils.logging.get_logger(__name__)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
class HansardSpeech(datasets.GeneratorBasedBuilder):
|
| 78 |
-
"""A dataset containing every speech in the House of Commons from May 1979-July 2020."""
|
| 79 |
-
|
| 80 |
-
VERSION = datasets.Version("3.1.0")
|
| 81 |
-
|
| 82 |
-
def _info(self):
|
| 83 |
-
features = datasets.Features(
|
| 84 |
-
{
|
| 85 |
-
"id": datasets.Value("string"),
|
| 86 |
-
"speech": datasets.Value("string"),
|
| 87 |
-
"display_as": datasets.Value("string"),
|
| 88 |
-
"party": datasets.Value("string"),
|
| 89 |
-
"constituency": datasets.Value("string"),
|
| 90 |
-
"mnis_id": datasets.Value("string"),
|
| 91 |
-
"date": datasets.Value("string"),
|
| 92 |
-
"time": datasets.Value("string"),
|
| 93 |
-
"colnum": datasets.Value("string"),
|
| 94 |
-
"speech_class": datasets.Value("string"),
|
| 95 |
-
"major_heading": datasets.Value("string"),
|
| 96 |
-
"minor_heading": datasets.Value("string"),
|
| 97 |
-
"oral_heading": datasets.Value("string"),
|
| 98 |
-
"year": datasets.Value("string"),
|
| 99 |
-
"hansard_membership_id": datasets.Value("string"),
|
| 100 |
-
"speakerid": datasets.Value("string"),
|
| 101 |
-
"person_id": datasets.Value("string"),
|
| 102 |
-
"speakername": datasets.Value("string"),
|
| 103 |
-
"url": datasets.Value("string"),
|
| 104 |
-
"government_posts": datasets.Sequence(datasets.Value("string")),
|
| 105 |
-
"opposition_posts": datasets.Sequence(datasets.Value("string")),
|
| 106 |
-
"parliamentary_posts": datasets.Sequence(datasets.Value("string")),
|
| 107 |
-
}
|
| 108 |
-
)
|
| 109 |
-
return datasets.DatasetInfo(
|
| 110 |
-
description=_DESCRIPTION,
|
| 111 |
-
features=features,
|
| 112 |
-
homepage=_HOMEPAGE,
|
| 113 |
-
license=_LICENSE,
|
| 114 |
-
citation=_CITATION,
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
def _split_generators(self, dl_manager):
|
| 118 |
-
temp_dir = dl_manager.download_and_extract(_URLS["csv"])
|
| 119 |
-
csv_file = os.path.join(temp_dir, "hansard-speeches-v310.csv")
|
| 120 |
-
json_file = dl_manager.download(_URLS["json"])
|
| 121 |
-
return [
|
| 122 |
-
datasets.SplitGenerator(
|
| 123 |
-
name=datasets.Split.TRAIN,
|
| 124 |
-
# These kwargs will be passed to _generate_examples
|
| 125 |
-
gen_kwargs={"filepaths": [csv_file, json_file], "split": "train",},
|
| 126 |
-
),
|
| 127 |
-
]
|
| 128 |
-
|
| 129 |
-
def _generate_examples(self, filepaths, split):
|
| 130 |
-
logger.warn("\nThis is a large dataset. Please be patient")
|
| 131 |
-
json_data = pd.read_json(filepaths[1])
|
| 132 |
-
csv_data_chunks = pd.read_csv(filepaths[0], chunksize=50000, dtype="object")
|
| 133 |
-
for data_chunk in csv_data_chunks:
|
| 134 |
-
data_chunk.fillna("", inplace=True)
|
| 135 |
-
for _, row in data_chunk.iterrows():
|
| 136 |
-
data_point = {}
|
| 137 |
-
for field in fields[:-3]:
|
| 138 |
-
data_point[field] = str(row[field]) if row[field] else ""
|
| 139 |
-
parl_post_list = []
|
| 140 |
-
if data_point["mnis_id"] and data_point["date"]:
|
| 141 |
-
speech_dt = data_point["date"] + " 00:00:00"
|
| 142 |
-
try:
|
| 143 |
-
parl_posts = json_data[
|
| 144 |
-
(json_data["mnis_id"] == int(data_point["mnis_id"]))
|
| 145 |
-
& (json_data["date"] == speech_dt)
|
| 146 |
-
]["parliamentary_posts"]
|
| 147 |
-
if len(parl_posts) > 0:
|
| 148 |
-
parl_posts = parl_posts.iloc[0]
|
| 149 |
-
for item in parl_posts:
|
| 150 |
-
parl_post_list.append(item["parl_post_name"])
|
| 151 |
-
except Exception as e:
|
| 152 |
-
logger.warn(
|
| 153 |
-
f"Data could not be fetched for mnis_id: {data_point['mnis_id']}, date: {data_point['date']}\nError: {repr(e)}"
|
| 154 |
-
)
|
| 155 |
-
opp_post = []
|
| 156 |
-
gov_post = []
|
| 157 |
-
data_point["government_posts"] = gov_post
|
| 158 |
-
data_point["opposition_posts"] = opp_post
|
| 159 |
-
data_point["parliamentary_posts"] = parl_post_list
|
| 160 |
-
yield data_point["id"], data_point
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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