fetchAll( "SELECT DISTINCT source_section FROM entity_relationships ORDER BY source_section" ); $section = $_GET['section'] ?? ''; $section = Security::sanitize($section); $entityType = $_GET['type'] ?? 'PERSON'; $entityType = in_array($entityType, ['PERSON', 'ORG']) ? $entityType : 'PERSON'; $minCount = max(2, (int)($_GET['min'] ?? 10)); $relationships = []; $nodes = []; if ($section) { // Get top relationships for this section $relationships = $db->fetchAll( "SELECT entity_a, entity_b, co_occurrence_count, document_count, sample_doc_ids FROM entity_relationships WHERE source_section = :section AND entity_a_type = :type AND co_occurrence_count >= :minCount ORDER BY co_occurrence_count DESC LIMIT 200", ['section' => $section, 'type' => $entityType, 'minCount' => $minCount] ); // Build node set with degree counts $nodeDegree = []; foreach ($relationships as $rel) { $a = $rel['entity_a']; $b = $rel['entity_b']; $nodeDegree[$a] = ($nodeDegree[$a] ?? 0) + (int)$rel['co_occurrence_count']; $nodeDegree[$b] = ($nodeDegree[$b] ?? 0) + (int)$rel['co_occurrence_count']; } arsort($nodeDegree); $nodes = $nodeDegree; } // Stats $stats = $db->fetchAll( "SELECT entity_a_type as type, source_section as section, COUNT(*) as rels, SUM(co_occurrence_count) as cooccurrences FROM entity_relationships GROUP BY entity_a_type, source_section ORDER BY rels DESC" ); require __DIR__ . '/../views/network.php'; } }