yes-man-today
commited on
Commit
•
083b21d
1
Parent(s):
624710d
clean some code
Browse files- regulatory_comments_api.py +72 -11
regulatory_comments_api.py
CHANGED
@@ -14,6 +14,7 @@
|
|
14 |
|
15 |
|
16 |
import requests
|
|
|
17 |
import datasets
|
18 |
from datasets import BuilderConfig
|
19 |
|
@@ -90,16 +91,69 @@ class RegulationsDataFetcher:
|
|
90 |
print(f'Failed to retrieve comment details: {response.status_code}')
|
91 |
return None
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
def collect_data(self):
|
94 |
"""Collect data and reshape into nested dictionary format."""
|
95 |
data = self.fetch_comments()
|
96 |
-
if data
|
97 |
-
return None
|
98 |
|
99 |
docket_info = self.get_docket_info()
|
100 |
-
if docket_info
|
101 |
return None
|
102 |
|
|
|
103 |
nested_data = {
|
104 |
"id": self.docket_id,
|
105 |
"agency": self.docket_id.split('-')[0],
|
@@ -111,32 +165,39 @@ class RegulationsDataFetcher:
|
|
111 |
"comments": []
|
112 |
}
|
113 |
|
|
|
114 |
if 'data' in data:
|
115 |
for comment in data['data']:
|
|
|
|
|
|
|
116 |
comment_details = self.fetch_comment_details(comment['links']['self'])
|
117 |
if 'data' in comment_details and 'attributes' in comment_details['data']:
|
118 |
comment_data = comment_details['data']['attributes']
|
|
|
|
|
119 |
comment_text = (comment_data.get('comment', '') or '').strip()
|
|
|
|
|
120 |
|
121 |
-
if
|
122 |
-
# Replace empty commenter_fname with "Anonymous"
|
123 |
nested_comment = {
|
124 |
"text": comment_text,
|
125 |
"comment_id": comment['id'],
|
126 |
"comment_url": comment['links']['self'],
|
127 |
"comment_date": comment['attributes']['postedDate'].split('T')[0],
|
128 |
"comment_time": comment['attributes']['postedDate'].split('T')[1].strip('Z'),
|
129 |
-
"commenter_fname": (
|
130 |
-
"commenter_lname": (
|
131 |
-
"comment_length": len(comment_text)
|
132 |
}
|
133 |
nested_data["comments"].append(nested_comment)
|
134 |
|
135 |
-
if len(nested_data["comments"]) >= 10:
|
136 |
-
break
|
137 |
-
|
138 |
return nested_data
|
139 |
|
|
|
|
|
|
|
140 |
|
141 |
class RegCommentsAPIConfig(BuilderConfig):
|
142 |
def __init__(self, api_key=None, docket_ids = None, **kwargs):
|
|
|
14 |
|
15 |
|
16 |
import requests
|
17 |
+
import re
|
18 |
import datasets
|
19 |
from datasets import BuilderConfig
|
20 |
|
|
|
91 |
print(f'Failed to retrieve comment details: {response.status_code}')
|
92 |
return None
|
93 |
|
94 |
+
# def collect_data(self):
|
95 |
+
# """Collect data and reshape into nested dictionary format."""
|
96 |
+
# data = self.fetch_comments()
|
97 |
+
# if data is None:
|
98 |
+
# return None
|
99 |
+
|
100 |
+
# docket_info = self.get_docket_info()
|
101 |
+
# if docket_info is None:
|
102 |
+
# return None
|
103 |
+
|
104 |
+
# nested_data = {
|
105 |
+
# "id": self.docket_id,
|
106 |
+
# "agency": self.docket_id.split('-')[0],
|
107 |
+
# "title": docket_info[1] if docket_info else "Unknown Title",
|
108 |
+
# "update_date": docket_info[2].split('T')[0] if docket_info and docket_info[2] else "Unknown Update Date",
|
109 |
+
# "update_time": docket_info[2].split('T')[1].strip('Z') if docket_info and docket_info[2] and 'T' in docket_info[2] else "Unknown Update Time",
|
110 |
+
# "purpose": docket_info[3],
|
111 |
+
# "keywords": docket_info[4],
|
112 |
+
# "comments": []
|
113 |
+
# }
|
114 |
+
|
115 |
+
# if 'data' in data:
|
116 |
+
# for comment in data['data']:
|
117 |
+
# comment_details = self.fetch_comment_details(comment['links']['self'])
|
118 |
+
|
119 |
+
# # Here, we extra information and perform cleaning.
|
120 |
+
# if 'data' in comment_details and 'attributes' in comment_details['data']:
|
121 |
+
# comment_data = comment_details['data']['attributes']
|
122 |
+
# comment_text = (comment_data.get('comment', '') or '').strip()
|
123 |
+
# comment_text = (comment_text # Doing some extra cleaning
|
124 |
+
# .replace("<br/>", "")
|
125 |
+
# .replace("<span style='padding-left: 30px'></span>", "")
|
126 |
+
# .replace(re.sub(r'&[^;]+;', '', comment_text)))
|
127 |
+
|
128 |
+
# if (comment_text and "attached" not in comment_text.lower() and "attachment" not in comment_text.lower() and comment_text.lower() != "n/a"):
|
129 |
+
# nested_comment = {
|
130 |
+
# "text": comment_text,
|
131 |
+
# "comment_id": comment['id'],
|
132 |
+
# "comment_url": comment['links']['self'],
|
133 |
+
# "comment_date": comment['attributes']['postedDate'].split('T')[0],
|
134 |
+
# "comment_time": comment['attributes']['postedDate'].split('T')[1].strip('Z'),
|
135 |
+
# "commenter_fname": ((comment_data.get('firstName') or 'Anonymous').split(',')[0]).capitalize(),
|
136 |
+
# "commenter_lname": ((comment_data.get('lastName') or 'Anonymous').split(',')[0]).capitalize(),
|
137 |
+
# "comment_length": len(comment_text) if comment_text is not None else 0
|
138 |
+
# }
|
139 |
+
# nested_data["comments"].append(nested_comment)
|
140 |
+
|
141 |
+
# if len(nested_data["comments"]) >= 10:
|
142 |
+
# break
|
143 |
+
|
144 |
+
# return nested_data
|
145 |
+
|
146 |
def collect_data(self):
|
147 |
"""Collect data and reshape into nested dictionary format."""
|
148 |
data = self.fetch_comments()
|
149 |
+
if not data:
|
150 |
+
return None
|
151 |
|
152 |
docket_info = self.get_docket_info()
|
153 |
+
if not docket_info:
|
154 |
return None
|
155 |
|
156 |
+
# Starting out with docket information
|
157 |
nested_data = {
|
158 |
"id": self.docket_id,
|
159 |
"agency": self.docket_id.split('-')[0],
|
|
|
165 |
"comments": []
|
166 |
}
|
167 |
|
168 |
+
# Adding in comment information
|
169 |
if 'data' in data:
|
170 |
for comment in data['data']:
|
171 |
+
if len(nested_data["comments"]) >= 10:
|
172 |
+
break
|
173 |
+
|
174 |
comment_details = self.fetch_comment_details(comment['links']['self'])
|
175 |
if 'data' in comment_details and 'attributes' in comment_details['data']:
|
176 |
comment_data = comment_details['data']['attributes']
|
177 |
+
|
178 |
+
# Basic comment text cleaning
|
179 |
comment_text = (comment_data.get('comment', '') or '').strip()
|
180 |
+
comment_text = comment_text.replace("<br/>", "").replace("<span style='padding-left: 30px'></span>", "")
|
181 |
+
comment_text = re.sub(r'&[^;]+;', '', comment_text)
|
182 |
|
183 |
+
if comment_text and "attached" not in comment_text.lower() and "attachment" not in comment_text.lower() and comment_text.lower() != "n/a":
|
|
|
184 |
nested_comment = {
|
185 |
"text": comment_text,
|
186 |
"comment_id": comment['id'],
|
187 |
"comment_url": comment['links']['self'],
|
188 |
"comment_date": comment['attributes']['postedDate'].split('T')[0],
|
189 |
"comment_time": comment['attributes']['postedDate'].split('T')[1].strip('Z'),
|
190 |
+
"commenter_fname": (comment_data.get('firstName', 'Anonymous').split(',')[0]).capitalize(),
|
191 |
+
"commenter_lname": (comment_data.get('lastName', 'Anonymous').split(',')[0]).capitalize(),
|
192 |
+
"comment_length": len(comment_text)
|
193 |
}
|
194 |
nested_data["comments"].append(nested_comment)
|
195 |
|
|
|
|
|
|
|
196 |
return nested_data
|
197 |
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
|
202 |
class RegCommentsAPIConfig(BuilderConfig):
|
203 |
def __init__(self, api_key=None, docket_ids = None, **kwargs):
|