oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
Documentation
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Community Detection for Knowledge Graphs
//!
//! This module provides community detection algorithms for identifying densely
//! connected groups of entities in knowledge graphs. Communities represent
//! semantic groups that can improve understanding and navigation of large graphs.

use anyhow::{anyhow, Result};
use scirs2_core::ndarray_ext::Array1;
use scirs2_core::random::Random;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use tracing::{debug, info};

use crate::Triple;

/// Community detection algorithm
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CommunityAlgorithm {
    /// Louvain modularity-based algorithm
    Louvain,
    /// Label propagation algorithm
    LabelPropagation,
    /// Girvan-Newman edge betweenness
    GirvanNewman,
    /// Embedding-based communities (using embeddings similarity)
    EmbeddingBased,
}

/// Community detection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommunityConfig {
    /// Algorithm to use
    pub algorithm: CommunityAlgorithm,
    /// Maximum iterations (for iterative algorithms)
    pub max_iterations: usize,
    /// Resolution parameter for Louvain
    pub resolution: f32,
    /// Minimum community size
    pub min_community_size: usize,
    /// Similarity threshold for embedding-based detection
    pub similarity_threshold: f32,
    /// Random seed
    pub random_seed: Option<u64>,
}

impl Default for CommunityConfig {
    fn default() -> Self {
        Self {
            algorithm: CommunityAlgorithm::Louvain,
            max_iterations: 100,
            resolution: 1.0,
            min_community_size: 2,
            similarity_threshold: 0.7,
            random_seed: None,
        }
    }
}

/// Community detection result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommunityResult {
    /// Community assignments (entity_id -> community_id)
    pub assignments: HashMap<String, usize>,
    /// Number of communities found
    pub num_communities: usize,
    /// Community sizes
    pub community_sizes: Vec<usize>,
    /// Modularity score (quality metric)
    pub modularity: f32,
    /// Coverage (fraction of edges within communities)
    pub coverage: f32,
    /// Community members (community_id -> set of entity_ids)
    pub communities: HashMap<usize, HashSet<String>>,
}

/// Graph structure for community detection
struct Graph {
    /// Adjacency list
    edges: HashMap<String, HashSet<String>>,
    /// Edge weights (for weighted graphs)
    weights: HashMap<(String, String), f32>,
    /// Total number of edges
    num_edges: usize,
}

impl Graph {
    fn new() -> Self {
        Self {
            edges: HashMap::new(),
            weights: HashMap::new(),
            num_edges: 0,
        }
    }

    fn add_edge(&mut self, from: &str, to: &str, weight: f32) {
        self.edges
            .entry(from.to_string())
            .or_default()
            .insert(to.to_string());

        self.edges
            .entry(to.to_string())
            .or_default()
            .insert(from.to_string());

        self.weights
            .insert((from.to_string(), to.to_string()), weight);
        self.weights
            .insert((to.to_string(), from.to_string()), weight);

        self.num_edges += 1;
    }

    fn get_neighbors(&self, node: &str) -> Option<&HashSet<String>> {
        self.edges.get(node)
    }

    fn get_weight(&self, from: &str, to: &str) -> f32 {
        self.weights
            .get(&(from.to_string(), to.to_string()))
            .copied()
            .unwrap_or(1.0)
    }

    fn degree(&self, node: &str) -> usize {
        self.edges.get(node).map(|s| s.len()).unwrap_or(0)
    }

    fn nodes(&self) -> Vec<String> {
        self.edges.keys().cloned().collect()
    }
}

/// Community detector
pub struct CommunityDetector {
    config: CommunityConfig,
    rng: Random,
}

impl CommunityDetector {
    /// Create new community detector
    pub fn new(config: CommunityConfig) -> Self {
        let rng = Random::default();

        Self { config, rng }
    }

