-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrokeAnalysis.js
272 lines (208 loc) · 8.58 KB
/
strokeAnalysis.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const strokeInfoDict = require('./res/strokeInfo')
const Heap = require('./Heap')
class Node {
constructor(last = null, gene = []) {
this.last = last;
this.gene = gene;
}
isLegalToAppend(value) {
return value > this.last;
}
append(value) {
this.last = value;
this.gene.push(value);
}
}
class Vector {
constructor(value = []) {
this.value = value;
}
normalized() {
return this.value.map(value => value / this.sum());
}
unified() {
return this.value.map(value => value / this.norm());
}
norm() {
let normSquared = 0;
for (let i = 0; i < this.value.length; i++) {
normSquared += Math.pow(this.value[i], 2);
}
return Math.sqrt(normSquared);
}
sum() {
let sum = 0;
for (let i = 0; i < this.value.length; i++) {
sum += this.value[i];
}
return sum;
}
}
class StrokeAnalysis {
constructor() {
this.characters = Object.keys(strokeInfoDict);
}
// find first 200 most similar substructures
// return array of potential characters in descending order and array of negative
search(refCharacter, refPositions) {
return this.searchWithRange(refCharacter,refPositions,this.characters);
}
searchWithRange(refCharacter, refPositions, characters) {
let potentialHeap = new Heap((a, b) => a[1] - b[1]);
let negative = [];
let refIndex = strokeInfoDict[refCharacter].index;
let substructure = refPositions.map(value => refIndex[value]).join('');
characters.forEach(character => {
if (!this.isPotential(substructure, strokeInfoDict[character].index)) {
negative.push(character);
} else {
let diff = this.computeSmallestDifference(refCharacter, refPositions, character);
diff > 4 ? negative.push(character) : potentialHeap.push([character, diff]);
}
})
let potential = [];
for (let i = 0; potential.push(potentialHeap.pop()[0]) < 200; i++) {
}
return [potential, negative]
}
// **** Above are public apis ***
// *** stroke order analysis ***
// find all positions in string of pattern reg
regExpFindAll(reg, string) {
let m;
let matches = [];
while (m = (reg.exec(string))) {
matches.push(m.index);
}
return matches;
}
// whether a stroke sequence is potential of having certain substructure on stroke stroke order level
isPotential(substructure, strokeSequence) {
substructure = Array.from(substructure).map(value => `\\d*${value}+`).join('') + '\\d*';
let reg = new RegExp(substructure, 'g');
return strokeSequence.match(reg) != null;
}
// 5 arrays of positions of 1,2,3,4,5
basicStrokesPositionsOf(strokeSequence) {
let result = [];
while (result.push(0) < 5) {
}
result = result.map((value, index) => {
return this.regExpFindAll(new RegExp(index + 1, 'g'), strokeSequence)
});
return result;
}
computeChunkNum(positions) {
let chunkNum = 1;
for (let i = 0; i < positions.length - 1; i++) {
if (positions[i + 1] - positions[i] > 1) {
chunkNum += 1;
}
}
return chunkNum;
}
// look for possible substructure stroke positions in stroke sequence
// search in stroke-wise way
strokePositionsOf(substructure, strokeSequence) {
let basicStrokePositions = this.basicStrokesPositionsOf(strokeSequence);
// record first stroke positions
let thisStrokePositions = basicStrokePositions[parseInt(substructure[0]) - 1];
let thisNodeList = thisStrokePositions.map(value => new Node(value, [value]));
// look for other strokes
for (let i = 1; i < substructure.length; i++) {
let nextStrokePositions = basicStrokePositions[parseInt(substructure[i]) - 1];
let nextNodeList = [];
// loop for each possible connection
for (let j = 0; j < thisNodeList.length; j++) {
let node = thisNodeList[j];
for (let k = 0; k < nextStrokePositions.length; k++) {
let position = nextStrokePositions[k];
if (node.isLegalToAppend(position)) {
let validNode = new Node(node.last, node.gene.slice());
validNode.append(position);
nextNodeList.push(validNode);
}
}
}
thisNodeList = nextNodeList;
}
return thisNodeList.map(node => node.gene);
}
// *** histogram analysis ***
pickHistogramInfo(histograms, positions) {
let strokeWiseHistograms = positions.map(value => histograms[value]);
let overallHistogram = [];
for (let i = 0; overallHistogram.push(0) < 4; i++) {
}
for (let i = 0; i < strokeWiseHistograms.length; i++) {
for (let j = 0; j < 4; j++) {
overallHistogram[j] += strokeWiseHistograms[i][j];
}
}
strokeWiseHistograms = strokeWiseHistograms.map(value => {
let v = new Vector(value);
return v.normalized();
})
let v = new Vector(overallHistogram);
overallHistogram = v.unified();
return [strokeWiseHistograms, overallHistogram]
}
// *** trajectory analysis ***
pickTrajectoryInfo(centroidLocations, positions) {
let trajs = [];
for (let i = 0; i < positions.length - 1; i++) {
trajs.push([centroidLocations[positions[i + 1]][0] - centroidLocations[positions[i]][0],
centroidLocations[positions[i + 1]][1] - centroidLocations[positions[i]][1]])
}
trajs = trajs.map(value => Math.acos(new Vector(value).unified()[0]));
return trajs;
}
// *** difference computation ***
computeDifference(refInfo, info, positions) {
let [refHists, refOverallHist, refTraj] = refInfo;
let [hists, overallHist, traj] = info;
// stroke-wise hist diff
let histsDeviation = hists.map((value1, index1) => {
return value1.map((value2, index2) => value2 - refHists[index1][index2]);
}).map(value => new Vector(value).norm() / 2);
let histsDiff = histsDeviation.reduce((a, b) => a + b);
// overall hist diff
let overallHistDeviation = overallHist.map((value, index) => (value - refOverallHist[index]) / 2);
let overallHistDiff = new Vector(overallHistDeviation).norm();
// traj diff
let trajDiff = 1;
if (traj.length > 2) {
let trajDeviation = traj.map((value, index) => value - refTraj[index]);
let power = traj.map((value, index) => positions[index + 1] - positions[index]);
trajDiff = trajDeviation.map((angle, index) => Math.pow(Math.abs(Math.tan(angle / 2)) + 1, power[index]));
trajDiff = trajDiff.reduce((a, b) => a * b);
}
// add all
let chunkNum = this.computeChunkNum(positions);
let diff = overallHistDiff * histsDiff * trajDiff * Math.pow(chunkNum, chunkNum);
return diff;
}
// smallest of all possibilities
computeSmallestDifference(refc, refPositions, c) {
let [refIndex, refHist, refTraj] = [strokeInfoDict[refc].index, strokeInfoDict[refc].hist, strokeInfoDict[refc].traj];
let [index, hists, trajs] = [strokeInfoDict[c].index, strokeInfoDict[c].hist, strokeInfoDict[c].traj];
let substructure = refPositions.map(value => refIndex[value]).join('');
let [refStrokeHists, refOverallHists] = strokeAnalysis.pickHistogramInfo(refHist, refPositions);
refTraj = strokeAnalysis.pickTrajectoryInfo(refTraj, refPositions);
let refInfo = [refStrokeHists, refOverallHists, refTraj];
// go through all possibilities
let potentialPositions = this.strokePositionsOf(substructure, index);
let smallestDiff = 10000;
potentialPositions.forEach(positions => {
let [strokeHists, overallHist] = strokeAnalysis.pickHistogramInfo(hists, positions);
let pickedTrajs = strokeAnalysis.pickTrajectoryInfo(trajs, positions);
let info = [strokeHists, overallHist, pickedTrajs];
let diff = this.computeDifference(refInfo, info, positions);
if (diff < smallestDiff) {
smallestDiff = diff;
}
})
return smallestDiff;
}
}
module.exports = StrokeAnalysis;