|
4 | 4 | import json |
5 | 5 | import logging |
6 | 6 | from pathlib import Path |
7 | | -from typing import Optional, Union, List, Set, Dict |
| 7 | +from typing import Optional, Union, List, Set, Dict, Any |
8 | 8 | from ..models.types import Study |
9 | 9 | from bs4 import BeautifulSoup, Comment |
10 | 10 |
|
@@ -244,3 +244,112 @@ def _clean_html_with_readability(html: str) -> str: |
244 | 244 | # If any error occurs, fall back to safe cleaning |
245 | 245 | logging.warning(f"Error using readabilipy, falling back to basic HTML cleaning: {e}") |
246 | 246 | return _safe_clean_html(html) |
| 247 | + |
| 248 | +def _load_activation_tables_from_csv( |
| 249 | + table_source: str, |
| 250 | + root_path: str, |
| 251 | + pmcids_to_include: Optional[Set[str]] = None |
| 252 | +) -> Dict[str, List[Dict[str, Any]]]: |
| 253 | + """ |
| 254 | + Load activation tables from a CSV file and map them to PMCIDs. |
| 255 | + |
| 256 | + Args: |
| 257 | + table_source: Path to the table.csv file |
| 258 | + root_path: Root path for resolving relative paths in table_raw_file |
| 259 | + pmcids_to_include: Optional set of PMCIDs to filter for |
| 260 | + |
| 261 | + Returns: |
| 262 | + Dictionary mapping PMCIDs to lists of table metadata dictionaries |
| 263 | + """ |
| 264 | + try: |
| 265 | + # Read the CSV file |
| 266 | + df = pd.read_csv(table_source) |
| 267 | + |
| 268 | + # Required columns |
| 269 | + required_columns = [ |
| 270 | + 'pmcid', 'table_id', 'table_label', 'table_caption', |
| 271 | + 'table_foot', 'table_raw_file' |
| 272 | + ] |
| 273 | + |
| 274 | + # Check if all required columns are present |
| 275 | + missing_columns = [ |
| 276 | + col for col in required_columns if col not in df.columns |
| 277 | + ] |
| 278 | + if missing_columns: |
| 279 | + raise ValueError( |
| 280 | + "Missing required columns in table source CSV: " |
| 281 | + f"{missing_columns}" |
| 282 | + ) |
| 283 | + |
| 284 | + # Create a mapping of pmcid to table metadata |
| 285 | + pmcid_to_tables = {} |
| 286 | + |
| 287 | + for _, row in df.iterrows(): |
| 288 | + pmcid = str(row['pmcid']) |
| 289 | + |
| 290 | + # Skip if filtering and this PMCID is not included |
| 291 | + if (pmcids_to_include is not None and |
| 292 | + pmcid not in pmcids_to_include): |
| 293 | + continue |
| 294 | + |
| 295 | + # Create absolute path for table_raw_file |
| 296 | + table_raw_file = row['table_raw_file'] |
| 297 | + if table_raw_file: |
| 298 | + # Resolve relative path against root_path |
| 299 | + table_path = str(Path(root_path) / table_raw_file) |
| 300 | + else: |
| 301 | + table_path = None |
| 302 | + |
| 303 | + # Create table metadata dictionary |
| 304 | + table_metadata = { |
| 305 | + 'table_id': str(row['table_id']), |
| 306 | + 'table_label': str(row['table_label']), |
| 307 | + 'table_path': table_path, |
| 308 | + 'table_caption': ( |
| 309 | + row['table_caption'] if pd.notna(row['table_caption']) |
| 310 | + else None |
| 311 | + ), |
| 312 | + 'table_foot': ( |
| 313 | + row['table_foot'] if pd.notna(row['table_foot']) |
| 314 | + else None |
| 315 | + ) |
| 316 | + } |
| 317 | + |
| 318 | + # Add to mapping |
| 319 | + if pmcid not in pmcid_to_tables: |
| 320 | + pmcid_to_tables[pmcid] = [] |
| 321 | + pmcid_to_tables[pmcid].append(table_metadata) |
| 322 | + |
| 323 | + return pmcid_to_tables |
| 324 | + |
| 325 | + except Exception as e: |
| 326 | + logging.warning(f"Failed to load activation tables from CSV: {e}") |
| 327 | + return {} |
| 328 | + |
| 329 | + |
| 330 | +def _map_pmcids_to_activation_tables( |
| 331 | + full_text_config: Dict[str, Any], |
| 332 | + pmcids_to_include: Optional[Set[str]] = None |
| 333 | +) -> Dict[str, List[Dict[str, Any]]]: |
| 334 | + """ |
| 335 | + Map PMCIDs to activation tables from user-provided table sources. |
| 336 | + |
| 337 | + Args: |
| 338 | + full_text_config: Configuration dictionary for a full text source |
| 339 | + pmcids_to_include: Optional set of PMCIDs to filter for |
| 340 | + |
| 341 | + Returns: |
| 342 | + Dictionary mapping PMCIDs to lists of table metadata dictionaries |
| 343 | + """ |
| 344 | + # Check if table_source is specified in the configuration |
| 345 | + table_source = full_text_config.get('table_source') |
| 346 | + if not table_source: |
| 347 | + return {} |
| 348 | + |
| 349 | + # Get root path from the configuration |
| 350 | + root_path = full_text_config.get('root_path', '.') |
| 351 | + |
| 352 | + # Load activation tables from CSV |
| 353 | + return _load_activation_tables_from_csv( |
| 354 | + table_source, root_path, pmcids_to_include |
| 355 | + ) |
0 commit comments