    /// Detect communities from knowledge graph triples
    pub fn detect_from_triples(&mut self, triples: &[Triple]) -> Result<CommunityResult> {
        // Build graph from triples
        let mut graph = Graph::new();

        for triple in triples {
            // Add edge from subject to object (undirected)
            graph.add_edge(&triple.subject.to_string(), &triple.object.to_string(), 1.0);
        }

        info!(
            "Detecting communities in graph with {} nodes and {} edges using {:?}",
            graph.nodes().len(),
            graph.num_edges,
            self.config.algorithm
        );

        self.detect_from_graph(&graph)
    }

    /// Detect communities from entity embeddings
    pub fn detect_from_embeddings(
        &mut self,
        embeddings: &HashMap<String, Array1<f32>>,
    ) -> Result<CommunityResult> {
        info!("Detecting communities from {} embeddings", embeddings.len());

        match self.config.algorithm {
            CommunityAlgorithm::EmbeddingBased => self.embedding_based_detection(embeddings),
            _ => {
                // Build similarity graph
                let graph = self.build_similarity_graph(embeddings);
                self.detect_from_graph(&graph)
            }
        }
    }

    /// Detect communities from graph structure
    fn detect_from_graph(&mut self, graph: &Graph) -> Result<CommunityResult> {
        match self.config.algorithm {
            CommunityAlgorithm::Louvain => self.louvain_detection(graph),
            CommunityAlgorithm::LabelPropagation => self.label_propagation(graph),
            CommunityAlgorithm::GirvanNewman => self.girvan_newman(graph),
            CommunityAlgorithm::EmbeddingBased => {
                Err(anyhow!("Embedding-based detection requires embeddings"))
            }
        }
    }

    /// Louvain modularity optimization
    fn louvain_detection(&mut self, graph: &Graph) -> Result<CommunityResult> {
        let nodes = graph.nodes();
        let m = graph.num_edges as f32;

        // Initialize: each node in its own community
        let mut community: HashMap<String, usize> = nodes
            .iter()
            .enumerate()
            .map(|(i, node)| (node.clone(), i))
            .collect();

        let mut improved = true;
        let mut iteration = 0;

        while improved && iteration < self.config.max_iterations {
            improved = false;
            iteration += 1;

            // For each node, try moving to neighbor's community
            for node in &nodes {
                let current_comm = community[node];
                let best_comm = self.find_best_community(node, current_comm, &community, graph, m);

                if best_comm != current_comm {
                    community.insert(node.clone(), best_comm);
                    improved = true;
                }
            }

            debug!("Louvain iteration {}: improved = {}", iteration, improved);
        }

        self.create_result(&community, graph)
    }

    /// Find best community for a node (max modularity gain)
    fn find_best_community(
        &self,
        node: &str,
        current_comm: usize,
        community: &HashMap<String, usize>,
        graph: &Graph,
        m: f32,
    ) -> usize {
        let neighbors = match graph.get_neighbors(node) {
            Some(n) => n,
            None => return current_comm,
        };

        // Get neighboring communities
        let mut neighbor_comms: HashSet<usize> = HashSet::new();
        for neighbor in neighbors {
            if let Some(&comm) = community.get(neighbor) {
                neighbor_comms.insert(comm);
            }
        }

        // Compute modularity gain for each community
        let current_modularity =
            self.compute_modularity_contribution(node, current_comm, community, graph, m);

        let mut best_comm = current_comm;
        let mut best_modularity = current_modularity;

        for &comm in &neighbor_comms {
            if comm == current_comm {
                continue;
            }

            let modularity = self.compute_modularity_contribution(node, comm, community, graph, m);

            if modularity > best_modularity {
                best_modularity = modularity;
                best_comm = comm;
            }
        }

        best_comm
    }

