-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcloud.js
98 lines (74 loc) · 2.44 KB
/
wordcloud.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
//
// Word Cloud
//
var maxWeightDouble = 0,
maxWeightSingle = 0,
doubleMaxWeight = '',
singleMaxWeight = '',
doubleWordcloud = {},
singleWordcloud = {},
wordcloud = function(message) {
var wordsArr = message.split(' '),
data = {
single: [],
double: [],
};
for (var i = 0; i <= wordsArr.length; i++) {
if (typeof wordsArr[i] === 'undefined' || wordsArr[i].length === 0) {
continue;
}
//
// Single Word
//
if (singleWordcloud[wordsArr[i]]) {
singleWordcloud[wordsArr[i]] = singleWordcloud[wordsArr[i]] + 1;
} else {
singleWordcloud[wordsArr[i]] = 1;
}
// Find Max Weight
if (singleWordcloud[wordsArr[i]] > maxWeightSingle) {
maxWeightSingle = singleWordcloud[wordsArr[i]];
singleMaxWeight = wordsArr[i];
}
//
// Double Word
//
var wc2dKey = '';
if (typeof wordsArr[i + 1] === 'undefined' || wordsArr[i + 1].length === 0) {
if (typeof wordsArr[i - 1] === 'undefined' || wordsArr[i - 1].length === 0) {
continue;
} else {
wc2dKey = wordsArr[i - 1] + ' ' + wordsArr[i];
}
} else {
wc2dKey = wordsArr[i] + ' ' + wordsArr[i + 1];
}
if (doubleWordcloud[wc2dKey]) {
doubleWordcloud[wc2dKey] = doubleWordcloud[wc2dKey] + 1;
} else {
doubleWordcloud[wc2dKey] = 1;
}
// Find Max Wieght
if (doubleWordcloud[wc2dKey] > maxWeightDouble) {
maxWeightDouble = doubleWordcloud[wc2dKey];
doubleMaxWeight = wc2dKey;
}
}
for (var key in doubleWordcloud) {
if (doubleWordcloud[key] > Math.round(maxWeightDouble/3)) {
data.double.push({name: key, weight: doubleWordcloud[key]});
}
}
for (var key in singleWordcloud) {
if (singleWordcloud[key] > Math.round(maxWeightSingle/3)) {
data.single.push({name: key, weight: singleWordcloud[key]});
}
}
return data;
};
//
// Export Variables
//
module.exports = {
update : wordcloud
};