@@ -2,7 +2,7 @@ import { clone } from '@antv/util';
22import getAdjMatrix from './adjacent-matrix' ;
33import { NodeConfig , ClusterData , GraphData , ClusterMap } from './types' ;
44import Vector from './utils/vector' ;
5- import { secondReg , dateReg } from './constants/time ' ;
5+ import { getPropertyWeight } from './utils/node-properties ' ;
66
77const getModularity = (
88 nodes : NodeConfig [ ] ,
@@ -28,59 +28,6 @@ const getModularity = (
2828 return modularity ;
2929}
3030
31- // 获取所有属性并排序
32- const getAllSortProperties = ( nodes : NodeConfig [ ] = [ ] ) => {
33- const propertyKeyInfo = { } ;
34- nodes . forEach ( node => {
35- Object . keys ( node . properties ) . forEach ( propertyKey => {
36- // 目前过滤只保留可以转成数值型的或日期型的, todo: 统一转成one-hot特征向量
37- if ( ! `${ node . properties [ propertyKey ] } ` . match ( secondReg ) &&
38- ! `${ node . properties [ propertyKey ] } ` . match ( dateReg ) &&
39- isNaN ( Number ( node . properties [ propertyKey ] ) ) || propertyKey === 'id' ) {
40- if ( propertyKeyInfo . hasOwnProperty ( propertyKey ) ) {
41- delete propertyKeyInfo [ propertyKey ] ;
42- }
43- return ;
44- }
45- if ( propertyKeyInfo . hasOwnProperty ( propertyKey ) ) {
46- propertyKeyInfo [ propertyKey ] += 1 ;
47- } else {
48- propertyKeyInfo [ propertyKey ] = 1 ;
49- }
50- } )
51- } )
52-
53- // 取top50的属性
54- const sortKeys = Object . keys ( propertyKeyInfo ) . sort ( ( a , b ) => {
55- return propertyKeyInfo [ b ] - propertyKeyInfo [ a ] ;
56- } ) ;
57- return sortKeys . length < 100 ? sortKeys : sortKeys . slice ( 0 , 100 ) ;
58- }
59-
60- const processProperty = ( properties , propertyKeys ) => propertyKeys . map ( key => {
61- if ( properties . hasOwnProperty ( key ) ) {
62- // 可以转成数值的直接转成数值
63- if ( ! isNaN ( Number ( properties [ key ] ) ) ) {
64- return Number ( properties [ key ] ) ;
65- }
66- // 时间型的转成时间戳
67- if ( properties [ key ] . match ( secondReg ) || properties [ key ] . match ( dateReg ) ) {
68- // @ts -ignore
69- return Number ( Date . parse ( new Date ( properties [ key ] ) ) ) / 1000 ;
70- }
71- }
72- return 0 ;
73- } )
74-
75- // 获取属性特征权重
76- const getPropertyWeight = ( propertyKeys , nodes ) => {
77- let allPropertiesWeight = [ ] ;
78- for ( let i = 0 ; i < nodes . length ; i ++ ) {
79- allPropertiesWeight [ i ] = processProperty ( nodes [ i ] . properties , propertyKeys ) ;
80- }
81- return allPropertiesWeight ;
82- }
83-
8431// 模块惯性度,衡量属性相似度
8532const getInertialModularity = (
8633 nodes : NodeConfig [ ] = [ ] ,
@@ -171,9 +118,8 @@ const louvain = (
171118 node . properties . nodeType = nodeTypeInfo . findIndex ( nodeType => nodeType === node . nodeType ) ;
172119 } )
173120 }
174- const propertyKeys = getAllSortProperties ( nodes ) ;
175121 // 所有节点属性特征向量集合
176- allPropertiesWeight = getPropertyWeight ( propertyKeys , nodes ) ;
122+ allPropertiesWeight = getPropertyWeight ( nodes ) ;
177123 }
178124
179125 let uniqueId = 1 ;
0 commit comments