    /// Compute modularity contribution for a node in a community
    fn compute_modularity_contribution(
        &self,
        node: &str,
        comm: usize,
        community: &HashMap<String, usize>,
        graph: &Graph,
        m: f32,
    ) -> f32 {
        let neighbors = match graph.get_neighbors(node) {
            Some(n) => n,
            None => return 0.0,
        };

        let k_i = graph.degree(node) as f32;

        // Sum of weights to nodes in community
        let mut e_ic = 0.0;
        let mut k_c = 0.0;

        for neighbor in neighbors {
            if let Some(&neighbor_comm) = community.get(neighbor) {
                if neighbor_comm == comm {
                    e_ic += graph.get_weight(node, neighbor);
                    k_c += graph.degree(neighbor) as f32;
                }
            }
        }

        (e_ic - (self.config.resolution * k_i * k_c) / (2.0 * m)) / m
    }

    /// Label propagation algorithm
    fn label_propagation(&mut self, graph: &Graph) -> Result<CommunityResult> {
        let nodes = graph.nodes();

        // Initialize: each node with unique label
        let mut labels: HashMap<String, usize> = nodes
            .iter()
            .enumerate()
            .map(|(i, node)| (node.clone(), i))
            .collect();

        for iteration in 0..self.config.max_iterations {
            let mut changed = false;

            // Randomize node order
            let mut node_order = nodes.clone();
            for i in (1..node_order.len()).rev() {
                let j = self.rng.random_range(0..i + 1);
                node_order.swap(i, j);
            }

            // Update labels
            for node in &node_order {
                let old_label = labels[node];
                let new_label = self.majority_label(node, &labels, graph);

                if new_label != old_label {
                    labels.insert(node.clone(), new_label);
                    changed = true;
                }
            }

            debug!(
                "Label propagation iteration {}: changed = {}",
                iteration + 1,
                changed
            );

            if !changed {
                info!("Label propagation converged at iteration {}", iteration + 1);
                break;
            }
        }

        self.create_result(&labels, graph)
    }

    /// Get majority label from neighbors
    fn majority_label(&self, node: &str, labels: &HashMap<String, usize>, graph: &Graph) -> usize {
        let neighbors = match graph.get_neighbors(node) {
            Some(n) => n,
            None => return labels[node],
        };

        let mut label_counts: HashMap<usize, usize> = HashMap::new();

        for neighbor in neighbors {
            if let Some(&label) = labels.get(neighbor) {
                *label_counts.entry(label).or_insert(0) += 1;
            }
        }

        // Return most frequent label
        label_counts
            .into_iter()
            .max_by_key(|(_, count)| *count)
            .map(|(label, _)| label)
            .unwrap_or_else(|| labels[node])
    }

    /// Girvan-Newman edge betweenness clustering
    fn girvan_newman(&mut self, graph: &Graph) -> Result<CommunityResult> {
        // Simplified implementation: iteratively remove highest betweenness edges
        // Full implementation would require connected components analysis

        let nodes = graph.nodes();
        let mut assignments: HashMap<String, usize> = HashMap::new();

        // Use BFS to identify connected components
        let mut visited = HashSet::new();
        let mut community_id = 0;

        for node in &nodes {
            if visited.contains(node) {
                continue;
            }

            // BFS from this node
            let component = self.bfs_component(node, graph, &visited);

            for comp_node in &component {
                assignments.insert(comp_node.clone(), community_id);
                visited.insert(comp_node.clone());
            }

            community_id += 1;
        }

        self.create_result(&assignments, graph)
    }

    /// BFS to find connected component
    fn bfs_component(
        &self,
        start: &str,
        graph: &Graph,
        visited: &HashSet<String>,
    ) -> HashSet<String> {
        let mut component = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(start.to_string());
        component.insert(start.to_string());

        while let Some(node) = queue.pop_front() {
            if let Some(neighbors) = graph.get_neighbors(&node) {
                for neighbor in neighbors {
                    if !visited.contains(neighbor) && !component.contains(neighbor) {
                        component.insert(neighbor.clone());
                        queue.push_back(neighbor.clone());
                    }
                }
            }
        }

        component
    }

