-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
231 lines (193 loc) · 7.36 KB
/
main.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
const nwPath = require("path");
const fs = require("fs");
const csv = require('csv-parser');
const origText = "materials/orig-test.txt";
const translatedText = "materials/translated-test.txt";
// result file
const resultOrigFile = "materials/result-orig.txt";
const resultTransFile = "materials/result-translated.txt";
// dictionary file, containing japanese to english value in csv
const dictionary = "materials/dict.csv";
// will the generator also create a copy of the unmodified texts?
const cloneUnmodifiedSentence = false;
const config = [
{
markers : ["\ue101", "\ue102", "\ue103"],
afterSentence : true,
beforeSentence : true,
midSentence : true,
placement : "after",
afterSentenceChance : 100, // % chance integer
beforeSentenceChance : 100, // % chance integer
midSentenceChance : 50 // % chance integer
}
]
const readCsv = async function(file) {
var csvData=[];
return new Promise((resolve, reject) => {
fs.createReadStream(file)
.pipe(csv())
.on('data', function(data){
try {
csvData.push(data);
} catch(err) {
//error handler
}
})
.on('end',function(){
resolve(csvData);
});
})
}
function chance(percent=100) {
return ((Math.random()*100) <= percent)
}
function getCombinations(valuesArray = []) {
var combi = [];
var temp = [];
var slent = Math.pow(2, valuesArray.length);
for (var i = 0; i < slent; i++)
{
temp = [];
for (var j = 0; j < valuesArray.length; j++)
{
if ((i & Math.pow(2, j)))
{
temp.push(valuesArray[j]);
}
}
if (temp.length > 0)
{
combi.push(temp);
}
}
combi.sort((a, b) => a.length - b.length);
//console.log(combi.join("\n"));
return combi;
}
function getAllCombination(item, n) {
const filter = typeof n !=='undefined';
n = n ? n : item.length;
const result = [];
const isArray = item.constructor.name === 'Array';
const count = isArray ? item.length : item;
const pow = (x, n, m = []) => {
if (n > 0) {
for (var i = 0; i < count; i++) {
const value = pow(x, n - 1, [...m, isArray ? item[i] : i]);
result.push(value);
}
}
return m;
}
pow(isArray ? item.length : item, n);
return filter ? result.filter(item => item.length == n) : result;
}
const countOccurance = function(haystack, needle) {
return haystack.split(needle).length -1
}
const countOccuranceArray = function(haystack, needle) {
var count = 0;
for (var i in haystack) {
if (haystack[i] == needle) count++
}
return count;
}
const countOccuranceEn = function(haystack, needle) {
if (needle.split(" ") > 1) {
return countOccurance(haystack.toLowerCase(), needle.toLowerCase());
}
else return countOccuranceArray(splitByCommonToken(haystack.toLowerCase()), needle.toLowerCase());
}
const isMatchInsensitive = function(haystack, needle) {
haystack = haystack.toLowerCase();
if (needle.split(" ")>1) {
return haystack.toLowerCase().includes(needle.toLowerCase());
}
return splitByCommonToken(haystack).includes(needle.toLowerCase())
}
const splitByCommonToken = function(str="") {
return str.split(/[ \-\.\,\[\]\/\"]/);
}
String.prototype.replaceAllInsensitive = function(strReplace, strWith) {
// See http://stackoverflow.com/a/3561711/556609
var esc = strReplace.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
var reg = new RegExp(esc, 'ig');
return this.replace(reg, strWith);
};
const clone = function(obj) {
return JSON.parse(JSON.stringify(obj))
}
void async function() {
var dict = await readCsv(dictionary);
console.log(dict);
var origTextContent = await fs.promises.readFile(origText);
origTextContent = origTextContent.toString().trim();
var origTextContents = origTextContent.replaceAll("\r", "").split("\n")
var transTextContent = await fs.promises.readFile(translatedText);
transTextContent = transTextContent.toString().trim();
var transTextContents = transTextContent.replaceAll("\r", "").split("\n")
if (origTextContents.length !== transTextContents.length) return console.error("Number of line between original text and translated texts are not same!", origTextContents.length, transTextContents.length);
// write original text
var resultOrig = []
var resultTrans = []
if (cloneUnmodifiedSentence) {
resultOrig = clone(origTextContents);
resultTrans = clone(transTextContents);
}
const assignBeforeSentence = async function(config) {
if (!config.beforeSentence) return;
for (var i in origTextContents) {
if (!chance(config.beforeSentenceChance)) continue;
for (var m in config.markers) {
resultOrig.push(config.markers[m]+origTextContents[i]);
resultTrans.push(config.markers[m]+transTextContents[i]);
}
}
}
const assignAfterSentence = async function(config) {
if (!config.afterSentence) return;
for (var i in origTextContents) {
if (!chance(config.afterSentenceChance)) continue;
for (var m in config.markers) {
resultOrig.push(origTextContents[i]+config.markers[m]);
resultTrans.push(transTextContents[i]+config.markers[m]);
}
}
}
const assignTranslation = async function(config) {
if (!config.midSentence) return;
for (var i in origTextContents) {
//lookup original text
for (var x in dict) {
if (!(origTextContents[i].includes(dict[x].original) && isMatchInsensitive(transTextContents[i], dict[x].translation.toLowerCase()))) continue;
if (countOccurance(origTextContents[i], dict[x].original) != countOccuranceEn(transTextContents[i], dict[x].translation)) continue;
if (!chance(config.midSentenceChance)) continue;
for (var m in config.markers) {
var replacedOrig = origTextContents[i].replaceAll(dict[x].original, dict[x].original+config.markers[m]);
var replacedTrans = transTextContents[i].replaceAllInsensitive(dict[x].translation, dict[x].translation+config.markers[m]);
resultOrig.push(replacedOrig);
resultTrans.push(replacedTrans);
}
}
}
}
const assignTokens = async function(config) {
var combinations = getAllCombination(config.markers);
for (var c in combinations) {
resultOrig.push(combinations[c].join(""));
resultTrans.push(combinations[c].join(""));
}
}
for (var i in config) {
await assignBeforeSentence(config[i]);
await assignAfterSentence(config[i]);
await assignTranslation(config[i]);
await assignTokens(config[i]);
}
await fs.promises.writeFile(resultOrigFile, resultOrig.join("\n"));
await fs.promises.writeFile(resultTransFile, resultTrans.join("\n"));
console.log("completed!");
console.log("Modified original data is :", resultOrigFile);
console.log("Modified Translated data is :", resultTransFile);
}();