100 lines
2.5 KiB
JavaScript
100 lines
2.5 KiB
JavaScript
|
// derived from https://git.dervland.net/biglyderv/new-bigly-chat/src/branch/master/docs/stats.php
|
||
|
function rankCalc(result, iterations = 10) {
|
||
|
let matrixe = {}
|
||
|
let fng = {};
|
||
|
let fnc ={};
|
||
|
let frs = {};
|
||
|
let msum_old = 0;
|
||
|
let pr = {};
|
||
|
|
||
|
for (let unn in result) {
|
||
|
matrixe[unn] = {};
|
||
|
matrixe[unn][unn] =1;
|
||
|
frs[unn] = result[unn].followers;
|
||
|
fng[unn] = result[unn].following;
|
||
|
fnc[unn] = fng[unn].length;
|
||
|
pr[unn] = 1;
|
||
|
}
|
||
|
|
||
|
for (let unn in result ) {
|
||
|
let fnu = frs[unn];
|
||
|
for (let follow of fnu) {
|
||
|
if (follow == unn) continue;
|
||
|
let dst = fnc[fnu] || 0;
|
||
|
matrixe[unn][follow] = 1 + 0.1 / (dst + 10);
|
||
|
msum_old += matrixe[unn][follow];
|
||
|
}
|
||
|
for (let unn2 in result) {
|
||
|
if (!matrixe[unn][unn2]) {
|
||
|
matrixe[unn][unn2] = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
for (let i = 0; i < iterations; i++) {
|
||
|
let prold = structuredClone(pr);
|
||
|
let matrixf = structuredClone(matrixe);
|
||
|
let msum = 1;
|
||
|
|
||
|
for (let una in result) {
|
||
|
pr[una] = 0;
|
||
|
if (frs[una].length == 0) continue;
|
||
|
|
||
|
for (let unb in result) {
|
||
|
prb = prold[unb];
|
||
|
if (prb < 1 / 1e6 || Object.keys(fng[unb]).length == 0) {
|
||
|
//msum += matrixe[una][unb];
|
||
|
continue;
|
||
|
}
|
||
|
matrixe[una][unb] = 0.03;
|
||
|
|
||
|
|
||
|
for (let unc in result) {
|
||
|
matrixe[una][unb] += matrixf[una][unc] * matrixf[unc][unb];
|
||
|
}
|
||
|
|
||
|
msum += matrixe[una][unb];
|
||
|
pr[una] += prb * matrixe[una][unb];
|
||
|
|
||
|
console.log(msum_old)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
for (let una in result) {
|
||
|
if ((frs[una]).length == 0) continue;
|
||
|
|
||
|
for (let unb in result) {
|
||
|
matrixe[una][unb] *= msum_old / msum;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
new_sum = Object.values(pr).reduce((a,b) => a+b, 0)
|
||
|
|
||
|
for (let unn in result) {
|
||
|
h = pr[unn];
|
||
|
pr[unn] /= new_sum;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return pr;
|
||
|
|
||
|
}
|
||
|
|
||
|
console.log(rankCalc({
|
||
|
'david': {
|
||
|
'following': [],
|
||
|
'followers': ['davis','david','bob']
|
||
|
},
|
||
|
|
||
|
'bob': {
|
||
|
'following': ['davis','david','bob'],
|
||
|
'followers': ['davis','david','bob']
|
||
|
},
|
||
|
|
||
|
'davis': {
|
||
|
'following': ['davis','david','bob'],
|
||
|
'followers': ['davis','david','bob']
|
||
|
}
|
||
|
},100));
|