File size: 1,849 Bytes
6dad1de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import List, Dict, Any
from tinytroupe.social_network import NetworkTopology, Community

class NetworkAnalyzer:
    """
    Provide analytics for understanding network dynamics.
    """
    
    @staticmethod
    def calculate_centrality_metrics(network: NetworkTopology) -> Dict[str, Dict[str, float]]:
        """
        Calculates various centrality metrics for each persona in the network.
        """
        # Simplistic implementation without NetworkX for now to avoid dependency issues in basic environment
        metrics = {name: {"degree": 0.0} for name in network.nodes}
        for edge in network.edges:
            metrics[edge.source_id]["degree"] += 1
            metrics[edge.target_id]["degree"] += 1
            
        n = max(len(network.nodes), 1)
        for name in metrics:
            metrics[name]["degree"] /= n
            
        return metrics

    @staticmethod
    def detect_communities(network: NetworkTopology) -> List[Community]:
        """
        Detects communities within the social network.
        """
        # Placeholder for community detection logic
        return network.communities

    @staticmethod
    def identify_key_influencers(network: NetworkTopology, top_k: int = 10) -> List[str]:
        """
        Identifies the top K influencers in the network.
        """
        metrics = NetworkAnalyzer.calculate_centrality_metrics(network)
        sorted_influencers = sorted(metrics.keys(), key=lambda x: metrics[x]["degree"], reverse=True)
        return sorted_influencers[:top_k]

    @staticmethod
    def calculate_density(network: NetworkTopology) -> float:
        """
        Calculates the density of the network.
        """
        n = len(network.nodes)
        if n < 2: return 0.0
        max_edges = n * (n - 1) / 2
        return len(network.edges) / max_edges