shamikbose89 commited on
Commit
774997a
1 Parent(s): beb9501

Upload hansard_speech.py

Browse files
Files changed (1) hide show
  1. hansard_speech.py +151 -0
hansard_speech.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 os
19
+ import pandas as pd
20
+
21
+ import datasets
22
+
23
+ _CITATION = """@misc{odell, evan_2021,
24
+ title={Hansard Speeches 1979-2021: Version 3.1.0},
25
+ DOI={10.5281/zenodo.4843485},
26
+ 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>},
27
+ note={This release is an update of previously released datasets. See full documentation for details.},
28
+ publisher={Zenodo},
29
+ author={Odell, Evan},
30
+ year={2021},
31
+ month={May} }
32
+ """
33
+
34
+ _DESCRIPTION = """
35
+ A dataset containing every speech in the House of Commons from May 1979-July 2020.
36
+ """
37
+
38
+ _HOMEPAGE = "https://evanodell.com/projects/datasets/hansard-data/"
39
+
40
+ _LICENSE = "Creative Commons Attribution 4.0 International License"
41
+
42
+ _URLS = {
43
+ "csv": "https://zenodo.org/record/4843485/files/hansard-speeches-v310.csv.zip?download=1",
44
+ "json": "https://zenodo.org/record/4843485/files/parliamentary_posts.json?download=1",
45
+ }
46
+
47
+ fields = [
48
+ "id",
49
+ "speech",
50
+ "display_as",
51
+ "party",
52
+ "constituency",
53
+ "mnis_id",
54
+ "date",
55
+ "time",
56
+ "colnum",
57
+ "speech_class",
58
+ "major_heading",
59
+ "minor_heading",
60
+ "oral_heading",
61
+ "year",
62
+ "hansard_membership_id",
63
+ "speakerid",
64
+ "person_id",
65
+ "speakername",
66
+ "url",
67
+ "parliamentary_posts",
68
+ "opposition_posts",
69
+ "government_posts",
70
+ ]
71
+
72
+ logger = datasets.utils.logging.get_logger(__name__)
73
+
74
+
75
+ class HansardSpeech(datasets.GeneratorBasedBuilder):
76
+ """A dataset containing every speech in the House of Commons from May 1979-July 2020."""
77
+
78
+ VERSION = datasets.Version("3.1.0")
79
+
80
+ def _info(self):
81
+ features = datasets.Features(
82
+ {
83
+ "id": datasets.Value("string"),
84
+ "speech": datasets.Value("string"),
85
+ "display_as": datasets.Value("string"),
86
+ "party": datasets.Value("string"),
87
+ "constituency": datasets.Value("string"),
88
+ "mnis_id": datasets.Value("string"),
89
+ "date": datasets.Value("string"),
90
+ "time": datasets.Value("string"),
91
+ "colnum": datasets.Value("string"),
92
+ "speech_class": datasets.Value("string"),
93
+ "major_heading": datasets.Value("string"),
94
+ "minor_heading": datasets.Value("string"),
95
+ "oral_heading": datasets.Value("string"),
96
+ "year": datasets.Value("string"),
97
+ "hansard_membership_id": datasets.Value("string"),
98
+ "speakerid": datasets.Value("string"),
99
+ "person_id": datasets.Value("string"),
100
+ "speakername": datasets.Value("string"),
101
+ "url": datasets.Value("string"),
102
+ "government_posts": datasets.Sequence(datasets.Value("string")),
103
+ "opposition_posts": datasets.Sequence(datasets.Value("string")),
104
+ "parliamentary_posts": datasets.Sequence(datasets.Value("string")),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ homepage=_HOMEPAGE,
111
+ license=_LICENSE,
112
+ citation=_CITATION,
113
+ )
114
+
115
+ def _split_generators(self, dl_manager):
116
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
117
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
118
+
119
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
120
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
121
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
122
+ temp_dir = dl_manager.download_and_extract(_URLS["csv"])
123
+ csv_file = os.path.join(temp_dir, "hansard-speeches-v310.csv")
124
+ json_file = dl_manager.download(_URLS["json"])
125
+ return [
126
+ datasets.SplitGenerator(
127
+ name=datasets.Split.TRAIN,
128
+ # These kwargs will be passed to _generate_examples
129
+ gen_kwargs={"filepaths": [csv_file, json_file], "split": "train",},
130
+ ),
131
+ ]
132
+
133
+ def _generate_examples(self, filepaths, split):
134
+ logger.warn("\nThis is a large dataset. Please be patient")
135
+ json_data = pd.read_json(filepaths[1])
136
+ csv_data_chunks = pd.read_csv(filepaths[0], chunksize=50000)
137
+ for data_chunk in csv_data_chunks:
138
+ for _, row in data_chunk.iterrows():
139
+ data_point = {}
140
+ for field in fields[:-3]:
141
+ data_point[field] = row[field]
142
+ parl_post = json_data.loc[
143
+ (json_data["mnis_id"] == data_point["mnis_id"])
144
+ & (json_data["date"] == data_point["date"])
145
+ ]
146
+ opp_post = []
147
+ gov_post = []
148
+ data_point["government_posts"] = gov_post
149
+ data_point["opposition_posts"] = opp_post
150
+ data_point["parliamentary_posts"] = parl_post
151
+ yield data_point["id"], data_point