-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.js
234 lines (198 loc) · 5.76 KB
/
src.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// Copyright: 2016
// Author: Isaac Dozier
// Email: isaactdozier@gmail.com
//
const dataSizeMatchMsg = "Data file Size Issue (data stream does not match/sync, check that both dataNew and dataArray are working streams of data.txt file)"
const fs = require('fs');
const alphaConst = 'bcdfghjklmnpqrstvwxz'.split('');
const alphaVowel = 'aeiouy'.split('');
//read and write files
const fileData = 'data.txt',
fileRecentWords = 'recentWords.txt',
fileSection = 'section.txt',
fileHistory = 'history.txt',
fileEnglishWords = 'words.txt',
//testing purposes--
//Beta testing paragraphs (current version | v1.0.1)
fileTestWords = 'testWords.txt';
//read wordsFile or fileTestWords and convert to array
var wordsArray = wordlistToArray(fs.readFileSync(fileTestWords,'utf8'));
//read current data.txt, history.txt files
var data = fs.readFileSync(fileData, 'utf8');
var history = fs.readFileSync(fileHistory, 'utf8');
//clean data.txt file stream of seperators
var dataArray = data.replace(/\r\n/g,'\n').split('\n');
var dataNew = dataArray;
var dataSection;
var dataTrail;
//check that altered data stream is not corrupted
var streamSynced = streamCheck(dataNew, dataArray);
var base = dataArray.length * 0.3;
var build = new Object();
function streamCheck(a,b){
//basic size match before deep check
var equalDataSize = a.length === b.length;
var partIdentify;
var fullIdentify;
var checkCleanTail;
var hickup;
if(equalDataSize){
for(var i=0;i<a.length;i++){
var equalStrSet = a[i] === b[i];
var equalStrOffset = a[i] === b[i+1] || b[i] === a[i+1];
if(equalStrOffset && !partIdentify){
partIdentify = true;
} else if(equalStrOffset && partIdentify){
fullIdentify = true;
} else if(fullIdentify && !equalStrSet){
hickup = false;
} else if(!hickup){
return true;
}
}
} else {
//throws error before deep stream sync check
throw new errorMsg(dataSizeMatchMsg)
}
};
function wordSwap(i){
const mark = '-';
var a = dataNew[i]
var b = dataNew[i - 1];
//check if str set is first in array
//if so, dont do anything
var moveable = i !== 0;
if(streamSynced){
if(moveable){
if(a.indexOf(mark) > -1){
a.replace(mark, '');
}
return dataNew.splice(i - 1, 2, a + '\r\n' + b);
}
} else {
throw new errorMsg(dataSizeMatchMsg);
}
}
function recognitionCheckBasic(str){
return 'test';
}
//main functionality
wordsArray.map(function(t,i){
var ref = '"(){}[]'.concat("'").split('');
var word = scrubRef(t,ref);
//WORD
build.word = word;
//REC
build.rec = recognitionCheckBasic(word);
//DNA
build.dna = dataNew.filter(function(str,piece){
return build.word.includes(str) && i < base;
},dataNew);
//SCORE
build.score = build.dna.map(function(str){
return dataNew.indexOf(str);
});
//SCORE-AVERAGE
if(build.score.length > 0){
build.scoreAvg = arrayTotalValue(build.score) / build.score.length;
} else {
build.scoreAvg = Array();
}
//CONST
build.const = alphaConst.filter(function(str){
return build.dna.toString().includes(str);
});
//VOWEL
build.vowel = alphaVowel.filter(function(str){
return build.dna.toString().includes(str);
});
//GATHER
//
if(dataTrail == undefined){
dataTrail = build.word;
} else {
dataTrail = dataTrail.concat(' ' + t);
}
//section seperator
//seperates according to punctuation
var endStatement = wordsArray.length === i;
if(endStatement){
if(dataSection == undefined){
dataSection = dataTrail;
} else {
dataSection = dataSection.concat('\r\n' + dataTrail);
}
//write to save files
//short term memory save
writeFile(fileSection, dataTrail.split(' ').join('\n'));
//history save
writeFile(fileHistory, dataSection + '\r\n' + history);
//clear section gathering
//for next sentence
dataTrail = null;
}
//
//alter dataNew array
//to overwrite data.txt
dataNew.map(function(str){
if(build.word.includes(str)){
wordSwap(dataNew.indexOf(str));
writeFile(fileData, dataNew.join('\n'));
}
})
//for the humans
console.log(build.word + '-' + build.scoreAvg + '-' + build.rec);
});
//
//clear stack before moving on to next cycle
//
//
//write changes to: data.txt
// history.txt
function writeFile(file,data){
process.nextTick(function(){
if(streamSynced){
fs.writeFileSync(file,data,'utf8');
}
});
}
//helper functions
function removeVowelArr(str){;
return str.split('').filter(function(l){
return alphaVowel.indexOf(l) > - 1;
});
}
function scrubRef(word,refArray){
var tmp = word;
refArray.map(function(p){
tmp = tmp.replace(p, '')
});
return tmp;
}
function wordlistToArray(data){
//trim whitespace line-by-line
data = data.split('\n').map(function(line){
return line.trim();
}).join('\n');
//seperate words line-by-line
data = data.replace(/ /g,'\n');
//return data as array
return data.toLowerCase().toString().split('\n');
}
//returns combined values of all numbers in array
function arrayTotalValue(arr){
var t = 0;
arr.map(function(n){
t += n;
})
return t;
}
//basic error message handling
function errorMsg(message) {
this.constructor.prototype.__proto__ = Error.prototype;
// capture stack trace
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
}