/
textAnalyzer.js
120 lines (91 loc) · 2.76 KB
/
textAnalyzer.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
/**
* Unlicensed code created by A Softer Space, 2019
* www.asofterspace.com/licenses/unlicense.txt
*/
window.tA = {
doanalyze: function() {
var orig = document.getElementById("tA-in").value;
var words = 1;
var lines = 1;
if (orig.trim() == "") {
words = 0;
lines = 0;
}
var sentences = 0;
var characterOccurrences = [];
var lastChar = ' ';
for (var i = 0; i < orig.length; i++) {
var curChar = orig.charAt(i);
if (curChar == "\n") {
lines++;
if ((lastChar != "\n") && (lastChar != " ")) {
words++;
}
} else {
if ((curChar == " ") && (lastChar != " ")) {
words++;
}
}
if (this.isSentenceEnd(curChar) && !this.isSentenceEnd(lastChar)) {
sentences++;
}
var curCharVal = curChar.toUpperCase().charCodeAt(0);
if (characterOccurrences[curCharVal]) {
characterOccurrences[curCharVal] += 1;
} else {
characterOccurrences[curCharVal] = 1;
}
lastChar = curChar;
}
var mostCommonChar = " ";
var mostCommonCharAmount = 0;
for (var i = 0; i < 65536; i++) {
if (characterOccurrences[i] > mostCommonCharAmount) {
mostCommonCharAmount = characterOccurrences[i];
mostCommonChar = String.fromCharCode(i);
}
}
if (mostCommonChar == " ") {
mostCommonChar = " ";
}
var mostCommonLetter = " ";
var mostCommonLetterAmount = 0;
for (var i = 65; i < 91; i++) {
if (characterOccurrences[i] > mostCommonLetterAmount) {
mostCommonLetterAmount = characterOccurrences[i];
mostCommonLetter = String.fromCharCode(i);
}
}
if (mostCommonLetter == " ") {
mostCommonLetter = " ";
}
document.getElementById("tA-out-length").innerHTML = "" + orig.length;
document.getElementById("tA-out-lines").innerHTML = "" + lines;
document.getElementById("tA-out-word-count").innerHTML = "" + words;
document.getElementById("tA-out-sentence-count").innerHTML = "" + sentences;
document.getElementById("tA-out-size-est").innerHTML = this.byteAmountToSizeString(orig.length);
document.getElementById("tA-out-most-common-char").innerHTML = mostCommonChar + " (" + mostCommonCharAmount;
document.getElementById("tA-out-most-common-letter").innerHTML = mostCommonLetter + " (" + mostCommonLetterAmount;
},
isSentenceEnd: function(s) {
return (s == ".") || (s == "!") || (s == "?");
},
byteAmountToSizeString: function(byteAmount) {
var resultAmount = byteAmount;
var resultUnit = "B";
if (resultAmount > 1024) {
resultAmount = resultAmount / 1024;
resultUnit = "KB";
}
if (resultAmount > 1024) {
resultAmount = resultAmount / 1024;
resultUnit = "MB";
}
if (resultAmount > 1024) {
resultAmount = resultAmount / 1024;
resultUnit = "GB";
}
return (Math.round(resultAmount * 100) / 100) + " " + resultUnit;
},
};
tA.doanalyze();