-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtweetAnalyzer.js
79 lines (69 loc) · 2.18 KB
/
tweetAnalyzer.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const R = require('ramda');
const tweetAnalyzerSideEffects = require('./tweetAnalyzerSideEffects');
const getTweets = tweetAnalyzerSideEffects.getTweets;
const printGraph = tweetAnalyzerSideEffects.printGraph;
const HASHTAGS = [
'boring',
'cleancode',
'curling',
'devweek2018',
'donaldtrump',
'functionalprogramming',
'golang',
'iheartawards',
'javascript',
'nflplayoffs',
'nodejs',
'thebachelor'
];
const tweets = getTweets(HASHTAGS);
// [Tweet] -> [Tweet]
const removeDuplicates = R.uniqBy(R.prop('id_str'));
// Hashtag -> String
const getLowerCaseText = R.pipe(R.prop('text'), R.toLower);
// Tweet -> [String]
const getHashtagsFromTweet = R.pipe(
R.path(['entities', 'hashtags']),
R.map(getLowerCaseText)
);
// Tweet -> String
// If the tweet has multiple hashtags that match those in the list HASHTAGS this will
// return the first one and ignore any others
const getHashtagInList = R.pipe(
getHashtagsFromTweet,
R.intersection(HASHTAGS),
R.ifElse(R.isEmpty, R.always('not-found'), R.head)
);
// [Tweet] -> {hashtag-text: [Tweet]}
const groupTweetsByHashtag = R.groupBy(getHashtagInList)
// {hashtag-text: Number} -> [[hashtag-text, Number]]
const sortByValue = R.pipe(
R.toPairs,
R.sortBy(R.nth(1))
);
// [Hashtag, Number] -> String
// e.g. createHashtagBar(['cleancode', 40]) -> 'cleancode ████'
const createHashtagBar = pair => {
const hashtag = pair[0],
num = pair[1];
const legend = hashtag + ' '.repeat(25 - hashtag.length);
const bar = '█'.repeat(Math.ceil(num / 10));
return legend + bar + ' ' + Math.round(num * 100)/100;
};
// {hashtag-text: Number} -> String
const constructGraph = R.pipe(
sortByValue,
R.map(createHashtagBar),
R.join('\n')
);
// {hashtag-text: [Tweet]} -> {hashtag-text: Number}
const getTweetCountForHashtags = R.map(R.length);
// [Tweet] -> String
const getTweetCountGraph = R.pipe(
removeDuplicates, // [Tweet] -> [Tweet]
groupTweetsByHashtag, // [Tweet] -> {hashtag: [Tweet]}
getTweetCountForHashtags, // {hashtag-text : [Tweet]} -> {hashtag-text: Number}
constructGraph // {hashtag-text: Number} -> String
);
const tweetGraph = getTweetCountGraph(tweets);
printGraph(tweetGraph);