-
Notifications
You must be signed in to change notification settings - Fork 1
/
countBy.js
137 lines (128 loc) · 2.33 KB
/
countBy.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const R = require('ramda');
const {countBy, invert, toLower, map, sortBy, pipe, match, tap, curry, identity} = R;
const log = curry((desc, x) => tap(()=> {console.log(desc, x)}, x));
const text = `
'have no fear of this mess,'
said the cat in the hat.
'i always pick up all my playthings
and so...
i will show you another
good trick that i know!'
then we saw him pick up all the things that were down.
he picked up the cake,
and the rake, and the gown,
and the milk, and the strings,
and the books, and the dish,
and the fan, and the cup,
and the ship, and the fish.
and he put them away.
then he said, 'that is that.'
and then he was gone
with a tip of his hat.
then our mother came in
and she said to us two,
'did you have any fun?
tell me. what did you do?'
and sally and i did not know
what to say.
should we tell her
the things that went on there that day?
should we tell her about it?
now, what SHOULD we do?
well...
what would YOU do
if your mother asked YOU?`;
// invert: from {foo: 'bar'} to {bar: foo}, if there are multi 'bar'
// then it group those into array
const countByWords = pipe(
match(/\w+/g),
countBy(toLower),
invert,
map(sortBy(identity))
);
const result = countByWords(text);
console.log(result);
/*
* { '1':
[ 'a',
'about',
'always',
'another',
'any',
'asked',
'away',
'books',
'cake',
'came',
'cat',
'cup',
'day',
'dish',
'down',
'fan',
'fear',
'fish',
'fun',
'gone',
'good',
'gown',
'him',
'his',
'if',
'is',
'it',
'me',
'mess',
'milk',
'my',
'no',
'not',
'now',
'on',
'our',
'picked',
'playthings',
'put',
'rake',
'sally',
'saw',
'say',
'she',
'ship',
'show',
'so',
'strings',
'them',
'there',
'this',
'tip',
'trick',
'two',
'us',
'was',
'well',
'went',
'were',
'will',
'with',
'would',
'your' ],
'2':
[ 'all',
'hat',
'have',
'her',
'in',
'know',
'mother',
'of',
'pick',
'things',
'to' ],
'3': [ 'did', 'do', 'said', 'should', 'tell', 'up' ],
'4': [ 'he', 'i', 'then', 'we', 'what' ],
'5': [ 'you' ],
'6': [ 'that' ],
'15': [ 'the' ],
'16': [ 'and' ] }
* */