-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt.ts
55 lines (47 loc) · 1.57 KB
/
fmt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// deno-lint-ignore-file ban-ts-comment
// @ts-nocheck
// TODO: Object.keys is not type safe
import { colors } from "@cliffy/ansi";
import { DisplayType, SentimentAnalysisResult, SentimentMap } from "./type.ts";
export const DISPLAY: Record<SentimentMap, DisplayType> = {
negative: {
title: "👎 Negative:",
color: colors.red,
},
positive: {
title: "👍 Positive:",
color: colors.green,
},
notr: {
title: "🤷♂️ Notr: ",
color: colors.gray,
},
};
const calculateMedian = (values) => {
const sortedValues = values.slice().sort((a, b) => a - b);
return sortedValues.length % 2 === 0
? (sortedValues[sortedValues.length / 2 - 1] +
sortedValues[sortedValues.length / 2]) /
2
: sortedValues[Math.floor(sortedValues.length / 2)];
};
export const print = (data: SentimentAnalysisResult[]) => {
// TODO: refactor this
const allPositiveValues = data.map((item) => item.positive);
const allNegativeValues = data.map((item) => item.negative);
const allNotrValues = data.map((item) => item.notr);
const medianPositive = calculateMedian(allPositiveValues);
const medianNegative = calculateMedian(allNegativeValues);
const medianNotr = calculateMedian(allNotrValues);
const averageScores = {
positive: medianPositive,
negative: medianNegative,
notr: medianNotr,
};
const KVResult = Object.keys(averageScores);
KVResult.forEach((label) => {
const percentage = averageScores[label].toFixed(2) * 100;
const { title, color } = DISPLAY[label];
console.log(color(`${title} ${percentage}%`));
});
};