Skip to content

Commit 61144e4

Browse files
committed
Add character count to analysis
1 parent 844c4c1 commit 61144e4

File tree

3 files changed

+695
-6
lines changed

3 files changed

+695
-6
lines changed

scripts/analyze.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ const languages = [
3131
'swift'
3232
]
3333

34+
let numberOfCharacters = 0
35+
let numberOfLines = 0
36+
let numberOfWords = 0
37+
3438
const pairs = []
3539
const allWords = []
3640
const allPunctuation = []
41+
const allCharacters = []
3742

3843
languages.forEach(language => {
3944
const languagePath = '../' + language
@@ -45,12 +50,22 @@ languages.forEach(language => {
4550
const filePath = projectPath + '/' + file
4651
const lines = fs.readFileSync(filePath, { encoding: 'utf8' }).split('\n')
4752
lines.forEach(line => {
53+
numberOfLines++
54+
4855
// Compact and replace all whitespace into regular, single spaces
4956
line = line.replace(/\s+/g, ' ')
5057

58+
line.split('').forEach(character => {
59+
if (allCharacters[character] === undefined) allCharacters[character] = { key: character, value: 0 }
60+
allCharacters[character]["value"]++
61+
numberOfCharacters++
62+
})
63+
5164
words = line.split(' ')
5265

5366
words.forEach(word => {
67+
numberOfWords++
68+
5469
const foundWords = word.match(/[A-Za-z0-9]+/g)
5570
if (foundWords) {
5671
foundWords.forEach(_word => {
@@ -67,8 +82,8 @@ languages.forEach(language => {
6782
})
6883
}
6984

70-
const numberOfPairs = word.length - 1
71-
for (let position = 0; position < numberOfPairs; position++) {
85+
const _numberOfPairs = word.length - 1
86+
for (let position = 0; position < _numberOfPairs; position++) {
7287
const pair = String(word.substring(position, position + 2))
7388
if (pairs[pair] === undefined) {
7489
pairs[pair] = { key: pair, value: 0 }
@@ -79,7 +94,7 @@ languages.forEach(language => {
7994
})
8095
})
8196
})
82-
console.log(`Finished analyzing '${language}'...`)
97+
console.log(`Finished analyzing '${language}'`)
8398
})
8499

85100
let fileData
@@ -106,6 +121,7 @@ allPairs.forEach(pair => {
106121
fileData += '| ' + pair.key + ' | ' + pair.value + ' |\n'
107122
})
108123
fs.writeFile('./results/all_pairs.md', fileData, onFileWriteErrorHandler)
124+
console.log(`All pairs counted, ${allPairs.length} found`);
109125

110126
alphanumericPairs.sort(function(a, b) {
111127
return b.value - a.value
@@ -115,6 +131,7 @@ alphanumericPairs.forEach(pair => {
115131
fileData += '| ' + pair.key + ' | ' + pair.value + ' |\n'
116132
})
117133
fs.writeFile('./results/alphanumeric_pairs.md', fileData, onFileWriteErrorHandler)
134+
console.log(`All alphanumeric pairs counted, ${alphanumericPairs.length} found`);
118135

119136
//
120137

@@ -131,6 +148,7 @@ all_words.forEach(word => {
131148
fileData += '| ' + word.key + ' | ' + word.value + ' |\n'
132149
})
133150
fs.writeFile('./results/all_words.md', fileData, onFileWriteErrorHandler)
151+
console.log(`All words counted, ${numberOfWords} found`);
134152

135153
//
136154

@@ -142,14 +160,29 @@ for (const key in allPunctuation) {
142160
punctuations.sort(function(a, b) {
143161
return b.value - a.value
144162
})
145-
fileData = '| Punctuation | Count |\n| ---- | ----- |\n'
163+
fileData = '| Punctuation | Count |\n| ----------- | ----- |\n'
146164
punctuations.forEach(punctuation => {
147165
fileData += '| ' + punctuation.key + ' | ' + punctuation.value + ' |\n'
148166
})
149167
fs.writeFile('./results/all_punctuation.md', fileData, onFileWriteErrorHandler)
168+
console.log(`All punctuation counted, ${punctuations.length} found`);
169+
170+
const characters = []
171+
for (const key in allCharacters) {
172+
characters.push(allCharacters[key])
173+
}
174+
characters.sort(function(a, b) {
175+
return b.value - a.value
176+
})
177+
fileData = '| Character | Count |\n| --------- | ----- |\n'
178+
characters.forEach(character => {
179+
fileData += '| ' + character.key + ' | ' + character.value + ' |\n'
180+
})
181+
fs.writeFile('./results/all_characters.md', fileData, onFileWriteErrorHandler)
182+
console.log(`All characters counted, ${numberOfCharacters} found`);
150183

151184
const timeElapsed = Math.round((new Date().getTime() - startedAt) / 100) / 10
152-
console.log(timeElapsed + ' seconds')
185+
console.log(`${numberOfLines} lines parsed in ${timeElapsed} seconds`);
153186

154187
function onFileWriteErrorHandler(error) {
155188
// Do nothing...

0 commit comments

Comments
 (0)