-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidationController.php
109 lines (88 loc) · 3.87 KB
/
ValidationController.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
<?php
namespace App\Http\Controllers;
use App\validation as Validation;
use Illuminate\Http\Request;
class ValidationController extends Controller
{
public function InvokeJS(Request $request)
{
$words = $request->word;
$routez = route('kunz.ml.proccess');
?>
<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script src="<?php echo asset('js/app.js'); ?>"></script>
<script>
$(document).ready(function () {
const threshold = 0.9;
toxicity.load(threshold).then(model => {
const sentences = ['$words'];
model.classify(sentences).then(predictions => {
// $('#result').html(JSON.stringify(predictions));
var sizeOfRequestPredictData = predictions.length;
var label =[];
var probability_0 =[];
var probability_1 =[];
var match =[];
for (let i = 0; i < sizeOfRequestPredictData; i++) //7
{
label.push([predictions[i]['label']]);
// probability_0.push((predictions[i]['results'][0]['probabilities'][0]));
// probability_1.push(predictions[i]['results'][0]['probabilities'][1]);
probability_0.push(Math.round(((predictions[i]['results'][0]['probabilities'][0]) * 100) * 1000)/1000);
probability_1.push(Math.round(((predictions[i]['results'][0]['probabilities'][1]) * 100) * 1000)/1000);
match.push(predictions[i]['results'][0]['match']);
}
for (var x in label) {
$('#result').append(label[x] +": " +
// probability_0[x] + ", "+
probability_1[x] +
// " = "+ match[x] +
"<br>");
};
});
});
});
</script>
<div id='result'>
</div>
<?php
}
public function process(Request $request)
{
$RequestPredictData = $request->PredictData;
$sizeOfRequestPredictData = sizeof($RequestPredictData);
$probability = [];
// loop starts
for ($i = 0; $i < sizeof($RequestPredictData); $i++) //7
{
for ($j = 0; $j < sizeof($RequestPredictData[$i]['results']); $j++) //1
{
$match['match'] = $RequestPredictData[$i]['results'][$j]['match'];
for ($k = 0; $k < sizeof($RequestPredictData[$i]['results'][$j]); $k++) //2
{
$probability['probability_' . $k] = array(
round($RequestPredictData[$i]['results'][$j]['probabilities'][$k] * 100, 2)
);
}
}
$labels[$RequestPredictData[$i]['label']] = array(
$probability,
$match
);
}
for ($i = 0; $i < $sizeOfRequestPredictData; $i++) {
$resp[] = $this->printData($labels, $RequestPredictData[$i]['label']);
}
return json_encode($resp);
}
public function printData($array, $label_name)
{
$final = $label_name . ': ';
$final .= json_encode($array[$label_name][0]['probability_0'][0]);
$final .= '%, ';
$final .= json_encode($array[$label_name][0]['probability_1'][0]);
$final .= '% <br>';
return $final;
}
}