From 16d77dd238e5a40ffffb8d317079a2df5c064afb Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Thu, 21 Mar 2019 10:59:38 +0700 Subject: [PATCH] Improve bias algorithm by reducing each currency metric the lower the influence --- src/content.js | 59 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/src/content.js b/src/content.js index 2c817c5..512ffd5 100644 --- a/src/content.js +++ b/src/content.js @@ -35,6 +35,50 @@ const preloadTweetData = () => { const escapeHtml = string => string.replace(/["'&<>]/g, ''); +const calculateBias = data => { + const SUPPORTED_CURRENCIES = ['BTC', 'ETH', 'XRP', 'BCH']; + const BIAS_REDUCER = 0.5; + const BIAS_MIN_THRESHOLD = 10; + + let currencies = data.clusters + .map(currency => ({ + symbol: currency.abbr, + name: currency.display, + influence: currency.score + })) + .filter(currency => SUPPORTED_CURRENCIES.includes(currency.symbol)) + .sort((a, b) => { + if (a.influence > b.influence) { + return -1; + } + if (a.influence < b.influence) { + return 1; + } + return 0; + }) + .map((currency, index) => { + const multiplier = Math.pow(BIAS_REDUCER, index); + const bias = currency.influence * multiplier; + + return {...currency, bias}; + }); + + const totalBiasSum = currencies + .map(currency => currency.bias) + .reduce((a, b) => a + b); + + currencies = currencies + .map(currency => { + let bias = (currency.bias / totalBiasSum) * 100; + bias = (bias < BIAS_MIN_THRESHOLD) ? 0 : bias; + + return {...currency, bias}; + }); + + return SUPPORTED_CURRENCIES + .map(symbol => currencies.find(currency => currency.symbol === symbol)); +} + const injectChart = async () => { const profileHoverContainer = document.querySelector('#profile-hover-container'); const profileCard = profileHoverContainer.querySelector('.profile-card'); @@ -82,24 +126,15 @@ const injectChart = async () => { const biases = container.children[0]; if (data) { - const currencies = data.clusters - .filter(currency => ['BTC', 'ETH', 'XRP', 'BCH'].includes(currency.abbr)); - - const totalScore = currencies - .map(currency => Number(currency.score)) - .reduce((a, b) => a + b); + const currencies = calculateBias(data); currencies.forEach(currency => { - const biasThreshold = 5; - let bias = (Number(currency.score) / totalScore) * 100; - bias = (bias < biasThreshold) ? 0 : bias; - const container = document.createElement('div'); container.innerHTML = `
- ${escapeHtml(currency.display)} + ${escapeHtml(currency.name)}
-
+
`; biases.appendChild(container.children[0]);