-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSBM.js
235 lines (166 loc) · 9.72 KB
/
OSBM.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
/**
This project is originally made by me(N Paul).My github profile https: //github.com/nirmalpaul383/ My youtube page https://www.youtube.com/channel/UCY6JY8bTlR7hZEvhy6Pldxg/
This is an open source program. You are welcomed to modify it...If you want to support me please give a like to our facebook page https://facebook.com/a.new.way.Technical/
OSBM(Option Selector based on the Best Matching) by N Paul. This project is originally made by me (N Paul) (https://github.com/nirmalpaul383).
You can download source files from my github profile https://github.com/nirmalpaul383/OSBM/ .
If you like these project please give a star to these projects. https://github.com/nirmalpaul383/OSBM/
My YouTube Page: https://www.youtube.com/channel/UCY6JY8bTlR7hZEvhy6Pldxg/
FaceBook Page: https://www.facebook.com/a.New.Way.Technical/
GitHub Page: https://github.com/nirmalpaul383
**/
// Class definition for OSBM(Option Selector based on the Best Matching)
class OSBM {
// Function(Method) definition for storing training dataset to the memory property of the OSBM object
train(trainingDataSet) {
// Here "this" means OSBM class 's object
this.memory = trainingDataSet;
}
// Function(Method) definition for testing whether two arrays are comparable or not
isCompararableOrNot(traning, actual) {
// For storing the test result in True or False
let YesOrNo = true;
// This loop will be applied to each element of the traning array(1 level deep).
traning.forEach((upperValue, upperIndex) => {
// This will compare the length of the dataset array of each element of the training array with the length of the actual array (1 level deep).
if (upperValue.dataSet.length != actual.length) {
// Result will be set to false if any mis match occures.
YesOrNo = false;
}
else {
// This loop will be applied to each element of the each element of the traning array(2 level deep).
upperValue.dataSet.forEach((lowerValue, lowerIndex) => {
// This will compare the length of each element of the dataset array of each element of the training array with the length of each element of the actual array (2 Level deep).
if (lowerValue.length != actual[lowerIndex].length) {
// Result will be set to false if any mis match occures.
YesOrNo = false;
}
})
};
})
// Returns the result
return YesOrNo;
};
//Function(Method) for calculating the mis matched value between the two 2d arrays and returns difference value
misMatchedCal(input1, input2) {
// For temporay storing difference(misMatch value) betwwen same positioned element in two 2D arrays
let temp = 0;
//This loop will be applied to each element of the input array(1 level deep).
input1.forEach((upperValue, upperIndex) => {
//This loop will be applied to each element of the each element of the input array(2 level deep).
input1[upperIndex].forEach((lowerValue, lowerIndex) => {
// We need the sum of the absolute differences between the values of the same positioned elements of the two 2D arrays
temp += Math.abs(input1[upperIndex][lowerIndex] - input2[upperIndex][lowerIndex])
})
})
return temp; //Returns the mismatch value of input1 array and input2 array
};
// Function(Method) for comparing actual data set with all available training dataset
run(actual) {
// For storing OSBM 's memory to the "traning" variable
let traning = this.memory;
// If the value of traning variable is a falsy(e.g undefined) value then the function will not be executed any further and the user will be shown the related message.
if (!(traning)) {
return `To use OSBM, you must first provide a training dataset`
}
// If the size of the training data set and the actual data set are different, the function will not be executed any further and the user will be shown the related message.
// Here "this" means OSBM Class
if (!((this.isCompararableOrNot(traning, actual)))) {
return `Training and actual DataSet size should be the same length`;
};
// After comparing each training data sets with the actual data set, this array will be used to store the results(mis Matched values) in that order respectively
let misMatchValue = [];
// For storing complement value of the misMatchValue
let complementryValue = [];
// For storing result: Matching option(in %) with restpect to other avilible option(s)
let result_wrtOption = {};
// For storing result: Option wise matching (in %) with restpect to actual input
let result_wrtActual = {};
// For storing all results: result_wrtOption , result_wrtActual
let result = {};
// This value decides whether a further percentage calculation process will continue for wrtOption 's result or not.
let wrtOptionFurther = false;
// For comparing each training data sets with the actual data set and storing the results(misMatchedValues) in misMatchValue array(in the comparing order respectivly)
traning.forEach((value, index) => {
misMatchValue[index] = this.misMatchedCal(traning[index].dataSet, actual);
});
// For storing sum value of all mis match value
let sumMisMatch = 0;
misMatchValue.forEach((value, index) => {
sumMisMatch += value;
});
// For calculating and storing complementery value of the misMatchValue
misMatchValue.forEach((value, index) => {
complementryValue[index] = sumMisMatch - value
});
// For storing sum value of all complementery value
let sumComplementery = 0;
complementryValue.forEach((value, index) => {
sumComplementery += value;
});
// For calculating option wise matching % with respect to actual input
// For calculating avialible 2D Array length
let avilibleLength2D = traning[0].dataSet.length * traning[0].dataSet[0].length;
traning.forEach((value, index) => {
// For temporay storing output label
let temp = traning[index].output;
// For temporary storing the calulation result
let temp1 = ((avilibleLength2D - misMatchValue[index]) / avilibleLength2D) * 100;
// For storing option wise matching % with respect to actual input
// If there is any previous matching % in the head of the same label(Output/Option), then the max value(Maximum of old matching % & new matching %) will be recorded in that label(Output/Option).
// If result[temp] is truthy value only then this if condition will be satisfied(Note: undefined is a falsy value)
if (result_wrtActual[temp]) {
result_wrtActual[temp] = Math.max(result_wrtActual[temp], temp1);
}
// Otherwise a new label(Output/Option) head will be created and the result will be kept in it
else {
result_wrtActual[temp] = temp1;
};
})
// For calculating appropiate option in compare with all options and storing the result according to the output label
traning.forEach((value, index) => {
// For temporay storing output label
let temp = traning[index].output;
// For temporary storing the calulation result
let temp1 = 0;
// If the value of the sumComplementary is zero, then 100(%) will be divided equally in all the options.
// For preventing divide by 0 situation
if (sumComplementery == 0) {
temp1 = 100 / traning.length;
}
else {
temp1 = (complementryValue[index] / sumComplementery) * 100;
}
// If there is any previous result in the head of the same label(Output/Option), then the max value will be recorded in that label(Output/Option) for further processing.
// If result[temp] is truthy value only then this if condition will be satisfied(Note: undefined is a falsy value)
if (result_wrtOption[temp]) {
result_wrtOption[temp] = Math.max(result_wrtOption[temp], temp1);
// If there is any previous result, only then this variable 's value will be true. This value decides whether a further percentage calculation process will continue for this result or not.
wrtOptionFurther = true;
}
// Otherwise a new label(Output/Option) head will be created and the result will be kept in it
else {
result_wrtOption[temp] = temp1;
}
});
// If there are more than one option with the same name/output/label, only then further percentage will be calculated for wrtOption
// If wrtOptionFurther is truthy value only then this if condition will be satisfied(Note: undefined is a falsy value)
if (wrtOptionFurther) {
// For storing sum value of all element's value from wrtoption
let sumOfwrtOptionV = 0;
// For summing up all elements's value from wrtOption Object
for (let tempIndex in result_wrtOption) {
sumOfwrtOptionV += result_wrtOption[tempIndex];
};
// For calculating new(further) percentage value for each element of wrtOption and for setting(storing) all those value in each element.
for(let tempIndex1 in result_wrtOption) {
// Here first "result_wrtOption[tempIndex1]" indicates for accessing wrtOption node (new value), where second "result_wrtOption[tempIndex1]" indicates the old value of this node
result_wrtOption[tempIndex1] = result_wrtOption[tempIndex1] / sumOfwrtOptionV * 100;
};
}
// For storing all results: result_wrtOption , result_wrtActual into result object
result['wrtOption'] = result_wrtOption;
result['wrtActual'] = result_wrtActual;
// Returns the result object
return result;
};
};