@@ -75,7 +75,13 @@ impl Communities {
7575 self . entities
7676 . iter ( )
7777 . zip ( self . labels . iter ( ) )
78- . filter_map ( |( e, & c) | if c == community { Some ( e. as_str ( ) ) } else { None } )
78+ . filter_map ( |( e, & c) | {
79+ if c == community {
80+ Some ( e. as_str ( ) )
81+ } else {
82+ None
83+ }
84+ } )
7985 . collect ( )
8086 }
8187}
@@ -111,27 +117,32 @@ impl TripletGraph {
111117 s. dedup ( ) ;
112118 s. len ( )
113119 } ;
114- Communities { entities, labels, levels, modularity : q, num_communities }
120+ Communities {
121+ entities,
122+ labels,
123+ levels,
124+ modularity : q,
125+ num_communities,
126+ }
115127 }
116128
117129 /// Dense entity index: a stable `Vec<String>` (sorted for determinism) and
118130 /// an entity → dense-id map.
119131 fn entity_index_dense ( & self ) -> ( Vec < String > , HashMap < String , usize > ) {
120132 let mut names: Vec < String > = self . entity_index . keys ( ) . cloned ( ) . collect ( ) ;
121133 names. sort_unstable ( ) ;
122- let index: HashMap < String , usize > =
123- names. iter ( ) . enumerate ( ) . map ( |( i, e) | ( e. clone ( ) , i) ) . collect ( ) ;
134+ let index: HashMap < String , usize > = names
135+ . iter ( )
136+ . enumerate ( )
137+ . map ( |( i, e) | ( e. clone ( ) , i) )
138+ . collect ( ) ;
124139 ( names, index)
125140 }
126141
127142 /// Weighted undirected adjacency (both endpoints), weight = summed NARS
128143 /// confidence over the triplets joining two entities. Deleted triplets and
129144 /// self-loops are skipped.
130- fn build_adjacency (
131- & self ,
132- index : & HashMap < String , usize > ,
133- n : usize ,
134- ) -> Vec < Vec < ( usize , f64 ) > > {
145+ fn build_adjacency ( & self , index : & HashMap < String , usize > , n : usize ) -> Vec < Vec < ( usize , f64 ) > > {
135146 // Accumulate undirected weights in a per-node BTreeMap for determinism.
136147 let mut acc: Vec < BTreeMap < usize , f64 > > = vec ! [ BTreeMap :: new( ) ; n] ;
137148 for t in & self . triplets {
@@ -176,7 +187,12 @@ fn build(adj: Vec<Vec<(usize, f64)>>, self_loop: Vec<f64>) -> WGraph {
176187 degree[ u] = d + 2.0 * self_loop[ u] ;
177188 }
178189 let two_m: f64 = degree. iter ( ) . sum ( ) ;
179- WGraph { adj, self_loop, degree, two_m }
190+ WGraph {
191+ adj,
192+ self_loop,
193+ degree,
194+ two_m,
195+ }
180196}
181197
182198/// First-appearance dense relabel — deterministic given node order.
@@ -267,8 +283,10 @@ fn aggregate(g: &WGraph, label: &[usize]) -> WGraph {
267283 }
268284 }
269285 }
270- let adj: Vec < Vec < ( usize , f64 ) > > =
271- super_adj. into_iter ( ) . map ( |m| m. into_iter ( ) . collect ( ) ) . collect ( ) ;
286+ let adj: Vec < Vec < ( usize , f64 ) > > = super_adj
287+ . into_iter ( )
288+ . map ( |m| m. into_iter ( ) . collect ( ) )
289+ . collect ( ) ;
272290 build ( adj, self_w)
273291}
274292
@@ -298,10 +316,7 @@ fn modularity(g: &WGraph, label: &[usize]) -> f64 {
298316
299317/// Multi-level Louvain. Returns (hierarchy over ORIGINAL nodes, coarsest
300318/// labels, `Q` of the coarsest partition on the original graph).
301- fn detect (
302- adj0 : Vec < Vec < ( usize , f64 ) > > ,
303- self0 : Vec < f64 > ,
304- ) -> ( Vec < Vec < usize > > , Vec < usize > , f64 ) {
319+ fn detect ( adj0 : Vec < Vec < ( usize , f64 ) > > , self0 : Vec < f64 > ) -> ( Vec < Vec < usize > > , Vec < usize > , f64 ) {
305320 let n0 = adj0. len ( ) ;
306321 let g0 = build ( adj0. clone ( ) , self0. clone ( ) ) ;
307322 let mut g = build ( adj0, self0) ;
@@ -345,8 +360,12 @@ mod tests {
345360 fn two_triangles_bridge_yields_two_communities ( ) {
346361 // {a,b,c} triangle, {d,e,f} triangle, single a-d bridge.
347362 let g = tg ( & [
348- ( "a" , "b" ) , ( "b" , "c" ) , ( "c" , "a" ) ,
349- ( "d" , "e" ) , ( "e" , "f" ) , ( "f" , "d" ) ,
363+ ( "a" , "b" ) ,
364+ ( "b" , "c" ) ,
365+ ( "c" , "a" ) ,
366+ ( "d" , "e" ) ,
367+ ( "e" , "f" ) ,
368+ ( "f" , "d" ) ,
350369 ( "a" , "d" ) ,
351370 ] ) ;
352371 let c = g. communities ( ) ;
@@ -360,13 +379,28 @@ mod tests {
360379
361380 #[ test]
362381 fn deterministic ( ) {
363- let g = tg ( & [ ( "a" , "b" ) , ( "b" , "c" ) , ( "c" , "a" ) , ( "d" , "e" ) , ( "e" , "f" ) , ( "f" , "d" ) , ( "a" , "d" ) ] ) ;
382+ let g = tg ( & [
383+ ( "a" , "b" ) ,
384+ ( "b" , "c" ) ,
385+ ( "c" , "a" ) ,
386+ ( "d" , "e" ) ,
387+ ( "e" , "f" ) ,
388+ ( "f" , "d" ) ,
389+ ( "a" , "d" ) ,
390+ ] ) ;
364391 assert_eq ! ( g. communities( ) . labels, g. communities( ) . labels) ;
365392 }
366393
367394 #[ test]
368395 fn clique_is_one_community ( ) {
369- let g = tg ( & [ ( "a" , "b" ) , ( "a" , "c" ) , ( "a" , "d" ) , ( "b" , "c" ) , ( "b" , "d" ) , ( "c" , "d" ) ] ) ;
396+ let g = tg ( & [
397+ ( "a" , "b" ) ,
398+ ( "a" , "c" ) ,
399+ ( "a" , "d" ) ,
400+ ( "b" , "c" ) ,
401+ ( "b" , "d" ) ,
402+ ( "c" , "d" ) ,
403+ ] ) ;
370404 assert_eq ! ( g. communities( ) . num_communities, 1 ) ;
371405 }
372406
@@ -385,7 +419,13 @@ mod tests {
385419 Triplet :: new( "a" , "b" , "rel" , 0 ) ,
386420 Triplet :: new( "b" , "c" , "rel" , 1 ) ,
387421 Triplet :: new( "c" , "a" , "rel" , 2 ) ,
388- Triplet :: with_truth( "c" , "d" , "rel" , crate :: graph:: spo:: truth:: TruthValue :: new( 1.0 , 0.05 ) , 3 ) ,
422+ Triplet :: with_truth(
423+ "c" ,
424+ "d" ,
425+ "rel" ,
426+ crate :: graph:: spo:: truth:: TruthValue :: new( 1.0 , 0.05 ) ,
427+ 3 ,
428+ ) ,
389429 ] ;
390430 g. add_triplets ( & ts) ;
391431 let c = g. communities ( ) ;
0 commit comments