{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "bab83122-6939-4366-8a44-17cf9d96621e", "metadata": {}, "outputs": [], "source": [ "import xml.etree.ElementTree as ET\n", "import re\n", "import os\n", "import json" ] }, { "cell_type": "code", "execution_count": 2, "id": "29e40ec8-5ed8-458f-b071-07c4a1d88ffb", "metadata": {}, "outputs": [], "source": [ "cleaned_data = []\n", "cleaned_test_data = []" ] }, { "cell_type": "markdown", "id": "2c03b036-edc1-4e40-9f43-36a5ccbcc787", "metadata": {}, "source": [ "# FUNCTION TO CLEAN DATA FILES IN SPECIFIC DIRECTORIES" ] }, { "cell_type": "code", "execution_count": 3, "id": "44c5a2ac-3e10-439d-8c0c-65bff7ce4c9f", "metadata": {}, "outputs": [], "source": [ "def clean_data(summary_path : str, bill_path : str):\n", " sums_path = summary_path\n", " bills_path = bill_path\n", " \n", " # GETTING A LIST OF THE FILE NAMES FOR THE SUMMARY DATA FILES\n", " sums_dir_list = []\n", " for x in os.listdir(sums_path):\n", " if x.endswith(\".xml\"):\n", " sums_dir_list.append(x)\n", " sums_dir_list = sorted(sums_dir_list)\n", "\n", " # GETTING A LIST OF THE FILE NAMES FOR THE BILL DATA FILES\n", " bills_dir_list = []\n", " for x in os.listdir(bills_path):\n", " if x.endswith(\".xml\"):\n", " bills_dir_list.append(x)\n", " bills_dir_list = sorted(bills_dir_list)\n", " \n", " for i in range(len(sums_dir_list)):\n", " # SUMMARY\n", " summary_texts = []\n", " sum_path = sums_path + sums_dir_list[i]\n", " tree_sum = ET.parse(sum_path)\n", " root_sum = tree_sum.getroot()\n", " for description in root_sum.iter('summary-text'):\n", " temp = description.text\n", " temp = re.sub('<[^>]+>', '', temp)\n", " temp = re.sub('\\s+', ' ', temp)\n", " temp = re.sub('^[\\s]+', '', temp)\n", " temp = re.sub('[\\s]+$', '', temp)\n", " summary_texts.append(str(temp))\n", " summary = summary_texts[0]\n", " \n", " # TITLE\n", " title = \"\"\n", " for title in root_sum.iter(\"title\"):\n", " title = title.text\n", " \n", " # BILL\n", " bill_path = bills_path + bills_dir_list[i]\n", " tree_bill = ET.parse(sum_path)\n", " root_bill = tree_bill.getroot()\n", " bill = \"\"\n", " for child in root_bill.iter():\n", " temp = child.text\n", " bill = bill + temp + \" \"\n", " bill = re.sub('<[^>]+>', '', bill)\n", " bill = re.sub('\\s+', ' ', bill)\n", " bill = re.sub('^[\\s]+', '', bill)\n", " bill = re.sub('[\\s]+$', '', bill)\n", " \n", " # MERGE TO LIST OF DICTIONARIES\n", " cleaned_data.append({\"summary\" : summary, \"text\" : bill, \"title\" : title})" ] }, { "cell_type": "markdown", "id": "d70c8e1a-39e5-46af-a72d-5f979272c70f", "metadata": {}, "source": [ "# CALLING CLEAN DATA FUNCTION ON ALL DATA FOLDERS" ] }, { "cell_type": "code", "execution_count": null, "id": "7524db26-7e8e-4bd7-ac0a-36223d51abb5", "metadata": {}, "outputs": [], "source": [ "clean_data(\"./data/sums/117/\", \"./data/bills/117\")" ] }, { "cell_type": "code", "execution_count": null, "id": "f6afd2a1-88a9-4fc0-892c-0c928a1a65af", "metadata": {}, "outputs": [], "source": [ "clean_data(\"./data/sums/116/\", \"./data/bills/116\")" ] }, { "cell_type": "code", "execution_count": null, "id": "23a9ff4b-bf53-4e62-a356-909b464687bb", "metadata": {}, "outputs": [], "source": [ "clean_data(\"./data/sums/115/\", \"./data/bills/115\")" ] }, { "cell_type": "code", "execution_count": null, "id": "501757e3-ae60-494e-a9dc-f9eeee1b997f", "metadata": {}, "outputs": [], "source": [ "clean_data(\"./data/sums/114/\", \"./data/bills/114\")" ] }, { "cell_type": "code", "execution_count": null, "id": "ca133591-0acc-427a-9c5d-dd8e8a530828", "metadata": {}, "outputs": [], "source": [ "clean_data(\"./data/sums/113/\", \"./data/bills/113\")" ] }, { "cell_type": "markdown", "id": "95a2be9e-5bc6-47f8-8183-c415c8f2a5d8", "metadata": {}, "source": [ "# CHECKING FOR CORRECT LENGTH AND SPOT CHECKING DATA" ] }, { "cell_type": "code", "execution_count": null, "id": "7db890e9-86bc-49bf-a278-f6d13277f6fb", "metadata": {}, "outputs": [], "source": [ "len(cleaned_data)" ] }, { "cell_type": "code", "execution_count": null, "id": "5119d34d-fc3a-49a6-ac1a-ac2f293a6829", "metadata": {}, "outputs": [], "source": [ "cleaned_data[19]" ] }, { "cell_type": "markdown", "id": "a8fe2092-2bed-4f50-bf80-934cdb4b7cac", "metadata": {}, "source": [ "# SAVING CLEANED DATA AS JSON FILE TO TRANSFER TO TEMU" ] }, { "cell_type": "code", "execution_count": null, "id": "12605fea-ea7a-43d4-9ed0-92bd7192d879", "metadata": {}, "outputs": [], "source": [ "# Convert and write JSON object to file\n", "with open(\"cleaned_bill_sum_data.json\", \"w\") as outfile: \n", " json.dump(cleaned_data, outfile)" ] }, { "cell_type": "markdown", "id": "1770d5e7-0970-44c2-8472-1494fc6c8d9d", "metadata": {}, "source": [ "# IMPORTING CLEANED DATA JSON FILE TO A LIST OF DICTIONARIES" ] }, { "cell_type": "code", "execution_count": null, "id": "7f890296-f09d-4590-a8d8-1489b07a1a34", "metadata": {}, "outputs": [], "source": [ "# Opening JSON file\n", "with open(\"cleaned_bill_sum_data.json\") as json_file:\n", " cleaned_data = json.load(json_file)" ] }, { "cell_type": "code", "execution_count": null, "id": "2d561958-5795-446d-9104-57012635db0b", "metadata": {}, "outputs": [], "source": [ "# import datasets\n", "# from datasets import load_dataset\n", "# import pandas as pd\n", "# from datasets import Dataset" ] }, { "cell_type": "code", "execution_count": null, "id": "c3eaa971-2f84-4962-912b-b0cee8147ae3", "metadata": {}, "outputs": [], "source": [ "# data = pd.DataFrame.from_dict(cleaned_data)" ] }, { "cell_type": "code", "execution_count": null, "id": "8397087c-9a2a-4fa1-8d79-7915b8c2d13b", "metadata": {}, "outputs": [], "source": [ "# dataset = Dataset.from_pandas(pd.DataFrame(data=data))" ] }, { "cell_type": "code", "execution_count": null, "id": "91e76f1d-2602-4270-84e5-7663446c79b2", "metadata": {}, "outputs": [], "source": [ "# billsum = dataset.train_test_split(test_size=0.2)" ] }, { "cell_type": "markdown", "id": "a2e80fe5-56fa-4803-9657-76e42cd9eeab", "metadata": {}, "source": [ "# FUNCTION TO CLEAN THE TEST DATA" ] }, { "cell_type": "code", "execution_count": null, "id": "1bb9a98f-913e-42d2-8bac-a8504f8d2013", "metadata": {}, "outputs": [], "source": [ "def clean_test_data(summary_path : str, bill_path : str):\n", " sums_path = summary_path\n", " bills_path = bill_path\n", " \n", " # GETTING A LIST OF THE FILE NAMES FOR THE SUMMARY DATA FILES\n", " sums_dir_list = []\n", " for x in os.listdir(sums_path):\n", " if x.endswith(\".xml\"):\n", " sums_dir_list.append(x)\n", " sums_dir_list = sorted(sums_dir_list)\n", "\n", " # GETTING A LIST OF THE FILE NAMES FOR THE BILL DATA FILES\n", " bills_dir_list = []\n", " for x in os.listdir(bills_path):\n", " if x.endswith(\".xml\"):\n", " bills_dir_list.append(x)\n", " bills_dir_list = sorted(bills_dir_list)\n", " \n", " for i in range(len(sums_dir_list)):\n", " # SUMMARY\n", " summary_texts = []\n", " sum_path = sums_path + sums_dir_list[i]\n", " tree_sum = ET.parse(sum_path)\n", " root_sum = tree_sum.getroot()\n", " for description in root_sum.iter('summary-text'):\n", " temp = description.text\n", " temp = re.sub('<[^>]+>', '', temp)\n", " temp = re.sub('\\s+', ' ', temp)\n", " temp = re.sub('^[\\s]+', '', temp)\n", " temp = re.sub('[\\s]+$', '', temp)\n", " summary_texts.append(str(temp))\n", " summary = summary_texts[0]\n", " \n", " # TITLE\n", " title = \"\"\n", " for title in root_sum.iter(\"title\"):\n", " title = title.text\n", " \n", " # BILL\n", " bill_path = bills_path + bills_dir_list[i]\n", " tree_bill = ET.parse(sum_path)\n", " root_bill = tree_bill.getroot()\n", " bill = \"\"\n", " for child in root_bill.iter():\n", " temp = child.text\n", " bill = bill + temp + \" \"\n", " bill = re.sub('<[^>]+>', '', bill)\n", " bill = re.sub('\\s+', ' ', bill)\n", " bill = re.sub('^[\\s]+', '', bill)\n", " bill = re.sub('[\\s]+$', '', bill)\n", " \n", " # MERGE TO LIST OF DICTIONARIES\n", " cleaned_test_data.append({\"summary\" : summary, \"text\" : bill, \"title\" : title})" ] }, { "cell_type": "markdown", "id": "fe641226-2779-4de1-8066-1b65abde1a21", "metadata": {}, "source": [ "# CALLING FUNCTION TO CLEAN THE TEST DATA" ] }, { "cell_type": "code", "execution_count": null, "id": "342881ed-cd6d-4be0-9e1e-ca402d9b57af", "metadata": {}, "outputs": [], "source": [ "clean_test_data(\"./data/test/sums/\", \"./data/test/bills/\")" ] }, { "cell_type": "markdown", "id": "f276dd3a-76f0-4e19-a9d3-59ea94fae58a", "metadata": {}, "source": [ "# SHOWING A SAMPLE OF THE TEST DATA" ] }, { "cell_type": "code", "execution_count": null, "id": "ded519a6-dcd8-4399-b415-384cd609bdf0", "metadata": {}, "outputs": [], "source": [ "cleaned_test_data[11]" ] }, { "cell_type": "markdown", "id": "f3d07f33-bd77-43dc-9ea6-20a1eb1d4314", "metadata": {}, "source": [ "# SAVING CLEANED TEST DATA TO JSON" ] }, { "cell_type": "code", "execution_count": null, "id": "1fc0a1c7-47a6-4b06-a47f-de77c7cc41df", "metadata": {}, "outputs": [], "source": [ "# Convert and write JSON object to file\n", "with open(\"cleaned_bill_sum_test_data.json\", \"w\") as outfile: \n", " json.dump(cleaned_test_data, outfile)" ] }, { "cell_type": "markdown", "id": "8ed577e6-e553-4ad8-8ddc-5ec3acb7f982", "metadata": {}, "source": [ "# LOADING CLEANED TEST DATA" ] }, { "cell_type": "code", "execution_count": null, "id": "254f3c2b-4d95-4c0e-8487-43cd2ea0f4be", "metadata": {}, "outputs": [], "source": [ "# Opening JSON file\n", "with open(\"cleaned_bill_sum_test_data.json\") as json_file:\n", " cleaned_test_data = json.load(json_file)" ] }, { "cell_type": "code", "execution_count": null, "id": "34054ada-c930-478d-866b-d1a6bc1ed241", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "926c806f-a3c4-4c86-a655-949e9c33da8f", "metadata": {}, "source": [ "# SINGLE FILE CLEANING" ] }, { "cell_type": "code", "execution_count": 4, "id": "a7205466-3b57-4f31-a61e-00023e23db2e", "metadata": {}, "outputs": [], "source": [ "new_cleaned_test_data = []" ] }, { "cell_type": "code", "execution_count": 5, "id": "e28bcda9-0dc5-46ba-ac90-05096e32b331", "metadata": {}, "outputs": [], "source": [ "def single_file_cleaning(summary : str, bill : str):\n", " sum_path = summary\n", " bill_path = bill\n", " \n", " # SUMMARY\n", " summary_texts = []\n", " # sum_path = sums_path + sums_dir_list[i]\n", " tree_sum = ET.parse(sum_path)\n", " root_sum = tree_sum.getroot()\n", " for description in root_sum.iter('summary-text'):\n", " temp = description.text\n", " temp = re.sub('<[^>]+>', '', temp)\n", " temp = re.sub('\\s+', ' ', temp)\n", " temp = re.sub('^[\\s]+', '', temp)\n", " temp = re.sub('[\\s]+$', '', temp)\n", " summary_texts.append(str(temp))\n", " summary = summary_texts[0]\n", " \n", " # TITLE\n", " title = \"\"\n", " for title in root_sum.iter(\"title\"):\n", " title = title.text\n", " \n", " # BILL\n", " # bill_path = bills_path + bills_dir_list[i]\n", " tree_bill = ET.parse(sum_path)\n", " root_bill = tree_bill.getroot()\n", " bill = \"\"\n", " for child in root_bill.iter():\n", " temp = child.text\n", " bill = bill + temp + \" \"\n", " bill = re.sub('<[^>]+>', '', bill)\n", " bill = re.sub('\\s+', ' ', bill)\n", " bill = re.sub('^[\\s]+', '', bill)\n", " bill = re.sub('[\\s]+$', '', bill)\n", " \n", " # MERGE TO LIST OF DICTIONARIES\n", " new_cleaned_test_data.append({\"summary\" : summary, \"text\" : bill, \"title\" : title})" ] }, { "cell_type": "code", "execution_count": 6, "id": "ccfd78ec-3d57-4459-badb-824ca3092c4c", "metadata": {}, "outputs": [], "source": [ "single_file_cleaning(\"./BILLSUM-118s1000.xml\", \"./BILLS-118s1000is.xml\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "3ef2cd68-7f37-4025-bf06-821c49c18932", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'summary': 'Saving Access to Laboratory Services Act This bill modifies provisions relating to Medicare payment rates for clinical diagnostic laboratory services, including by requiring payment rates for certain widely available clinical diagnostic laboratory tests to be based on a statistical sampling of private sector rates.',\n", " 'text': 'Saving Access to Laboratory Services Act 2023-03-28 Introduced in Senate Saving Access to Laboratory Services Act This bill modifies provisions relating to Medicare payment rates for clinical diagnostic laboratory services, including by requiring payment rates for certain widely available clinical diagnostic laboratory tests to be based on a statistical sampling of private sector rates. text/xml EN Pursuant to Title 17 Section 105 of the United States Code, this file is not subject to copyright protection and is in the public domain. Congressional Research Service, Library of Congress This file contains bill summaries for federal legislation. A bill summary describes the most significant provisions of a piece of legislation and details the effects the legislative text may have on current law and federal programs. Bill summaries are authored by the Congressional Research Service (CRS) of the Library of Congress. As stated in Public Law 91-510 (2 USC 166 (d)(6)), one of the duties of CRS is \"to prepare summaries and digests of bills and resolutions of a public general nature introduced in the Senate or House of Representatives\". For more information, refer to the User Guide that accompanies this file.',\n", " 'title': 'Saving Access to Laboratory Services Act'}]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_cleaned_test_data" ] }, { "cell_type": "code", "execution_count": 8, "id": "510b26bf-afff-44b8-ba3e-516445d98379", "metadata": {}, "outputs": [], "source": [ "single_file_cleaning(\"./BILLSUM-118s1016.xml\", \"./BILLS-118s1016is.xml\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "1d5ceac2-0fa7-4ddf-b8d5-888ffd51b754", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'summary': 'Saving Access to Laboratory Services Act This bill modifies provisions relating to Medicare payment rates for clinical diagnostic laboratory services, including by requiring payment rates for certain widely available clinical diagnostic laboratory tests to be based on a statistical sampling of private sector rates.',\n", " 'text': 'Saving Access to Laboratory Services Act 2023-03-28 Introduced in Senate Saving Access to Laboratory Services Act This bill modifies provisions relating to Medicare payment rates for clinical diagnostic laboratory services, including by requiring payment rates for certain widely available clinical diagnostic laboratory tests to be based on a statistical sampling of private sector rates. text/xml EN Pursuant to Title 17 Section 105 of the United States Code, this file is not subject to copyright protection and is in the public domain. Congressional Research Service, Library of Congress This file contains bill summaries for federal legislation. A bill summary describes the most significant provisions of a piece of legislation and details the effects the legislative text may have on current law and federal programs. Bill summaries are authored by the Congressional Research Service (CRS) of the Library of Congress. As stated in Public Law 91-510 (2 USC 166 (d)(6)), one of the duties of CRS is \"to prepare summaries and digests of bills and resolutions of a public general nature introduced in the Senate or House of Representatives\". For more information, refer to the User Guide that accompanies this file.',\n", " 'title': 'Saving Access to Laboratory Services Act'},\n", " {'summary': 'Agriculture Resilience Act of 2023 This bill establishes, expands, and revises multiple programs and activities of the Department of Agriculture (USDA) primarily to reduce carbon emissions from the agriculture sector. Specifically, USDA must finalize and implement a plan to achieve net-zero emissions from the sector by 2040. USDA must periodically review and revise the plan, as necessary, and annually report on its implementation. Additionally, the bill expands the scope of various USDA research, extension, and education programs; conservation programs; and livestock programs to incorporate climate change adaptation and mitigation. Expanded activities include efforts to improve soil health and preserve farmland and grassland. Further, the bill changes programs that support renewable energy in rural areas to address carbon emissions in the agriculture sector. Among these changes, the bill provides statutory authority for the AgSTAR program for reducing methane emissions from livestock waste and requires the program to be moved from the Environmental Protection Agency to USDA. The bill also addresses food waste, for example, by (1) standardizing the voluntary labels used by food producers to indicate the date by which food should be used or discarded, and (2) making composting activities eligible for support through USDA conservation programs. Moreover, the bill establishes grants to reduce and prevent food waste in landfills and in schools.',\n", " 'text': 'Agriculture Resilience Act of 2023 2023-03-28 Introduced in Senate Agriculture Resilience Act of 2023 This bill establishes, expands, and revises multiple programs and activities of the Department of Agriculture (USDA) primarily to reduce carbon emissions from the agriculture sector. Specifically, USDA must finalize and implement a plan to achieve net-zero emissions from the sector by 2040. USDA must periodically review and revise the plan, as necessary, and annually report on its implementation. Additionally, the bill expands the scope of various USDA research, extension, and education programs; conservation programs; and livestock programs to incorporate climate change adaptation and mitigation. Expanded activities include efforts to improve soil health and preserve farmland and grassland. Further, the bill changes programs that support renewable energy in rural areas to address carbon emissions in the agriculture sector. Among these changes, the bill provides statutory authority for the AgSTAR program for reducing methane emissions from livestock waste and requires the program to be moved from the Environmental Protection Agency to USDA. The bill also addresses food waste, for example, by (1) standardizing the voluntary labels used by food producers to indicate the date by which food should be used or discarded, and (2) making composting activities eligible for support through USDA conservation programs. Moreover, the bill establishes grants to reduce and prevent food waste in landfills and in schools. text/xml EN Pursuant to Title 17 Section 105 of the United States Code, this file is not subject to copyright protection and is in the public domain. Congressional Research Service, Library of Congress This file contains bill summaries for federal legislation. A bill summary describes the most significant provisions of a piece of legislation and details the effects the legislative text may have on current law and federal programs. Bill summaries are authored by the Congressional Research Service (CRS) of the Library of Congress. As stated in Public Law 91-510 (2 USC 166 (d)(6)), one of the duties of CRS is \"to prepare summaries and digests of bills and resolutions of a public general nature introduced in the Senate or House of Representatives\". For more information, refer to the User Guide that accompanies this file.',\n", " 'title': 'Agriculture Resilience Act of 2023'}]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_cleaned_test_data" ] }, { "cell_type": "code", "execution_count": 10, "id": "0d1ce846-892c-4856-b45f-aeaa4d2cbc33", "metadata": {}, "outputs": [], "source": [ "# Convert and write JSON object to file\n", "with open(\"new_cleaned_bill_sum_test_data.json\", \"w\") as outfile: \n", " json.dump(new_cleaned_test_data, outfile)" ] }, { "cell_type": "code", "execution_count": null, "id": "0f8c6a5f-2c4d-4f99-ab42-e258ac13701b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }