-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrumpasaurus.js
More file actions
142 lines (105 loc) · 4.18 KB
/
Copy pathtrumpasaurus.js
File metadata and controls
142 lines (105 loc) · 4.18 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
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
138
139
140
141
142
"use strict"
// Get the selected speech and initiate statistic generation
function getSpeech() {
// Get the name of the speech we're requesting
const title = document.getElementById("title").value
// Indicate something's happening
document.getElementById("speech").innerHTML = "Loading " + title + "..."
document.getElementById("results").innerHTML = "Processing..."
// Create a new AJAX request
var client = new XMLHttpRequest()
// Set up handler for AJAX response
client.onreadystatechange = function() {
// Check response is a good one
if (this.readyState === 4 && this.status === 200) {
// We have the response, update the page
document.getElementById("speech").innerHTML = client.responseText
// And generate the stats for the speech
// Give the browser a chance to get something on screen
setTimeout(statistics, 200)
}
}
// Request the file
client.open("GET", "/trumpasaurus/speeches/" + title)
client.send()
}
// Generate stats based on the selected speech
function statistics() {
// Record the time we began processing for later
const start = new Date()
// Get the speech and results from the HTML
const speech = document.getElementById("speech").innerText.toLowerCase()
const results = document.getElementById("results")
// Extract sentences and sort by length
const sentences = speech.split(/[!.?] */)
sentences.sort(function(a, b){ return b.length - a.length })
// Create array for results
var phrases = []
var uniqueWords = []
var totalWords = 0
// Iterate over each sentence and create unsorted associative array of phrases
for (var s in sentences) {
// Split sentence into words
var words = sentences[s].split(/[ •!"\#$%&()*+,\-./:;<=>?@\[\\\]^_`{|}~—–]+/)
totalWords += words.length
// Create/increment entry for each word
for (var i = 0; i < words.length; ++i) {
// Create entry if it doesn't exist, otherwise increment
uniqueWords[words[i]] == undefined
? uniqueWords[words[i]] = 1
: ++uniqueWords[words[i]]
// Initialise segment
var segment = ""
// Iterate over remaining sentence creating entries as we go
for (var j = i; j < words.length; ++j) {
// Append to segment
segment += words[j] + " "
// Check if we've already seen it
if (phrases[segment] === undefined)
phrases[segment] = 1
else
++phrases[segment]
}
}
}
var uniqueWordCount = 0
for (var i in uniqueWords)
++uniqueWordCount
// Word stats
results.innerHTML = "<h3>Summary</h3>"
results.innerHTML += "Total words " + totalWords + "<br>"
results.innerHTML += "Unique words " + uniqueWordCount + "<br>"
const uniqueWordRatio = (100 * uniqueWordCount / totalWords)
// Sentence stats
results.innerHTML += "Unique word ratio " + uniqueWordRatio.toFixed(0) + "%<br>"
results.innerHTML += "Sentences " + sentences.length + "<br>"
const averageSentenceLength = totalWords / sentences.length
results.innerHTML += "Sentence length " + averageSentenceLength.toFixed(0) + "<br>"
// Create array for our trimmed down results
var pruned = []
// Store if occurance count is significant
for (var i in phrases)
if (phrases[i] > 1 && i.split(" ").length > 3)
pruned.push({ phrase: i, freq: phrases[i], count: i.split(" ").length })
// Sort by frequency
pruned.sort(function(a, b) { return b.freq - a.freq })
// Sentence / phrase ratio can only be calculated after pruning
const sentencePhraseRatio = pruned.length / sentences.length
results.innerHTML += "Phrase/sentence ratio "
+ sentencePhraseRatio.toFixed(2) + "<br>"
// Summarise in a sentence
const summary =
uniqueWordRatio < 25 ? "is repetitive and tiresome" :
uniqueWordRatio < 30 ? "is overly finessed" :
uniqueWordRatio < 33 ? "has had some work done" :
uniqueWordRatio < 36 ? "is practiced but loose" :
uniqueWordRatio < 42 ? "is spontaneous"
: "is a rant"
results.innerHTML += "<br>This speech " + summary + "<br>"
// Report processing time
results.innerHTML += "<br><small>Processing took " + (new Date() - start) + "ms</small><br>"
// Dump the common phrases
results.innerHTML += "<h3>Common phrases " + pruned.length + "</h3>"
for (var i in pruned)
results.innerHTML += pruned[i].phrase + " - " + pruned[i].freq + "<br>"
}