-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordCounter.html
More file actions
86 lines (62 loc) · 1.71 KB
/
WordCounter.html
File metadata and controls
86 lines (62 loc) · 1.71 KB
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>S(old)ver the Word Counter</title>
<meta charset="UTF-8">
<style>
html, body { height: 100%; margin: 0; }
body {
background-color: #111111;
color: white;
}
</style>
</head>
<body>
text: <input id="textImportId" type="file" accept="text/*"></input> <button onclick="countWords()">Count Words</button><br><br>
<pre id="wordCountId"></pre>
</div>
<script>
let fullText = "";
let sortedWordCounts = [];
function countWords(){
let words = fullText.replaceAll("\t","").replaceAll("\r\n", " ").split(" "); //these should be removed aswell :,{}()
let wordCounts = {};
for (let word of words){
if (wordCounts[word] == undefined){
wordCounts[word] = 0;
}
wordCounts[word]++;
}
sortedWordCounts = [];
for (let word in wordCounts) {
sortedWordCounts.push([word, wordCounts[word]]);
}
sortedWordCounts.sort(function(a, b) {
return b[1] - a[1];
});
setWordCountPreToWordCounts();
}
function setWordCountPreToWordCounts(){
let text = "";
for (let i in sortedWordCounts){
text += sortedWordCounts[i][0] + " " + sortedWordCounts[i][1] + "\n";
}
document.getElementById("wordCountId").innerHTML = text;
}
window.onload = function(){
let textInput = document.getElementById('textImportId');
textInput.addEventListener('change', handleTextFiles, false);
}
function handleTextFiles(e){ //https://stackoverflow.com/a/6776066/12777947
let f = e.target.files[0];
let reader = new FileReader();
reader.onload = function(event)
{
fullText = event.target.result;
};
reader.readAsText(f);
}
</script>
</script>
</body>
</html>