    /// Embedding-based community detection
    fn embedding_based_detection(
        &mut self,
        embeddings: &HashMap<String, Array1<f32>>,
    ) -> Result<CommunityResult> {
        let entity_list: Vec<String> = embeddings.keys().cloned().collect();
        let mut assignments: HashMap<String, usize> = HashMap::new();
        let mut community_id = 0;

        let mut unassigned: HashSet<String> = entity_list.iter().cloned().collect();

        while !unassigned.is_empty() {
            // Pick random seed
            let seed = unassigned
                .iter()
                .next()
                .expect("unassigned should not be empty")
                .clone();
            let mut community = HashSet::new();
            community.insert(seed.clone());
            unassigned.remove(&seed);

            // Grow community by similarity
            let mut changed = true;
            while changed {
                changed = false;

                for entity in &entity_list {
                    if community.contains(entity) || !unassigned.contains(entity) {
                        continue;
                    }

                    // Check similarity to community members
                    let avg_similarity =
                        self.average_similarity_to_community(entity, &community, embeddings);

                    if avg_similarity >= self.config.similarity_threshold {
                        community.insert(entity.clone());
                        unassigned.remove(entity);
                        changed = true;
                    }
                }
            }

            // Assign community
            if community.len() >= self.config.min_community_size {
                for member in community {
                    assignments.insert(member, community_id);
                }
                community_id += 1;
            } else {
                // Assign to noise/outlier community
                for member in community {
                    assignments.insert(member, usize::MAX);
                }
            }
        }

        // Build dummy graph for result creation
        let mut graph = Graph::new();
        for entity in &entity_list {
            graph.edges.insert(entity.clone(), HashSet::new());
        }

        self.create_result(&assignments, &graph)
    }

    /// Compute average similarity to community members
    fn average_similarity_to_community(
        &self,
        entity: &str,
        community: &HashSet<String>,
        embeddings: &HashMap<String, Array1<f32>>,
    ) -> f32 {
        if community.is_empty() {
            return 0.0;
        }

        let entity_emb = &embeddings[entity];

        let total_sim: f32 = community
            .iter()
            .map(|member| {
                let member_emb = &embeddings[member];
                self.cosine_similarity(entity_emb, member_emb)
            })
            .sum();

        total_sim / community.len() as f32
    }

    /// Cosine similarity
    fn cosine_similarity(&self, a: &Array1<f32>, b: &Array1<f32>) -> f32 {
        let dot = a.dot(b);
        let norm_a = a.dot(a).sqrt();
        let norm_b = b.dot(b).sqrt();

        if norm_a == 0.0 || norm_b == 0.0 {
            0.0
        } else {
            dot / (norm_a * norm_b)
        }
    }

    /// Build similarity graph from embeddings
    fn build_similarity_graph(&self, embeddings: &HashMap<String, Array1<f32>>) -> Graph {
        let mut graph = Graph::new();
        let entity_list: Vec<String> = embeddings.keys().cloned().collect();

        for i in 0..entity_list.len() {
            for j in (i + 1)..entity_list.len() {
                let sim = self
                    .cosine_similarity(&embeddings[&entity_list[i]], &embeddings[&entity_list[j]]);

                if sim >= self.config.similarity_threshold {
                    graph.add_edge(&entity_list[i], &entity_list[j], sim);
                }
            }
        }

        graph
    }

    /// Create community result from assignments
    fn create_result(
        &self,
        assignments: &HashMap<String, usize>,
        graph: &Graph,
    ) -> Result<CommunityResult> {
        // Compute community sizes
        let mut community_sizes: HashMap<usize, usize> = HashMap::new();
        let mut communities: HashMap<usize, HashSet<String>> = HashMap::new();

        for (entity, &comm) in assignments {
            if comm != usize::MAX {
                *community_sizes.entry(comm).or_insert(0) += 1;
                communities.entry(comm).or_default().insert(entity.clone());
            }
        }

        let num_communities = community_sizes.len();
        let sizes: Vec<usize> = (0..num_communities)
            .map(|i| community_sizes.get(&i).copied().unwrap_or(0))
            .collect();

        // Compute modularity
        let modularity = self.compute_modularity(assignments, graph);

        // Compute coverage
        let coverage = self.compute_coverage(assignments, graph);

        Ok(CommunityResult {
            assignments: assignments.clone(),
            num_communities,
            community_sizes: sizes,
            modularity,
            coverage,
            communities,
        })
    }

