-
Notifications
You must be signed in to change notification settings - Fork 10
/
ai_extended_logic.class.php
339 lines (267 loc) · 10.3 KB
/
ai_extended_logic.class.php
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Class ai_extended_logic
*
* Public logic class for https://MBTI.ai
*
* @package conversionLogic
*/
class ai_extended_logic extends logic_calculation_data
{
/**
* Check if trait value is associated
* with a negative or positive light and
* return new calculated values.
* @param int $value Trait score
* @param int $rating User answer 1-5
* @param bool $no Is it for a
* negative value? I.e. If NO is true
* then the value is associated with NO
* and not YES.
* @return int New total.
*/
protected function convertTotal($value, $rating, $no = false)
{
$return = 0;
if (true === $no) {
switch ($rating) {
case 1:
$return = $value;
break;
case 2:
$return = $value / 2;
break;
}
} else {
switch ($rating) {
case 4:
$return = $value / 2;
break;
case 5:
$return = $value;
break;
}
}
return $return;
}
/**
* We receive a rating 1-5 for the question along with
* the traits associated with said question. We need
* to manipulate the traits to match what results have
* been decided.
*
* @param int $rating Value 1-5 from user answer
* @param array $traits List of traits & their values
* Array (
* [ti] => Array ( [value] => 100 )
* [se] => Array ( [value] => 100, [no] => true )
* )
* @return array List of traits, corrected for answer
*/
protected function calculateQuestionTraits($rating, $traits)
{
if ( ! empty($traits) ) {
foreach ($traits as $personality => $var) {
$traits[$personality]['value'] = $this->convertTotal($var['value'], $rating, isset($var['no']) && true === $var['no']);
}
}
return $traits;
}
/**
* Step 1: Order the array using score values from test results.
* (Highest score for e/i [i.e. ne/ni] is the conscious result)
* (Lowest score for e/i [i.e. se/si] is the unconscious result)
*
* @param array $dataset = Array
* (
* [f] => Array
* (
* [conscious_trait] => fi
* [conscious_value] => 2065
* [shadow_trait] => fe
* [shadow_value] => 1740
* )
*
* [n] => Array
* (
* [conscious_trait] => ni
* [conscious_value] => 2000
* [shadow_trait] => ne
* ...
*
* Step 2 [conscious division only]: Swap parent with child if conflicting with hero.
*
*/
public static function functionOrder($dataset, $type = 'conscious')
{
// Step 1
$order = $labels = array();
$labels['conscious'] = array('inferior', 'child', 'parent', 'hero');
$labels['shadow'] = array('demon', 'trickster', 'critic', 'nemesis');
// Order by array value
usort($dataset, function ($a, $b) use (&$type) { return strnatcmp($a[$type . '_value'], $b[$type . '_value']); });
foreach ($dataset as $key => $var) {
$order[ $labels[$type][$key] ]['trait'] = $var[$type . '_trait'];
$order[ $labels[$type][$key] ]['value'] = $var[$type . '_value'];
}
// Step 2
if ($type == 'conscious') {
$matches = false;
foreach (self::$personality_traits as $personality_type => $functions) {
// $functions[0] is hero, $functions[2] is child.
if ($order['hero']['trait'] == $functions[0] && $order['parent']['trait'] == $functions[2]) {
$matches = $personality_type;
}
}
if ($matches != false) {
// Swap parent with child
$original_order = $order;
$order['parent'] = $original_order['child'];
$order['child'] = $original_order['parent'];
}
}
return $order;
}
/**
* Check value exists and decide
* whether function is a conscious
* or shadow division
*
* @param array $traits List of traits:
* Array
* (
* [fe] => 149
* [fi] => 625
* [ne] => 1574
* [ni] => 1387
* [se] => 500
* [si] => 1037
* [te] => 724
* [ti] => 1687
* )
* @param string $function_e extraverted function name
* @param string $function_i introverted function name
* @return array division predictions
*
*/
public static function divisionPrediction($trait_data, $function_e, $function_i)
{
if (isset($trait_data[$function_e]) && ! empty($trait_data[$function_e])) {
// Exists
} else {
$trait_data[$function_e] = 0;
}
if (isset($trait_data[$function_i]) && ! empty($trait_data[$function_i])) {
// Exists
} else {
$trait_data[$function_i] = 0;
}
return array(
'conscious_trait' => ($trait_data[$function_e] > $trait_data[$function_i] ? $function_e : $function_i),
'conscious_value' => ($trait_data[$function_e] > $trait_data[$function_i] ? $trait_data[$function_e] : $trait_data[$function_i]),
'shadow_trait' => ($trait_data[$function_e] > $trait_data[$function_i] ? $function_i : $function_e),
'shadow_value' => ($trait_data[$function_e] > $trait_data[$function_i] ? $trait_data[$function_i] : $trait_data[$function_e]),
);
}
/**
* Try and predict the personality
* from the traits given from our
* questions.
*
* @param array $traits List of traits:
* Array
* (
* [fe] => 149
* [fi] => 625
* [ne] => 1574
* [ni] => 1387
* [se] => 500
* [si] => 1037
* [te] => 724
* [ti] => 1687
* )
* @return array Personality predictions
*/
public static function predictPersonality($traits, $log = true)
{
arsort($traits);
$data = array();
$data['debug'] = array();
$data['predictions'] = self::$personality_defaults;
$data['ordered'] = array(
'f' => self::divisionPrediction($traits, 'fe', 'fi'),
'n' => self::divisionPrediction($traits, 'ne', 'ni'),
's' => self::divisionPrediction($traits, 'se', 'si'),
't' => self::divisionPrediction($traits, 'te', 'ti')
);
$data['order_conscious'] = self::functionOrder($data['ordered'], 'conscious');
$data['order_shadow'] = self::functionOrder($data['ordered'], 'shadow');
// Below is where the personality matches are found.
// self::$personality_traits = Array
// (
// [ESTJ] => Array
// (
// [0] => te
// [1] => si
// [2] => ne
// [3] => fi
// [4] => ti
// [5] => se
// [6] => ni
// [7] => fe
// )
// [ISTJ] => Array
// (
// [0] => si
// [1] => te
// [2] => fi
// [3] => ne
// ...
foreach (self::$personality_traits as $personality_acronym => $function) {
if ($data['order_conscious']['hero']['trait'] == $function[0]) {
$data['predictions'][$personality_acronym]++;
$data['debug'][] = 'Matching ' . htmlentities($personality_acronym) . ' high conscious trait.';
}
if ($data['order_conscious']['parent']['trait'] == $function[1]) {
$data['predictions'][$personality_acronym]++;
$data['debug'][] = 'Matching ' . htmlentities($personality_acronym) . ' second highest conscious trait.';
}
if ($data['order_conscious']['child']['trait'] == $function[2]) {
$data['predictions'][$personality_acronym]++;
$data['debug'][] = 'Matching ' . htmlentities($personality_acronym) . ' second lowest conscious trait.';
}
if ($data['order_conscious']['inferior']['trait'] == $function[3]) {
$data['predictions'][$personality_acronym]++;
$data['debug'][] = 'Matching ' . htmlentities($personality_acronym) . ' lowest conscious trait.';
}
if ($data['order_shadow']['nemesis']['trait'] == $function[4]) {
$data['predictions'][$personality_acronym]++;
$data['debug'][] = 'Matching ' . htmlentities($personality_acronym) . ' high unconscious trait.';
}
if ($data['order_shadow']['critic']['trait'] == $function[5]) {
$data['predictions'][$personality_acronym]++;
$data['debug'][] = 'Matching ' . htmlentities($personality_acronym) . ' second highest unconscious trait.';
}
if ($data['order_shadow']['trickster']['trait'] == $function[6]) {
$data['predictions'][$personality_acronym]++;
$data['debug'][] = 'Matching ' . htmlentities($personality_acronym) . ' second lowest unconscious trait.';
}
if ($data['order_shadow']['demon']['trait'] == $function[7]) {
$data['predictions'][$personality_acronym]++;
$data['debug'][] = 'Matching ' . htmlentities($personality_acronym) . ' lowest unconscious trait.';
}
// Further logic to calculate results is top-heavy. Code is conscious. Personalities are subconscious.
}
arsort($data['predictions']);
if ($log === true && function_exists('the_logger')) {
global_logger('Results: ' . print_r($data['predictions'], true));
}
$data['further_predictions'] = self::furtherPredictions($data);
return $data;
}
public static function furtherPredictions($data)
{
// self::$trait_relation
return [];
}
}