Skip to content

Commit 0d532ca

Browse files
committed
Rewrite code and update results, objective-c included
1 parent 61144e4 commit 0d532ca

File tree

10 files changed

+141657
-145637
lines changed

10 files changed

+141657
-145637
lines changed

scripts/analyze.js

Lines changed: 8 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -14,177 +14,28 @@ NOTE: code is ugly, blah blah
1414
*/
1515

1616
const fs = require('fs')
17+
const process = require('process')
18+
const App = require('./lib/app.js')
1719
const startedAt = new Date().getTime()
1820

19-
// objective-c is disabled, because the corpus folder contains subfolder,
20-
// and it is a Sunday, which makes me too lazy to consider adding things
21-
// such as recursion.
2221
const languages = [
2322
'c',
2423
'cc',
2524
'java',
2625
'javascript',
27-
// 'objective-c',
26+
'objective-c',
2827
'php',
2928
'python',
3029
'ruby',
3130
'swift'
3231
]
3332

34-
let numberOfCharacters = 0
35-
let numberOfLines = 0
36-
let numberOfWords = 0
37-
38-
const pairs = []
39-
const allWords = []
40-
const allPunctuation = []
41-
const allCharacters = []
33+
const app = new App('..')
4234

4335
languages.forEach(language => {
44-
const languagePath = '../' + language
45-
const projects = fs.readdirSync(languagePath)
46-
projects.forEach(project => {
47-
const projectPath = languagePath + '/' + project
48-
const files = fs.readdirSync(projectPath)
49-
files.forEach(file => {
50-
const filePath = projectPath + '/' + file
51-
const lines = fs.readFileSync(filePath, { encoding: 'utf8' }).split('\n')
52-
lines.forEach(line => {
53-
numberOfLines++
54-
55-
// Compact and replace all whitespace into regular, single spaces
56-
line = line.replace(/\s+/g, ' ')
57-
58-
line.split('').forEach(character => {
59-
if (allCharacters[character] === undefined) allCharacters[character] = { key: character, value: 0 }
60-
allCharacters[character]["value"]++
61-
numberOfCharacters++
62-
})
63-
64-
words = line.split(' ')
65-
66-
words.forEach(word => {
67-
numberOfWords++
68-
69-
const foundWords = word.match(/[A-Za-z0-9]+/g)
70-
if (foundWords) {
71-
foundWords.forEach(_word => {
72-
if (allWords[_word] === undefined) allWords[_word] = { key: _word, value: 0 }
73-
allWords[_word]["value"]++
74-
})
75-
}
76-
77-
const foundPunctuation = word.match(/[^A-Za-z0-9]+/g)
78-
if (foundPunctuation) {
79-
foundPunctuation.forEach(_punctuation => {
80-
if (allPunctuation[_punctuation] === undefined) allPunctuation[_punctuation] = { key: _punctuation, value: 0 }
81-
allPunctuation[_punctuation]["value"]++
82-
})
83-
}
84-
85-
const _numberOfPairs = word.length - 1
86-
for (let position = 0; position < _numberOfPairs; position++) {
87-
const pair = String(word.substring(position, position + 2))
88-
if (pairs[pair] === undefined) {
89-
pairs[pair] = { key: pair, value: 0 }
90-
}
91-
pairs[pair]["value"]++
92-
}
93-
})
94-
})
95-
})
96-
})
97-
console.log(`Finished analyzing '${language}'`)
98-
})
99-
100-
let fileData
101-
102-
const allPairs = []
103-
const alphanumericPairs = []
104-
105-
for (const key in pairs) {
106-
const pair = pairs[key]
107-
const value = pairs[key].value
108-
if (value > 10) {
109-
allPairs.push(pair)
110-
if (key.match(/^[a-zA-Z0-9]+$/)) {
111-
alphanumericPairs.push(pair)
112-
}
113-
}
114-
}
115-
116-
allPairs.sort(function(a, b) {
117-
return b.value - a.value
118-
})
119-
fileData = '| Pair | Count |\n| ---- | ----- |\n'
120-
allPairs.forEach(pair => {
121-
fileData += '| ' + pair.key + ' | ' + pair.value + ' |\n'
122-
})
123-
fs.writeFile('./results/all_pairs.md', fileData, onFileWriteErrorHandler)
124-
console.log(`All pairs counted, ${allPairs.length} found`);
125-
126-
alphanumericPairs.sort(function(a, b) {
127-
return b.value - a.value
36+
app.loadLanguage(language)
12837
})
129-
fileData = '| Pair | Count |\n| ---- | ----- |\n'
130-
alphanumericPairs.forEach(pair => {
131-
fileData += '| ' + pair.key + ' | ' + pair.value + ' |\n'
132-
})
133-
fs.writeFile('./results/alphanumeric_pairs.md', fileData, onFileWriteErrorHandler)
134-
console.log(`All alphanumeric pairs counted, ${alphanumericPairs.length} found`);
135-
136-
//
137-
138-
const all_words = []
139-
for (const key in allWords) {
140-
all_words.push(allWords[key])
141-
}
142-
143-
all_words.sort(function(a, b) {
144-
return b.value - a.value
145-
})
146-
fileData = '| Word | Count |\n| ---- | ----- |\n'
147-
all_words.forEach(word => {
148-
fileData += '| ' + word.key + ' | ' + word.value + ' |\n'
149-
})
150-
fs.writeFile('./results/all_words.md', fileData, onFileWriteErrorHandler)
151-
console.log(`All words counted, ${numberOfWords} found`);
152-
153-
//
154-
155-
const punctuations = []
156-
for (const key in allPunctuation) {
157-
punctuations.push(allPunctuation[key])
158-
}
159-
160-
punctuations.sort(function(a, b) {
161-
return b.value - a.value
162-
})
163-
fileData = '| Punctuation | Count |\n| ----------- | ----- |\n'
164-
punctuations.forEach(punctuation => {
165-
fileData += '| ' + punctuation.key + ' | ' + punctuation.value + ' |\n'
166-
})
167-
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`);
183-
184-
const timeElapsed = Math.round((new Date().getTime() - startedAt) / 100) / 10
185-
console.log(`${numberOfLines} lines parsed in ${timeElapsed} seconds`);
18638

187-
function onFileWriteErrorHandler(error) {
188-
// Do nothing...
189-
// This is just here to suppress the silly deprecation warning.
190-
}
39+
app.writeResultsToDisk()
40+
app.showStats()
41+
process.exit()

0 commit comments

Comments
 (0)