-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateTrainingData.js
More file actions
134 lines (124 loc) · 4.87 KB
/
createTrainingData.js
File metadata and controls
134 lines (124 loc) · 4.87 KB
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
const fs = require('fs');
const dictionaryFile = process.argv[2] || './preferred-stock.json';
const readDictionary = () => new Promise((resolve, reject) => {
fs.readFile(dictionaryFile, (err, data) => {
if (err) {
reject(err)
}
resolve(JSON.parse(data))
});
});
const readTextFile = fileName => new Promise((resolve, reject) => {
fs.readFile(fileName, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
})
});
const readTextFiles = () => new Promise((resolve, reject) => {
fs.readdir('./init_docs/text', async (err, data) => {
try {
if (err) {
throw err;
}
const textContent = await Promise.all(data.map(async f => {
const path = `./init_docs/text/${f}`;
return (await readTextFile(path)).toString();
}));
resolve(textContent.join('\n'));
} catch(error) {
reject(error);
}
});
});
function getIndicesOf(searchStr, str, caseSensitive = false) {
var searchStrLen = searchStr.length;
if (searchStrLen == 0) {
return [];
}
var startIndex = 0, index, indices = [];
if (!caseSensitive) {
str = str.toLowerCase();
searchStr = searchStr.toLowerCase();
}
while ((index = str.indexOf(searchStr, startIndex)) > -1) {
indices.push(index);
startIndex = index + searchStrLen;
}
return indices;
}
(async function() {
try {
const { entities } = await readDictionary();
const rawText = await readTextFiles();
const rawTextSplit = rawText.split("\n");
for (const entity of entities) {
const { id, normalizedText } = entity;
const label = id.replace(/[-]/g, "_");
const ents = [];
for (const nText of normalizedText) {
let matches = rawTextSplit.map(text => {
let textToCheck = nText.split("...");
const containsGap = textToCheck.length > 1;
const target = containsGap
? textToCheck.filter(t => t.indexOf("[") > -1)[0].replace(/[^A-Z\s/]/gi, '') : textToCheck[0];
if (containsGap) {
textToCheck = textToCheck.map(t => t.replace(/[^A-Z\s/]/gi, ''));
}
const remaining = textToCheck.filter(nt => text.toLowerCase().indexOf(nt.toLowerCase()) > -1);
if (textToCheck.length === remaining.length) {
if (containsGap) {
const fidxs = getIndicesOf(textToCheck[0], text)
const lidxs = getIndicesOf(textToCheck[1], text).map(i => i + textToCheck[1].length)
const idxs = fidxs.map((fi, i) => lidxs[i] && fi < lidxs[i] ? [fi, lidxs[i]] : null).filter(i => !!i);
let found = idxs.map(idxSet => {
return text.substring(...idxSet)
});
found = found.map((t, i) => {
const tidx = t.toLowerCase().indexOf(target.toLowerCase());
const idxs = [tidx, tidx + target.length]
return {
t,
idxs: [idxs]
}
});
if (found.length) {
return { found };
}
} else {
const idxs = getIndicesOf(textToCheck[0], text).map(i => [i, i + target.length]);
return { t: text, idxs };
}
}
return null;
});
const _matches = matches.slice();
_matches.forEach((m, i) => {
if (m && Object.keys(m).indexOf('found') > -1) {
matches.push(...m.found);
matches[i] = null;
}
});
matches = matches.filter(m => !!m);
// console.log(matches);
if (matches.length) {
ents.push(...matches);
}
}
if (ents.length) {
const filename = `./training-data/${label}.json`;
fs.writeFile(filename, JSON.stringify({
entities: ents
}, null, 2), (err) => {
if (err) {
throw err;
}
console.log(`Generated ${filename}`);
});
}
}
} catch(err) {
console.log(err)
}
})()