    /// Compute overall modularity
    fn compute_modularity(&self, assignments: &HashMap<String, usize>, graph: &Graph) -> f32 {
        let m = graph.num_edges as f32;
        if m == 0.0 {
            return 0.0;
        }

        let nodes = graph.nodes();
        let mut modularity = 0.0;

        for node_i in &nodes {
            for node_j in &nodes {
                if let (Some(&comm_i), Some(&comm_j)) =
                    (assignments.get(node_i), assignments.get(node_j))
                {
                    if comm_i == comm_j && comm_i != usize::MAX {
                        let a_ij = if graph
                            .get_neighbors(node_i)
                            .map(|n| n.contains(node_j))
                            .unwrap_or(false)
                        {
                            1.0
                        } else {
                            0.0
                        };

                        let k_i = graph.degree(node_i) as f32;
                        let k_j = graph.degree(node_j) as f32;

                        modularity += a_ij - (k_i * k_j) / (2.0 * m);
                    }
                }
            }
        }

        modularity / (2.0 * m)
    }

    /// Compute coverage (fraction of edges within communities)
    fn compute_coverage(&self, assignments: &HashMap<String, usize>, graph: &Graph) -> f32 {
        if graph.num_edges == 0 {
            return 0.0;
        }

        let mut internal_edges = 0;

        for (node, neighbors) in &graph.edges {
            if let Some(&comm) = assignments.get(node) {
                if comm == usize::MAX {
                    continue;
                }

                for neighbor in neighbors {
                    if let Some(&neighbor_comm) = assignments.get(neighbor) {
                        if comm == neighbor_comm {
                            internal_edges += 1;
                        }
                    }
                }
            }
        }

        // Each edge is counted twice
        (internal_edges / 2) as f32 / graph.num_edges as f32
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::NamedNode;
    use scirs2_core::ndarray_ext::array;

    #[test]
    fn test_community_detection_from_triples() {
        let triples = vec![
            Triple::new(
                NamedNode::new("a").expect("should succeed"),
                NamedNode::new("r").expect("should succeed"),
                NamedNode::new("b").expect("should succeed"),
            ),
            Triple::new(
                NamedNode::new("b").expect("should succeed"),
                NamedNode::new("r").expect("should succeed"),
                NamedNode::new("c").expect("should succeed"),
            ),
            Triple::new(
                NamedNode::new("d").expect("should succeed"),
                NamedNode::new("r").expect("should succeed"),
                NamedNode::new("e").expect("should succeed"),
            ),
        ];

        let config = CommunityConfig::default();
        let mut detector = CommunityDetector::new(config);
        let result = detector
            .detect_from_triples(&triples)
            .expect("should succeed");

        assert!(result.num_communities > 0);
        assert_eq!(result.assignments.len(), 5); // a, b, c, d, e
    }

    #[test]
    fn test_embedding_based_detection() {
        let mut embeddings = HashMap::new();
        embeddings.insert("e1".to_string(), array![1.0, 0.0]);
        embeddings.insert("e2".to_string(), array![0.9, 0.1]);
        embeddings.insert("e3".to_string(), array![0.0, 1.0]);
        embeddings.insert("e4".to_string(), array![0.1, 0.9]);

        let config = CommunityConfig {
            algorithm: CommunityAlgorithm::EmbeddingBased,
            similarity_threshold: 0.8,
            ..Default::default()
        };

        let mut detector = CommunityDetector::new(config);
        let result = detector
            .detect_from_embeddings(&embeddings)
            .expect("should succeed");

        assert!(result.num_communities >= 1);
        // Similar embeddings should be in same community
        assert_eq!(result.assignments.get("e1"), result.assignments.get("e2"));
    }
}