-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-tools.js
executable file
·254 lines (213 loc) · 8.91 KB
/
text-tools.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const fs = require('fs');
const path = require('path');
const availFeatures = ['-a', '-s', '-u', '-f', '-c', '-o'];
// command arguments
const args = process.argv || null;
const [feature, fnOne, fnTwo] = args.slice(2);
let helpMsg = `
please select feature:
\t-a folders - combine all txt files inside folder into all.txt
\t-u txtfile - unique sentences by lines
\t-f txtfile - shuffle lines of input file
\t-s txtfile - sort file content by lines
\t-c txtfile input_method.cin - use input method table to calculate pronunciation coverage rate of txtfile
\t-o txtfile txtref.txt - calculate how many chars from char_list.txt appearing in txtfile
`;
const errMsg = {
noFeature: new Error(`Please specify a feature to use: -a, -s, -u, -f, -c, or -o`),
noFileOne: new Error(`Please provide a file or directory as the first argument`),
noFileContent: new Error(`Didn't find any sentences in the file`),
noFileTwo: new Error(`Please provide a second file as the second argument`)
};
if (!feature || !availFeatures.includes(feature)) {
console.error(errMsg.noFeature);
console.log(helpMsg);
return;
}
if (!fnOne) {
console.error(errMsg.noFileOne);
console.log(helpMsg);
return;
}
// console.log('feature, fnOne, fnTwo:', feature, fnOne, fnTwo);
function getTxtFilesInPathSync(currentDirPath) {
let txtFiles = [];
fs.readdirSync(currentDirPath).forEach(function (name) {
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile() && path.extname(name) === '.txt')
txtFiles.push(filePath);
else if (stat.isDirectory())
txtFiles = txtFiles.concat(getTxtFilesInPathSync(filePath));
});
return txtFiles;
}
let txtOne = '', txtTwo = '';
try {
let txtFiles = [fnOne];
if (fs.statSync(fnOne).isDirectory())
txtFiles = getTxtFilesInPathSync(fnOne);
// console.log('txtFiles:', txtFiles);
txtFiles.forEach(function(fileName) {
txtOne += fs.readFileSync(fileName, 'utf-8') + '\n';
});
if (fnTwo)
txtTwo = fs.readFileSync(fnTwo, 'utf-8');
}
catch (error) {
console.error(error);
return;
}
if ((!txtOne) || (fnTwo && !txtTwo)) {
console.error(errMsg.noFileContent);
return;
}
function arrayShuffle(a) { // https://stackoverflow.com/a/6274381
let b = a.slice();
for (let i = b.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[b[i], b[j]] = [b[j], b[i]];
}
return b;
}
switch (feature) {
case '-o': {
if (!txtTwo) {
console.error(errMsg.noFileTwo);
return;
}
var convertToNonRepeatCharArray = function(txtFile) {
let txtAryOne = txtFile.split('\n');
let allCharObj = {};
txtAryOne.forEach(line => {
for (i=0; i<line.length; i++) allCharObj[line[i]] = 1;
});
return Object.keys(allCharObj); // all non-repeat chars
};
let charsOne = convertToNonRepeatCharArray(txtOne);
let charsTwo = convertToNonRepeatCharArray(txtTwo);
// check how many chars from charsTwo appear in charsOne
// compare both arry to each other
let charsTwoCoverStat = function(allChars, commonChars) {
return charsTwo.map(function(char) {
return charsOne.includes(char);
});
}(charsOne, charsTwo);
// list chars in charTwo which is not available in charOne
let missingChars = [];
let availChars = [];
for (const index in charsTwo) {
// console.log(`${index}: ${charsTwo[index]}: ${charsTwoCoverStat[index]}`);
if (charsTwoCoverStat[index])
availChars.push(charsTwo[index]);
else
missingChars.push(charsTwo[index]);
}
let coverRate = Math.round(availChars.length/charsTwo.length*1000)/10;
let missRate = Math.round(missingChars.length/charsTwo.length*1000)/10;
console.log(`Numbers of chars in ${fnOne} are ${charsOne.length}`);
console.log(`Numbers of chars in ${fnTwo} are ${charsTwo.length}`);
console.log(`--------------------`);
// console.log(`${fnOne} includes ${availChars.length} chars from ${fnTwo} (${coverRate}%): [${availChars}]`);
console.log(`${fnOne} includes ${availChars.length} chars from ${fnTwo} (${coverRate}%)`);
console.log(`${fnOne} missing ${missingChars.length} chars from ${fnTwo} (${missRate}%):`);
console.log(`[${missingChars}]`);
} break;
case '-c': {
if (!txtTwo) {
console.error(errMsg.noFileTwo);
return;
}
// read cin table and convert to obj map of char to phonetic
let [cinObj, phoneticNum] = function(cinFile){
let cinTable = cinFile.split('\n');
cinTable = cinTable.filter(line => { return !['#', '%'].includes(line[0]); });
cinTable = cinTable.filter(line => { return (line.length > 0); });
let cinObj = {};
let allPhonetic = {};
let ignore = [];
let ignoreRegex = null;
// ignore specific tones in .cin, eg. "3,4,6,7" in Chinese Zhuyin
if (args[5] == '-i' && args[6]) {
ignore = args[6].split(',');
ignoreRegex = new RegExp(ignore.join('|'));
}
cinTable.forEach(line => {
let [phe, char] = line.split(/\s|\t/);
let pheStr = phe.toString();
if (ignoreRegex) pheStr = pheStr.replace(ignoreRegex, '');
cinObj[char] = pheStr;
allPhonetic[pheStr] = 1;
});
return [cinObj, Object.keys(allPhonetic).length];
}(txtTwo);
// read sentences file and convert to non-repeat chars array
let txtAryOne = txtOne.split('\n');
let allCharObj = {};
txtAryOne.forEach(line => {
for (i=0; i<line.length; i++) allCharObj[line[i]] = 1;
});
let allCharAry = Object.keys(allCharObj); // all non-repeat chars
let allPhoneObj = {};
allCharAry.forEach(cha => { allPhoneObj[cinObj[cha]] = 1; });
let allPhoneLen = Object.keys(allPhoneObj).length; // all non-repeat phonetic
console.log(`Total numbers of phonetic in ${fnTwo} are ${phoneticNum}`);
console.log(`Numbers of phonetic from ${allCharAry.length} characters in ${fnOne} are ${allPhoneLen}`);
console.log(`We have cover ${Math.round(allPhoneLen/phoneticNum*10000)/100}% of the pronunciations.`);
} break;
case '-a': {
let txtAryOne = txtOne.split('\n');
txtAryOne = txtAryOne.map(sentence => sentence.trim());
txtAryOne = txtAryOne.filter(sentence => sentence); // trim empty lines
let fn = 'all.txt';
let err = fs.writeFileSync(fn, txtAryOne.join('\n'));
if (err)
console.log(err);
else
console.log("file saved as " + fn);
} break;
case '-u': {
let txtAryOne = txtOne.split('\n');
txtAryOne = txtAryOne.filter(sentence => sentence); // trim empty lines
txtAryOne = txtAryOne.map(sentence => sentence.trim());
txtAryOne = [...new Set(txtAryOne)]; // array unique, https://stackoverflow.com/a/14438954
let filenameSplit = fnOne.split('.');
let fn = filenameSplit[0] + '_unique.' + filenameSplit[1];
let err = fs.writeFileSync(fn, txtAryOne.join('\n'));
if (err)
console.log(err);
else
console.log("file saved as " + fn);
} break;
case '-s': {
// sort
let txtAryOne = txtOne.split('\n');
txtAryOne = txtAryOne.filter(sentence => sentence); // trim empty lines
txtAryOne = txtAryOne.map(sentence => sentence.trim());
txtAryOne = [...new Set(txtAryOne)]; // array unique, https://stackoverflow.com/a/14438954
let txtAryShuffle = txtAryOne.sort();
let filenameSplit = fnOne.split('.');
let fn = filenameSplit[0] + '_sort.' + filenameSplit[1];
let err = fs.writeFileSync(fn, txtAryShuffle.join('\n'));
if (err)
console.log(err);
else
console.log("file saved as " + fn);
} break;
case '-f': {
// shuffle
let txtAryOne = txtOne.split('\n');
txtAryOne = txtAryOne.filter(sentence => sentence); // trim empty lines
txtAryOne = txtAryOne.map(sentence => sentence.trim());
txtAryOne = [...new Set(txtAryOne)]; // array unique, https://stackoverflow.com/a/14438954
let txtAryShuffle = arrayShuffle(txtAryOne);
let filenameSplit = fnOne.split('.');
let fn = filenameSplit[0] + '_shuffle.' + filenameSplit[1];
let err = fs.writeFileSync(fn, txtAryShuffle.join('\n'));
if (err)
console.log(err);
else
console.log("file saved as " + fn);
} break;
}
return;