-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfungsi.php
186 lines (168 loc) · 5.21 KB
/
fungsi.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
<?php
function connectDB ($db){
$server = "localhost";
$username = "root";
$password = "";
$connect = mysqli_connect($server,$username,$password,$db);
if (!$connect) {
die('Could not connect: ' . mysqli_error());
}
return $connect;
}
function praproses($input){
if (!empty($input)) {
//panggil fungsi prepos trainning pertanyaan
$stopWords = file_get_contents("term_stopwords_id.txt"); //Panggil File Stopwords
$pisahKalimat = pecahkalimat($input);
$caseFolding = caseFolding($pisahKalimat);
$tokenizing = tokenizing($caseFolding);
$stopwordsRemoved = stopwordsRemoval($stopWords,$tokenizing);
}
return $stopwordsRemoved;
}
function praproses_baru($input){
if (!empty($input)) {
//panggil fungsi prepos trainning pertanyaan
$stopWords = file_get_contents("term_stopwords_id.txt"); //Panggil File Stopwords
$pisahKalimat[0] = pecahkalimat3($input);
$caseFolding = caseFolding($pisahKalimat);
$tokenizing = tokenizing($caseFolding);
$stopwordsRemoved = stopwordsRemoval($stopWords,$tokenizing);
}
return $stopwordsRemoved;
}
function pecahkalimat($input){
//split konten per kalimat
$pisahKalimat = array();
$hasilPisah = array();
// $teks = str_replace(".", " ", $input);
// $pisahKalimat = preg_split("/[.!?]+/", $teks);
$pisahKalimat = preg_split("/[.!?]+/", $input);
//$pisahKalimat = preg_split("#\n#", $input);
$pisahKalimat = array_slice($pisahKalimat, 0, sizeof($pisahKalimat)-1); // buang array terakhir (kosong)
for ($i=0; $i < count($pisahKalimat); $i++) {
array_push($hasilPisah, $pisahKalimat[$i]);
}
return $pisahKalimat;
}
function pecahkalimat2($input){
foreach ($input as $key => $value) {
$pisahKalimat = preg_replace("/[.!?]+/"," ", $value);
$input[$key] = $pisahKalimat;
}
return $input;
}
function pecahkalimat3($input){
$pisahKalimat = preg_replace("/[.!?]+/"," ", $input);
return $pisahKalimat;
}
function caseFolding($pisahKalimat){
$caseFolding = array();
$caseFolding = array_map("strtolower", $pisahKalimat);
$caseFolding = preg_replace("/[\d\W]+/"," ", $caseFolding); //[\d] hapus angka [\W] hapus simbol
return $caseFolding;
}
function tokenizing($caseFolding){
$tokenizing = array();
$hasilTokenizing = array();
for ($i=0; $i <count($caseFolding) ; $i++) {
$tokenizing = preg_split("/[\s]+/", $caseFolding[$i]);
array_push($hasilTokenizing, $tokenizing);
}
//rapih rapih array
$hasilTokenizing = array_map('array_filter', $hasilTokenizing);
$hasilTokenizing = array_filter($hasilTokenizing);
$hasilTokenizing = array_map('array_values', $hasilTokenizing);
$hasilTokenizing = array_values($hasilTokenizing);
return $hasilTokenizing;
}
function stopwordsRemoval($stopWords,$tokenizing){
//pisah berdasarkan spasi
$stopwordsRemoved = array();
$hasilStopWordsRemoved = array();
$getstopWords = preg_split("/[\s]+/", $stopWords);
for ($i=0; $i < count($tokenizing); $i++) {
$stopwordsRemoved = array_diff($tokenizing[$i], $getstopWords);
$stopwordsRemoved = array_values($stopwordsRemoved); // perbaiki indeks
array_push($hasilStopWordsRemoved, $stopwordsRemoved);
}
//rapih rapih array
$hasilStopWordsRemoved = array_map('array_filter', $hasilStopWordsRemoved);
$hasilStopWordsRemoved = array_filter($hasilStopWordsRemoved);
$hasilStopWordsRemoved = array_map('array_values', $hasilStopWordsRemoved);
$hasilStopWordsRemoved = array_values($hasilStopWordsRemoved);
return $hasilStopWordsRemoved;
}
function textpreprocessing($isi) {
$HasilstopwordsRemoved = array();
//panggil fungsi prepos
$pisahKalimat = pecahkalimat($isi);
$caseFolding = caseFolding($pisahKalimat);
$tokenizing = tokenizing($caseFolding);
$HasilstopwordsRemoved = stopwordsRemoval($stopWords,$tokenizing);
return $HasilstopwordsRemoved;
}
function getFrequency($hasilStem){
$frequency = []; //array baru untuk tampung
foreach ($hasilStem as $key => $value) {
foreach ($value as $key2 => $value2) {
$needle = $value2; //key yang di cari
if (array_key_exists($needle, $frequency))
$frequency[$needle]++;
else
$frequency[$needle] = 1;
}
}
return $frequency;
}
function getDF($hasilStem){
$df = [];
$unique = array_map('array_unique', $hasilStem);
foreach ($unique as $key => $value) {
foreach ($value as $key2 => $value2) {
if(array_key_exists($value2, $df))
$df[$value2]++;
else
$df[$value2] = 1;
}
}
return $df;
}
function daftarKata($w){
$daftarKata = [];
foreach ($w as $key => $value) {
$daftarKata[] = $key;
}
return $daftarKata;
}
function createDummyMatrix($daftarKata,$hasilStem){
$matrixDummy = [];
for ($i=0; $i < count($hasilStem) ; $i++) {
for ($j=0; $j < count($daftarKata) ; $j++) {
$matrixDummy[$i][] = $daftarKata[$j];
}
}
for ($i=0; $i < count($matrixDummy); $i++) {
for ($j=0; $j < count($matrixDummy[$i]) ; $j++) {
if (!in_array($matrixDummy[$i][$j], $hasilStem[$i])) {
$matrixDummy[$i][$j] = 0;
}
}
}
return $matrixDummy;
}
function createMatrix($matrixDummy,$w,$daftarKata){
$matrix = [];
foreach ($matrixDummy as $key => $value) {
foreach ($value as $key2 => $value2) {
$dummy = $matrixDummy[$key][$key2];
if (in_array($dummy, $daftarKata))
$matrix[$key][$key2] = $w[$dummy];
//ubah null jadi 0
if (is_null($matrix[$key][$key2]))
$matrix[$key][$key2] = 0;
}
}
return $matrix;
}
?>