Commit ·
73f08aa
1
Parent(s): 1e38a5a
Upload hansard_speech.py
Browse files- hansard_speech.py +17 -18
hansard_speech.py
CHANGED
|
@@ -17,6 +17,7 @@ A dataset containing every speech in the House of Commons from May 1979-July 202
|
|
| 17 |
|
| 18 |
import json
|
| 19 |
import os
|
|
|
|
| 20 |
import pandas as pd
|
| 21 |
from datetime import datetime
|
| 22 |
import datasets
|
|
@@ -127,36 +128,34 @@ class HansardSpeech(datasets.GeneratorBasedBuilder):
|
|
| 127 |
|
| 128 |
def _generate_examples(self, filepaths, split):
|
| 129 |
logger.warn("\nThis is a large dataset. Please be patient")
|
| 130 |
-
json_data = pd.read_json(filepaths[1]
|
| 131 |
csv_data_chunks = pd.read_csv(filepaths[0], chunksize=50000, dtype="object")
|
| 132 |
for data_chunk in csv_data_chunks:
|
| 133 |
data_chunk.fillna("", inplace=True)
|
| 134 |
for _, row in data_chunk.iterrows():
|
| 135 |
data_point = {}
|
| 136 |
for field in fields[:-3]:
|
| 137 |
-
data_point[field] = row[field] if row[field] else ""
|
| 138 |
parl_post_list = []
|
| 139 |
if data_point["mnis_id"] and data_point["date"]:
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
)
|
| 144 |
-
else:
|
| 145 |
-
speech_dt = data_point["date"] + " 00:00:00"
|
| 146 |
-
speech_dt_obj = datetime.strptime(speech_dt, "%Y-%m-%d %H:%M:%S")
|
| 147 |
-
parl_posts = json_data[
|
| 148 |
-
(json_data["mnis_id"] == data_point["mnis_id"])
|
| 149 |
-
& (json_data["date"] == speech_dt_obj)
|
| 150 |
-
]["parliamentary_posts"]
|
| 151 |
-
if len(parl_posts) > 0:
|
| 152 |
-
parl_posts = parl_posts.iloc[0]
|
| 153 |
-
for item in parl_posts:
|
| 154 |
-
parl_post_list.append(item["parl_post_name"])
|
| 155 |
-
|
| 156 |
opp_post = []
|
| 157 |
gov_post = []
|
| 158 |
data_point["government_posts"] = gov_post
|
| 159 |
data_point["opposition_posts"] = opp_post
|
| 160 |
data_point["parliamentary_posts"] = parl_post_list
|
| 161 |
yield data_point["id"], data_point
|
| 162 |
-
|
|
|
|
| 17 |
|
| 18 |
import json
|
| 19 |
import os
|
| 20 |
+
import time
|
| 21 |
import pandas as pd
|
| 22 |
from datetime import datetime
|
| 23 |
import datasets
|
|
|
|
| 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 |
+
|