@@ -31,9 +31,14 @@ const languages = [
31
31
'swift'
32
32
]
33
33
34
+ let numberOfCharacters = 0
35
+ let numberOfLines = 0
36
+ let numberOfWords = 0
37
+
34
38
const pairs = [ ]
35
39
const allWords = [ ]
36
40
const allPunctuation = [ ]
41
+ const allCharacters = [ ]
37
42
38
43
languages . forEach ( language => {
39
44
const languagePath = '../' + language
@@ -45,12 +50,22 @@ languages.forEach(language => {
45
50
const filePath = projectPath + '/' + file
46
51
const lines = fs . readFileSync ( filePath , { encoding : 'utf8' } ) . split ( '\n' )
47
52
lines . forEach ( line => {
53
+ numberOfLines ++
54
+
48
55
// Compact and replace all whitespace into regular, single spaces
49
56
line = line . replace ( / \s + / g, ' ' )
50
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
+
51
64
words = line . split ( ' ' )
52
65
53
66
words . forEach ( word => {
67
+ numberOfWords ++
68
+
54
69
const foundWords = word . match ( / [ A - Z a - z 0 - 9 ] + / g)
55
70
if ( foundWords ) {
56
71
foundWords . forEach ( _word => {
@@ -67,8 +82,8 @@ languages.forEach(language => {
67
82
} )
68
83
}
69
84
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 ++ ) {
72
87
const pair = String ( word . substring ( position , position + 2 ) )
73
88
if ( pairs [ pair ] === undefined ) {
74
89
pairs [ pair ] = { key : pair , value : 0 }
@@ -79,7 +94,7 @@ languages.forEach(language => {
79
94
} )
80
95
} )
81
96
} )
82
- console . log ( `Finished analyzing '${ language } '... ` )
97
+ console . log ( `Finished analyzing '${ language } '` )
83
98
} )
84
99
85
100
let fileData
@@ -106,6 +121,7 @@ allPairs.forEach(pair => {
106
121
fileData += '| ' + pair . key + ' | ' + pair . value + ' |\n'
107
122
} )
108
123
fs . writeFile ( './results/all_pairs.md' , fileData , onFileWriteErrorHandler )
124
+ console . log ( `All pairs counted, ${ allPairs . length } found` ) ;
109
125
110
126
alphanumericPairs . sort ( function ( a , b ) {
111
127
return b . value - a . value
@@ -115,6 +131,7 @@ alphanumericPairs.forEach(pair => {
115
131
fileData += '| ' + pair . key + ' | ' + pair . value + ' |\n'
116
132
} )
117
133
fs . writeFile ( './results/alphanumeric_pairs.md' , fileData , onFileWriteErrorHandler )
134
+ console . log ( `All alphanumeric pairs counted, ${ alphanumericPairs . length } found` ) ;
118
135
119
136
//
120
137
@@ -131,6 +148,7 @@ all_words.forEach(word => {
131
148
fileData += '| ' + word . key + ' | ' + word . value + ' |\n'
132
149
} )
133
150
fs . writeFile ( './results/all_words.md' , fileData , onFileWriteErrorHandler )
151
+ console . log ( `All words counted, ${ numberOfWords } found` ) ;
134
152
135
153
//
136
154
@@ -142,14 +160,29 @@ for (const key in allPunctuation) {
142
160
punctuations . sort ( function ( a , b ) {
143
161
return b . value - a . value
144
162
} )
145
- fileData = '| Punctuation | Count |\n| ---- | ----- |\n'
163
+ fileData = '| Punctuation | Count |\n| ----------- | ----- |\n'
146
164
punctuations . forEach ( punctuation => {
147
165
fileData += '| ' + punctuation . key + ' | ' + punctuation . value + ' |\n'
148
166
} )
149
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` ) ;
150
183
151
184
const timeElapsed = Math . round ( ( new Date ( ) . getTime ( ) - startedAt ) / 100 ) / 10
152
- console . log ( timeElapsed + ' seconds' )
185
+ console . log ( ` ${ numberOfLines } lines parsed in ${ timeElapsed } seconds` ) ;
153
186
154
187
function onFileWriteErrorHandler ( error ) {
155
188
// Do nothing...
0 commit comments