bigly-caret/rank.js

142 lines
3.7 KiB
JavaScript
Raw Normal View History

2025-01-29 19:06:26 -05:00
2025-01-29 18:54:30 -05:00
// derived from https://git.dervland.net/biglyderv/new-bigly-chat/src/branch/master/docs/stats.php
2025-01-31 23:42:51 -05:00
function rankCalc(result, iterations = 10, main = [], domain_mode = false) {
2025-01-29 18:54:30 -05:00
let matrixe = {}
let fng = {};
2025-01-29 19:06:26 -05:00
let fnc = {};
2025-01-29 18:54:30 -05:00
let frs = {};
2025-01-30 08:20:17 -05:00
let msum_old = 0.001;
2025-01-29 18:54:30 -05:00
let pr = {};
2025-01-29 19:06:26 -05:00
2025-01-31 16:23:20 -05:00
let rl = Object.keys(result).length;
2025-01-29 18:54:30 -05:00
for (let unn in result) {
2025-01-31 23:29:01 -05:00
2025-01-29 18:54:30 -05:00
matrixe[unn] = {};
2025-01-29 19:06:26 -05:00
matrixe[unn][unn] = 1;
2025-01-29 18:54:30 -05:00
frs[unn] = result[unn].followers;
fng[unn] = result[unn].following;
2025-01-31 23:29:01 -05:00
2025-01-31 23:42:51 -05:00
let lf = Object.keys(fng[unn]).length;
if (domain_mode) {
let domains = [];
for (let x of fng[unn]) {
try {
let a = new URL(x);
domains.push(a.host);
} catch(err) {
}
2025-01-31 23:29:01 -05:00
}
2025-01-31 23:42:51 -05:00
domains = [...new Set(domains)];
fnc[unn] = lf / (1+domains.length);
} else {
fnc[unn] = lf;
2025-01-31 23:29:01 -05:00
}
2025-01-31 23:41:28 -05:00
2025-01-31 16:23:20 -05:00
pr[unn] = 0.1 / rl;
2025-01-29 18:54:30 -05:00
}
2025-01-29 19:06:26 -05:00
for (let unn in result) {
2025-01-29 18:54:30 -05:00
let fnu = frs[unn];
for (let follow of fnu) {
if (follow == unn) continue;
let dst = fnc[fnu] || 0;
2025-01-31 23:41:28 -05:00
matrixe[unn][follow] = 1 + 1 / (dst + 3);
2025-01-29 18:54:30 -05:00
msum_old += matrixe[unn][follow];
}
for (let unn2 in result) {
if (!matrixe[unn][unn2]) {
matrixe[unn][unn2] = 0;
}
}
}
2025-01-30 09:56:42 -05:00
let mm = (process.env.matrixIterations || iterations);
2025-01-29 19:06:26 -05:00
2025-01-30 20:17:43 -05:00
2025-01-30 09:56:42 -05:00
for (let i = 0; i < mm; i++) {
2025-01-30 07:59:30 -05:00
let prold = pr;
let matrixf = matrixe;
pr = [];
matrixe = [];
2025-01-29 18:54:30 -05:00
let msum = 1;
2025-01-31 16:16:14 -05:00
let intv = Math.pow(0.01 / rl, Math.pow(0.09, i / mm));
2025-01-31 16:09:33 -05:00
console.log(`Completed ${i} iterations with ${intv} threshold`)
2025-01-29 18:54:30 -05:00
2025-01-30 11:00:54 -05:00
let th = -1;
2025-01-29 18:54:30 -05:00
for (let una in result) {
2025-01-30 11:00:54 -05:00
th++;
2025-01-31 16:23:20 -05:00
pr[una] = 0.1 / rl;
2025-01-30 07:59:30 -05:00
matrixe[una] = [];
2025-01-30 11:30:18 -05:00
if (frs[una].length == 0) {
2025-01-30 10:27:52 -05:00
pr[una] = prold[una];
2025-01-30 10:14:23 -05:00
matrixe[una] = matrixf[una];
2025-01-30 08:20:17 -05:00
continue;
}
2025-01-29 18:54:30 -05:00
2025-01-31 16:09:33 -05:00
let ar = Object.keys(result);
let rf = pr[una];
for (let unb of ar) {
2025-01-29 19:07:09 -05:00
let prb = prold[unb];
2025-01-31 16:09:33 -05:00
2025-01-30 08:34:59 -05:00
matrixe[una][unb] = 0.03;
2025-01-31 16:09:33 -05:00
if (prb * matrixf[una][unb] < intv || fnc[unb] == 0) {
let mfb = matrixf[una][unb];
if (isNaN(mfb) || !mfb) continue;
pr[una] += prb * mfb;
2025-01-29 18:54:30 -05:00
continue;
}
2025-01-31 21:46:59 -05:00
2025-01-29 18:54:30 -05:00
for (let unc in result) {
2025-01-31 16:09:33 -05:00
let mfc = matrixf[una][unc];
let mfb = matrixf[unc][unb];
if (isNaN(mfc) || isNaN(mfb) || !mfc || !mfb) continue;
matrixe[una][unb] += mfc * mfb;
2025-01-29 18:54:30 -05:00
}
2025-01-31 21:23:51 -05:00
msum += matrixe[una][unb];
2025-01-30 08:00:58 -05:00
pr[una] += prb * matrixe[una][unb];
2025-01-31 21:46:59 -05:00
2025-02-01 02:12:01 -05:00
if (main.indexOf(unb) != -1) {
2025-01-31 21:46:59 -05:00
matrixe[una][unb] *= 1.15;
}
2025-01-29 18:54:30 -05:00
}
}
for (let una in result) {
if ((frs[una]).length == 0) continue;
for (let unb in result) {
matrixe[una][unb] *= msum_old / msum;
}
}
2025-01-31 17:54:32 -05:00
let ov = Object.keys(pr);
2025-01-31 20:55:49 -05:00
let new_sum = ov.filter(i => !isNaN(pr[i]) && main.indexOf(i) != -1).map(n => pr[n]).reduce((a, b) => a + b, 1e-9);
let new_sum2 = ov.filter(i => !isNaN(pr[i]) && main.indexOf(i) == -1).map(n => pr[n]).reduce((a, b) => a + b, 1e-9);
2025-01-29 18:54:30 -05:00
for (let unn in result) {
2025-01-31 17:54:32 -05:00
if (main.indexOf(unn) == -1) {
pr[unn] /= new_sum2 * 2;
} else {
pr[unn] /= new_sum * 2;
}
2025-01-29 18:54:30 -05:00
}
2025-01-30 11:30:18 -05:00
2025-01-29 18:54:30 -05:00
}
return pr;
}
2025-01-30 07:59:30 -05:00
export {
rankCalc
}