-
Notifications
You must be signed in to change notification settings - Fork 1
/
poindexter.js
98 lines (87 loc) · 2.75 KB
/
poindexter.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
function frequency(min, arr){
var result = group(
group(arr)
.map(function (word){ return [word[0], word[1].length]; }),
function (word){ return word[1]; },
function (word){ return word[0]; })
.map(function (count){
count[1].sort();
return [count[0], count[1].join(", ")];
})
.filter(function(count){
return count[0] >= min
});
result.sort(function (a, b){
return b[0] - a[0];
});
return result.map(function (word){
return fmt("$1: $2", word[0], word[1]);
}).join("\n<br>");
}
function wordCount(text){
var words = text
.replace(/<\/?(br|p)>/g, "\n")
.replace(/<[^>]+>/g, "")
.replace(/ /g, " ")
.match(/[\w'’]+/g);
return words && words.length || 0;
}
function countWords(){
var count = wordCount(writer.getValue());
data.chapters[data.currentChapter].currentCount = count;
totalWordCount.setValue(count);
var additional = count - data.chapters[data.currentChapter].lastCount;
if(additional >= 0){
additional = fmt("+$1", additional);
}
addWordCount.setValue(additional);
}
function analyzeScreenShow(){
var words = writer.getValue()
.replace(/<\/?(br|p)>/g, "\n")
.replace(/<[^>]+>/g, "")
.replace(/ /g, " ")
.match(/[\w'’]+/g),
min = parseInt(minFreqCount.getValue(), 10),
counts1 = "", counts2 = "", counts3 = "", counts4 = "";
var exclude = excludeWords
.getValue()
.split(",")
.map(function(word){
return word.trim();
})
.filter(function(word){
return word.length > 0;
});
if(exclude.length > 0){
words = words.filter(function(word){
return exclude.indexOf(word) == -1;
});
}
if(words != null){
words = words.map(function(word){
return word.toLowerCase();
});
counts1 = frequency(min, words);
var words2 = [], words3 = [], words4 = [];
for (var i = 0; i < words.length; ++i){
if (i > 0){
words2.push(words.slice(i - 1, i + 1).join(" "));
}
if (i > 1){
words3.push(words.slice(i - 2, i + 1).join(" "));
}
if (i > 2){
words4.push(words.slice(i - 3, i + 1).join(" "));
}
}
counts2 = frequency(min, words2);
counts3 = frequency(min, words3);
counts4 = frequency(min, words4);
}
word1Frequency.setValue(counts1);
word2Frequency.setValue(counts2);
word3Frequency.setValue(counts3);
word4Frequency.setValue(counts4);
ga('send', 'event', 'report', "analysisFreq");
}