sanjoy19's picture
Update utils.py
332bece verified
Raw History Blame Contribute Delete
1.09 kB
import re
# =====================================================
# Convert Contract ID to Filesystem-Safe Format
# =====================================================
def safe_contract_id(contract_id: str) -> str:
"""
Convert a contract ID to a filesystem-safe directory name.
"""
contract_id = re.sub(r'[<>:"/\\|?*#]', "_", contract_id)
contract_id = re.sub(r"\s+", " ", contract_id).strip()
return contract_id
# =====================================================
# Extract Contract ID
# =====================================================
def get_contract_id(sample_id: str) -> str:
"""
Extract the contract ID from a CUAD sample ID and
return its filesystem-safe version.
"""
return safe_contract_id(sample_id.split("__", 1)[0])
# =====================================================
# Get Contract ID for Legal Dataset
# =====================================================
def get_contract_id_if_needed(
config, contract_id
):
if config.domain != "legal":
return None
return get_contract_id(contract_id)