Senate Hearings is a comprehensive Python package for scraping, extracting, and analyzing congressional hearing data from the govinfo website. It combines web scraping with natural language processing techniques to automatically identify, retrieve, and process hearing transcripts and metadata.
The package provides a complete pipeline for:
Web Scraping and Content Extraction: Dynamically retrieves fully expanded HTML content from govinfo pages using Selenium, handling JavaScript-rendered content and interactive elements.
Transcript Parsing: Extracts and cleans raw congressional hearing transcripts, removing formatting artifacts, metadata, and structural noise to produce readable narrative text.
Metadata Extraction: Automatically extracts key metadata from hearing documents including titles, dates, categories, and session information.
Keyword Extraction and Normalization: Implements advanced keyword extraction using Maximal Marginal Relevance (MMR) to identify semantically relevant and diverse keywords from hearing content. Keywords are filtered, normalized, and consolidated for consistency.
Data Management: Processes and manages large collections of hearing data, supporting CSV I/O operations and structured data organization.
Key Features:
Robust Web Scraping: Handles dynamic content, timeouts, and page interactions gracefully
Intelligent Keyword Filtering: Removes banned terms and person names automatically
Semantic Keyword Selection: Uses embeddings and MMR to select diverse, relevant keywords
Text Normalization: Converts variant forms (abbreviations, capitalization) to canonical representations
Scalable Data Processing: Processes congressional session data efficiently in batch operations
The package is ideal for researchers, data scientists, and analysts working with legislative records, policy analysis, and congressional hearing data.
Functions
is_banned_candidate()
Description:
Determines whether a candidate keyword phrase should be excluded from further consideration.
A candidate is considered banned if it contains:
Any substring from a predefined list of banned terms, or
Any person name from a provided set
All checks are performed in a case-insensitive manner. This function is typically used early in the pipeline to remove inappropriate or unwanted keyword phrases.
Parameters:
c (str):
Candidate keyword phrase to evaluate.
banned_substrings (list[str]):
List of substrings that should not appear in valid keywords.
person_names (set[str]):
Set of person names that should be excluded if found in a candidate.
Returns:
bool
Returns True if the candidate should be banned, otherwise False.
Examples:
from senate_hearings.keybert_funcs import*is_banned_candidate( c="john doe analysis", banned_substrings=["spam"], person_names={"john doe"})# Trueis_banned_candidate( c="machine learning", banned_substrings=["spam"], person_names=set())# False
False
mmr_select()
Description:
Selects a subset of candidate keywords using Maximal Marginal Relevance (MMR).
MMR balances:
Relevance of each candidate to the document, and diversity relative to keywords that have already been selected.
This approach helps avoid redundant keywords while still prioritizing semantic relevance. Similarity calculations are based on vector dot products (cosine similarity when embeddings are normalized).
Parameters:
doc_emb (numpy.ndarray):
Embedding vector representing the document.
cand_embs (numpy.ndarray):
2D array of candidate keyword embeddings with shape (n_candidates, embedding_dim).
candidates (list[str]):
Candidate keyword phrases corresponding to the embedding rows.
top_n (int, default = 10):
Number of keywords to select.
lambda_param (float, default = 0.7):
Trade-off parameter between relevance and diversity:
Values closer to 1 emphasize relevance
Values closer to 0 emphasize diversity
Returns:
list[str]
List of selected keyword phrases in selection order.
Trims a list of candidate keyword phrases based on their overall frequency in a corpus.
Candidate phrases are ranked by total frequency across all documents, and only the most frequent phrases are retained. This is commonly used to reduce the candidate set before embedding or MMR selection.
Parameters:
candidates (list[str]):
Candidate keyword phrases.
X (scipy.sparse matrix):
Document-term matrix (e.g., output of CountVectorizer). Columns correspond to candidates.
max_candidates (int):
Maximum number of candidates to retain.
Returns:
list[str]
Trimmed list of candidate phrases sorted by descending frequency.