|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import requests |
|
import datasets |
|
from datasets import BuilderConfig |
|
|
|
_DESCRIPTION = """\ |
|
United States governmental agencies often make proposed regulations open to the public for comment. |
|
Proposed regulations are organized into "dockets". This dataset will use Regulation.gov public API |
|
to aggregate and clean public comments for dockets that mention opioid use. |
|
|
|
Each example will consist of one docket, and include metadata such as docket id, docket title, etc. |
|
Each docket entry will also include information about the top 10 comments, including comment metadata |
|
and comment text. |
|
""" |
|
|
|
|
|
_HOMEPAGE = "https://www.regulations.gov/" |
|
|
|
|
|
_URLS = {"url": "https://huggingface.co/datasets/ro-h/regulatory_comments/raw/main/docket_comments_all.json"} |
|
|
|
class RegulationsDataFetcher: |
|
BASE_COMMENT_URL = 'https://api.regulations.gov/v4/comments' |
|
BASE_DOCKET_URL = 'https://api.regulations.gov/v4/dockets/' |
|
|
|
def __init__(self, docket_id, api_key): |
|
self.docket_id = docket_id |
|
self.api_key = api_key |
|
self.docket_url = self.BASE_DOCKET_URL + docket_id |
|
self.headers = { |
|
'X-Api-Key': self.api_key, |
|
'Content-Type': 'application/json' |
|
} |
|
|
|
def fetch_comments(self): |
|
"""Fetch a single page of 25 comments.""" |
|
url = f'{self.BASE_COMMENT_URL}?filter[docketId]={self.docket_id}&page[number]=1&page[size]=25' |
|
response = requests.get(url, headers=self.headers) |
|
|
|
if response.status_code == 200: |
|
return response.json() |
|
elif response.status_code == 429: |
|
print(f'API Rate Limit Reached') |
|
return None |
|
|
|
else: |
|
print(f'Failed to retrieve comments: {response.status_code}') |
|
return None |
|
|
|
def get_docket_info(self): |
|
"""Get docket information.""" |
|
response = requests.get(self.docket_url, headers=self.headers) |
|
|
|
if response.status_code == 200: |
|
docket_data = response.json() |
|
return (docket_data['data']['attributes']['agencyId'], |
|
docket_data['data']['attributes']['title'], |
|
docket_data['data']['attributes']['modifyDate'], |
|
docket_data['data']['attributes']['docketType'], |
|
docket_data['data']['attributes']['keywords']) |
|
elif response.status_code == 429: |
|
print(f'API Rate Limit Reached') |
|
return None |
|
|
|
else: |
|
print(f'Failed to retrieve docket info: {response.status_code}') |
|
return None |
|
|
|
def fetch_comment_details(self, comment_url): |
|
"""Fetch detailed information of a comment.""" |
|
response = requests.get(comment_url, headers=self.headers) |
|
|
|
if response.status_code == 200: |
|
return response.json() |
|
else: |
|
print(f'Failed to retrieve comment details: {response.status_code}') |
|
return None |
|
|
|
def collect_data(self): |
|
"""Collect data and reshape into nested dictionary format.""" |
|
data = self.fetch_comments() |
|
if data is None: |
|
return None |
|
|
|
docket_info = self.get_docket_info() |
|
if docket_info is None: |
|
return None |
|
|
|
|
|
nested_data = { |
|
"id": self.docket_id, |
|
"title": docket_info[1] if docket_info else "Unknown Title", |
|
"context": docket_info[2] if docket_info else "Unknown Context", |
|
"purpose": docket_info[3], |
|
"keywords": docket_info[4], |
|
"comments": [] |
|
} |
|
|
|
if data and 'data' in data: |
|
for comment in data['data']: |
|
comment_details = self.fetch_comment_details(comment['links']['self']) |
|
|
|
if comment_details and 'data' in comment_details and 'attributes' in comment_details['data']: |
|
comment_data = comment_details['data']['attributes'] |
|
nested_comment = { |
|
"text": comment_data.get('comment', ''), |
|
"comment_id": comment['id'], |
|
"comment_url": comment['links']['self'], |
|
"comment_date": comment['attributes']['postedDate'], |
|
"comment_title": comment['attributes']['title'], |
|
"commenter_fname": comment_data.get('firstName', ''), |
|
"commenter_lname": comment_data.get('lastName', ''), |
|
"comment_length": len(comment_data.get('comment', '')) if comment_data.get('comment') is not None else 0 |
|
} |
|
nested_data["comments"].append(nested_comment) |
|
|
|
if len(nested_data["comments"]) >= 10: |
|
break |
|
|
|
return nested_data |
|
|
|
|
|
class RegCommentsAPIConfig(BuilderConfig): |
|
def __init__(self, api_key=None, **kwargs): |
|
self.api_key = api_key |
|
super(RegCommentsAPIConfig, self).__init__(**kwargs) |
|
|
|
|
|
class RegCommentsAPI(datasets.GeneratorBasedBuilder): |
|
VERSION = datasets.Version("1.1.0") |
|
|
|
BUILDER_CONFIGS = [ |
|
RegCommentsAPIConfig( |
|
name="default", |
|
version=datasets.Version("1.0.0"), |
|
description="Dataset of regulatory comments", |
|
) |
|
] |
|
BUILDER_CONFIG_CLASS = RegCommentsAPIConfig |
|
|
|
|
|
def _info(self): |
|
|
|
features = datasets.Features({ |
|
"id": datasets.Value("string"), |
|
"title": datasets.Value("string"), |
|
"context": datasets.Value("string"), |
|
"purpose": datasets.Value("string"), |
|
"keywords": datasets.Sequence(datasets.Value("string")), |
|
"comments": datasets.Sequence({ |
|
"text": datasets.Value("string"), |
|
"comment_id": datasets.Value("string"), |
|
"comment_url": datasets.Value("string"), |
|
"comment_date": datasets.Value("string"), |
|
"comment_title": datasets.Value("string"), |
|
"commenter_fname": datasets.Value("string"), |
|
"commenter_lname": datasets.Value("string"), |
|
"comment_length": datasets.Value("int32") |
|
}) |
|
}) |
|
|
|
|
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=features, |
|
homepage=_HOMEPAGE |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
|
|
api_key = self.config.api_key |
|
|
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"api_key": api_key, |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, api_key): |
|
|
|
dockets = ['FDA-2000-P-0565', 'FDA-1995-P-0018', 'FDA-2010-P-0338', |
|
'FDA-2017-N-2903', 'FDA-1976-N-0517', 'CDC-2008-0002', |
|
'DEA-2017-0006', 'DEA-2017-0004', 'DEA-2023-0028', |
|
'DEA-2007-0060', 'FDA-2005-P-0191', 'CDC-2017-0057', |
|
'FDA-2003-P-0115', 'FDA-2012-P-1028', 'FDA-1989-P-0003', |
|
'FDA-2009-P-0415', 'FDA-2007-P-0352', 'FDA-2007-P-0124', |
|
'FDA-2009-P-0459', 'FDA-2014-P-2168', 'FDA-2014-N-0233', |
|
'FDA-2002-P-0291', 'DOD-2012-HA-0117', 'FDA-2015-P-1004', |
|
'DEA-2016-0005', 'FDA-2002-P-0049', 'FDA-2008-P-0327', |
|
'FDA-2022-D-1847', 'FDA-2015-P-2344', 'FDA-2009-P-0429', |
|
'FDA-2012-P-0053', 'FDA-2009-P-0289', 'DEA-2023-0106', |
|
'FDA-1986-P-0002', 'CMS-2019-0090', 'FDA-2018-D-1334', |
|
'FDA-1975-N-0003', 'FDA-2019-N-5973', 'FDA-2004-P-0101', |
|
'FDA-2023-N-0148', 'FDA-2000-P-0580', 'FDA-2007-P-0128', |
|
'FDA-2017-N-5608', 'FDA-1994-P-0014', 'DEA-2016-0018', |
|
'FDA-2017-P-6918', 'DOD-2013-HA-0085', 'FDA-2003-P-0270', |
|
'FDA-2011-P-0869', 'FDA-2010-P-0593', 'FDA-2012-P-1251', |
|
'DEA-2014-0005', 'FDA-1994-P-0003', 'FDA-2021-P-0514', |
|
'FDA-1987-P-0026', 'FDA-2013-N-1485', 'FDA-1993-N-0363', |
|
'FDA-2017-P-0428', 'SAMHSA-2023-0001', 'FDA-2012-P-1180', |
|
'FDA-2019-P-5268', 'DEA-2017-0011', 'FDA-2016-P-3912', |
|
'DOD-2015-HA-0109', 'FDA-2022-P-1863', 'FDA-2011-N-0802', |
|
'FDA-1996-P-0154', 'FDA-2021-N-0951', 'DEA-2015-0009', |
|
'FDA-2022-N-2393', 'FDA-2019-P-1783', 'FDA-2017-N-1094', |
|
'CDC-2018-0085', 'FDA-2005-P-0327', 'FDA-2014-P-2058', |
|
'DEA-2020-0031', 'FDA-2018-P-3355', 'FDA-2003-P-0183', |
|
'FDA-2002-P-0167', 'DOD-2015-OS-0118', 'FDA-2008-P-0401', |
|
'FDA-2013-P-1606', 'ONDCP_FRDOC_0001', 'FDA-1998-P-0018', |
|
'FDA-2008-P-0366', 'FDA-2013-D-0045', 'FDA-2013-P-1055', |
|
'FDA-2017-N-4515', 'FDA-1982-N-0046', 'FDA-2018-P-0673', |
|
'FDA-2012-P-0818', 'FDA-2019-P-1643', 'FDA-2004-P-0098', |
|
'FDA-2008-P-0127', 'FDA-2004-P-0096', 'DEA-2019-0014', |
|
'FDA-2019-N-0299', 'FDA-1994-P-0021', 'FDA-2021-N-0275', |
|
'CDC-2017-0043', 'SAMHSA-2016-0001', 'VA-2020-VHA-0023', |
|
'CNCS-2021-0018', 'FDA-2005-P-0054', 'DEA-2017-0005', |
|
'FDA-2019-P-5593', 'FDA-1997-P-0036', 'FDA-2002-P-0051', |
|
'SAMHSA-2016-0004', 'FDA-2016-D-0785', 'FDA-2020-N-1561', |
|
'SAMHSA-2022-0001', 'DEA-2018-0015', 'FDA-2010-P-0082', |
|
'FDA-2023-P-2656', 'FDA-2004-P-0085', 'FDA-2007-P-0415', |
|
'DEA-2017-0013', 'DEA-2018-0004', 'FDA-2005-P-0058', |
|
'FDA-2002-N-0320', 'FDA-2014-P-0752', 'FDA-2002-P-0219', |
|
'DEA-2013-0010', 'FDA-2003-N-0396', 'FDA-2005-P-0425', |
|
'CMS-2020-0060', 'FRA-2019-0071', 'FDA-2013-N-0883', |
|
'FDA-2009-P-0154', 'HHS-OS-2009-0007', 'CDC-2020-0001', |
|
'FDA-2012-P-0189', 'DEA-2023-0058', 'FDA-2017-P-1911', |
|
'DOD-2011-HA-0085', 'FDA-2017-P-6307', 'FDA-2004-P-0162', |
|
'FDA-2001-P-0056', 'FDA-2013-P-0675', 'FDA-1993-P-0018', |
|
'FDA-2017-P-5370', 'FDA-2012-N-0548', 'FDA-2012-N-0563', |
|
'FDA-2009-P-0388', 'FDA-2001-P-0416', 'FDA-2005-N-0093', |
|
'FDA-2002-P-0003', 'FDA-2016-P-1875', 'FDA-1987-P-0012', |
|
'FDA-2004-P-0216', 'FDA-2007-P-0065', 'FDA-2015-P-0516', |
|
'FDA-2023-N-3159', 'FDA-2008-P-0156', 'FDA-2021-P-0036', |
|
'FDA-1995-P-0100', 'FDA-2012-P-1034', 'DEA-2018-0002', |
|
'FDA-2015-P-1153', 'FDA-1995-P-0004', 'FDA-1985-P-0014', |
|
'FDA-2016-P-1090', 'FDA-1977-N-0411', 'FDA-2009-P-0325', |
|
'FDA-2005-P-0440', 'FDA-2019-P-1679', 'FDA-2011-P-0045', |
|
'DEA-2020-0034', 'DOD-2015-HA-0062', 'FDA-2018-P-1448', |
|
'FDA-2009-P-0446', 'FDA-2006-P-0279', 'FDA-2013-P-0995', |
|
'CMS-2022-0191', 'FDA-2009-P-0227', 'FDA-2005-P-0009', |
|
'USCBP-2013-0002', 'HHS-ASPE-2016-0011', 'FDA-2011-P-0302', |
|
'FDA-2003-P-0185', 'FDA-2012-E-0152', 'FDA-1994-P-0016', |
|
'FDA-2016-N-2648', 'CMS-2019-0111', 'FDA-2022-N-2673', |
|
'FDA-1988-P-0026', 'FDA-1999-P-3513', 'DOT-OST-2016-0189', |
|
'FDA-2017-P-0663', 'FDA-2003-P-0276', 'FDA-2006-P-0329', |
|
'FDA-2016-P-4094', 'FDA-1993-P-0041', 'FDA-2005-P-0325', |
|
'DEA-2020-0002', 'FDA-2007-E-0400', 'FDA-2002-P-0050', |
|
'VA-2017-VHA-0004', 'FDA-2000-P-0024', 'FDA-2002-P-0528', |
|
'FDA-2009-P-0252', 'FDA-2017-D-2497', 'CNCS-2021-0011', |
|
'FDA-2006-P-0148', 'DEA-2008-0012', 'FDA-2018-N-3685', |
|
'FDA-2023-P-4223', 'FDA-2014-P-1383', 'ONDCP-2020-0001', |
|
'FDA-2008-P-0134', 'FDA-2007-P-0245', 'FDA-2013-P-1711', |
|
'FDA-2016-P-1650', 'FDA-2019-P-1364', 'FDA-2015-P-1003', |
|
'FDA-2007-N-0438', 'FDA-1985-P-0002', 'FDA-2007-P-0062', |
|
'SAMHSA-2022-0002', 'FDA-2006-P-0016', 'FDA-2010-P-0012', |
|
'FDA-2006-P-0278', 'FDA-1981-N-0248', 'CMS-2021-0167', |
|
'FDA-2015-P-1752', 'FDA-2005-P-0055', 'FDA-2004-P-0473', |
|
'FDA-2001-P-0191', 'FDA-1986-P-0034'] |
|
|
|
for docket_id in dockets: |
|
fetcher = RegulationsDataFetcher(docket_id, api_key) |
|
docket_data = fetcher.collect_data() |
|
|
|
if docket_data is None: |
|
print(f"Error with API Retrieval. Pausing Data Collection.") |
|
break |
|
|
|
if len(docket_data["comments"]) != 0: |
|
yield docket_id, docket_data |
|
|
|
|
|
|