Jumat, 26 Juni 2020
PHP COde TFIDF + Naive Bayes
Proses DImulai dari
1.Buat datalatih Buku+Normaliasinya
Normalisasi meliputi: gabungan antara judul dan sinipsis/deskripsi
2.Input datauji + Normalisasinya
Normalisasi meliputi: gabungan antara judul dan sinipsis/deskripsi
Proses Normaliasi automatis terbentuk sat dokumen disimpan atau di ubah....
#Join Judul+Deskripsi -> CaseFolding -> Stopword - Stemming ->normalisasi
Selanjutnya dataUji akan di tokenisasi
Lanjut ke perhitungan Naive Bayes:
Dan Ini adalah contoh code implementasinya:
<style>
#table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
background-color:#ddd;
}
#table td, #table th {
border: 1px solid #696969;
padding: 8px;
}
#table th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #A9A9A9;
color: white;
}
</style>
<link rel="stylesheet" href="jsacor/jquery-ui.css">
<link rel="stylesheet" href="resources/demos/style.css">
<script src="jsacor/jquery-1.12.4.js"></script>
<script src="jsacor/jquery-ui.js"></script>
<script>
$( function() {
$( "#accordion" ).accordion({
collapsible: true
});
} );
</script>
<?php
$time_start = microtime(true);
//usleep(100);
$id_datauji=$_GET["id"];
$sql="select * from `$tb_datauji` where `id_datauji`='$id_datauji'";
$d=getField($conn,$sql);
$id_datauji=$d["id_datauji"];
$id_datauji0=$d["id_datauji"];
$judul=$d["judul"];
$ringkasan=$d["ringkasan"];
$jenis=$d["jenis"];
$normalisasi=$d["normalisasi"];
$class=$d["class"];
$dokumen=$d["dokumen"];
$dokumen0=$d["dokumen"];
$tanggal=WKT($d["tanggal"]);
$durasi=$d["durasi"];
$rekapitulasi=$d["rekapitulasi"];
$status=$d["status"];
$keterangan=$d["keterangan"];
//$normalisasi=getNorm($komentar0);
?>
<div id="accordion">
<h4>Info Data Pengujian</h4>
<div>
<table width="90%">
<tr>
<th width="166" ><label for="id_datauji">Id Pengujian</label>
<th width="19">:
<th width="397" colspan="2"><b><?php echo "<b>$id_datauji</b>";?></b>
</tr>
<tr>
<td valign="top"><label for="judul">Judul</label>
<td valign="top">:
<td valign="top"><?php echo "<b>$judul</b>";?></td>
</tr>
<tr>
<td valign="top"><label for="ringkasan">Ringkasan</label>
<td valign="top">:
<td valign="top"><?php echo $ringkasan;?></td>
</tr>
<tr>
<td valign="top"><label for="normalisasi">Normalisasi</label>
<td valign="top">:
<td valign="top"><?php echo $normalisasi;?></td>
</tr>
<tr>
<td><label for="penilaian">Jenis</label>
<td>:<td colspan="2"><?php echo $jenis;?>
</td></tr>
<tr>
<td height="24"><label for="keterangan">Keterangan</label>
<td>:<td colspan="2"><?php echo $keterangan;?></td></tr>
</table>
</div>
<?php
require_once __DIR__ . '/vendor/autoload.php';
error_reporting(0);
$initos = new \Sastrawi\Stemmer\StemmerFactory();
$bikinos = $initos->createStemmer();
$stemming=$bikinos->stem($normalisasi);
$stemmingnew=strtolower($stemming);
$ak=getStopNumber();
$ar=getStopWords();
$wordStop=$stemmingnew;
for($i=0;$i<count($ar);$i++){
$wordStop =str_replace($ar[$i]." ","", $wordStop);
}
for($i=0;$i<count($ak);$i++){
$wordStop =str_replace($ak[$i],"", $wordStop);
}
$juduluji=str_replace(" "," ", $wordStop);
//=====================================================
$stemming=$juduluji;//NORM
//=====================================================
$arAsli=explode(".",$stemming);
$jumk=count($arAsli);
$AR=explode(" ",$stemming);
$AR=array_unique($AR);
$m=0;
for($i=0;$i<count($AR);$i++){
if(strlen($AR[$i])>1){
$arUnix[$m]=$AR[$i];
$m++;
}
}
//======================================
$sql="select * from `$tb_datalatih` order by `id_datalatih`";// asc limit 0,3";
$arr=getData($conn,$sql);
$i=0;
$arStem[0]=$stemming;
$gabungan=$stemming." ";
foreach($arr as $d) {
$judul0=$d["judul"];
$id_kategori0=$d["id_datalatih"];
$kalimat=$d["normalisasi"];
$kalimat0=$d["normalisasi"];
$kategori0=$d["class"];
$arJ[$i]=$judul0;
$arKode[$i]=$id_kategori0;
$arKat[$i]=$kategori0;
$arAslidoc[$i]=$kalimat0;
//==================
$stemming2=$bikinos->stem($kalimat0);
$stemmingnew2=strtolower($stemming2);
$wordStop2=$stemmingnew2;
for($y=0;$y<count($ar);$y++){
$wordStop2 =str_replace($ar[$y]." ","", $wordStop2);
}
for($y=0;$y<count($ak);$y++){
$wordStop2 =str_replace($ak[$y],"", $wordStop2);
}
$kalimatfilter=str_replace(" "," ", $wordStop2);
//==================
$gabungan.=$kalimatfilter." ";
$arStem[$i+1]=$kalimatfilter;
$arDoc[$i]=$kalimatfilter;
$arKe[$i]="Data ke-".($i+1);
$TOT[$i]=0;
$i++;
}
$jumk=$i;
//======================================
$arAsli=explode(" ",$gabungan);
$arUnix0=array_unique($arAsli);
$ii=0;
for($i=0;$i<count($arUnix0);$i++){
if($arUnix0[$i]==""){}
else{
$arUnix[$ii]=$arUnix0[$i];
$ii++;
}
}
$jumb=count($arUnix);
echo"<br><br><b>Hitung TF/IDF</b>";
$gab="<table width='300%' border='1'>";
$gab.="<tr><td>Kata";
$gab.="<td>Q";
for($i=0;$i<$jumk;$i++){
$u=$i+1;
$gab.="<td><label title='$arJ[$i]'>D".$u."</label>";
}
$gab.="<td>df";
$gab.="<td>IDF";
$gab.="<td>QDF";
for($i=0;$i<$jumk;$i++){
$u=$i+1;
$gab.="<td>QD".$u;
}
for($i=0;$i<$jumk;$i++){
$u=$i+1;
$gab.="<td>QDFD".$u;
}
$gab.="<td>Q<sup>2</sup>";
for($i=0;$i<$jumk;$i++){
$u=$i+1;
$gab.="<td>D<sup>2</sup>".$u;
}
$gab.="</tr>";
$bar=count($arUnix);
for($i=0;$i<$bar;$i++){
$kata=$arUnix[$i];
$hitung=0;
$gab.="<tr><td>".$kata."</td>";
$jumada=0;
for($j=0;$j<$jumk+1;$j++){
$ada=getHit($kata,$arStem[$j]);
$M[$i][$j]=$ada;
if($ada>0){
$jumada++;
}
$gab.="<td>".$ada;
}
// $log=log($jumk+1,10)/$jumada;
$log=log(($jumk)/$jumada,10);
$log=abs($log);
$gab.="<td>$jumada</td>";//idf
$gab.="<td>log($jumk/$jumada)=$log";
for($j=0;$j<$jumk+1;$j++){
$N[$i][$j]=$M[$i][$j] * $log;
$N2[$i][$j]=pow($N[$i][$j],2);
$TOT[$j]=$TOT[$j]+$N[$i][$j];
$gab.= "<td>".$N[$i][$j];
}
for($j=1;$j<$jumk+1;$j++){
$NN[$i][$j-1]=$N[$i][0] * $N[$i][$j];
$gab.= "<td>".$NN[$i][$j-1];
}
for($j=0;$j<$jumk+1;$j++){
$gab.= "<td>".$N2[$i][$j];
}
$gab.="</tr>";
}//for i
for($j=0;$j<$jumk;$j++){//kolom
$TOT1[$j]=0;
for($k=0;$k<$bar;$k++){//baris
$TOT1[$j]+=$NN[$k][$j];
}
}
for($j=0;$j<$jumk+1;$j++){
$TOT2[$j]=0;
for($k=0;$k<$bar;$k++){//baris
$TOT2[$j]+=$N2[$k][$j];
}
}
//------------------------------------
$gab.="<tr><td>Kata";
$gab.="<td>Q";
for($i=0;$i<$jumk;$i++){
$u=$i+1;
$gab.="<td>D".$u;
}
$gab.="<td>df";
$gab.="<td>IDF";
$gab.="<td>QDF";
for($i=0;$i<$jumk;$i++){
$u=$i+1;
$gab.="<td>QD".$u;
}
for($i=0;$i<$jumk;$i++){
$gab.="<td>".$TOT1[$i];
}
for($i=0;$i<$jumk+1;$i++){
$gab.="<td>".$TOT2[$i];
}
$gab.="</tr>";
$gab.="</table>";
echo $gab;
//++++++++++++++++++++++++++++NORM2
echo"<br><br><b>Hitung Klasifikasi</b>";
$i=0;
$tot=0;
$sqlq="select distinct(`class`) from `$tb_datalatih` order by `class` asc";
$arrq=getData($conn,$sqlq);
foreach($arrq as $dq) {
$class=$dq["class"];
$nk=$class;
$sql="select `id_datalatih` from `$tb_datalatih` where class='$class'";
$jum=getJum($conn,$sql);
$arKat[$i]=$class;
$arIdKat[$i]=$class;
$arJum[$i]=$jum;
$tot+=$jum;
$i++;
}//foreach
$p=$i;
echo"<table border='1' width='60%'>";
echo"<tr bgcolor='#bbbbbb'><td align='center'>No<td align='center'>Class<td>Jumlah</tr>";
for($i=0;$i<$p;$i++){
$no=$i+1;
$kat=$arKat[$i];
$jum=$arJum[$i];
$color='#dddddd';
if($i%2==0){$color='#eeeeee';}
echo"<tr bgcolor='$color'><td>$no<td>$kat<td align='right'>$jum</tr>";
}//for
echo"</table>";
echo"Total data=".$tot."<br>";
$gab="";
$sql="select `normalisasi` from `$tb_datalatih` order by `class` asc";
$arr=getData($conn,$sql);
foreach($arr as $d) {
$normalisasi=$d["normalisasi"];
if(strlen($normalisasi)>0){
$gab.=$normalisasi." ";
}
}
$ar0=explode(" ",$gab);
$ar=getUnik($ar0);
$N=count($ar);
echo"<strong> Tokenization</strong>";
$no=0;
echo"<table border='1' width='60%'>";
echo"<tr bgcolor='#bbbbbb'><td align='center'>No<td align='center'>Token";
for($i=0;$i<$p;$i++){
$kat=$arKat[$i];
echo"<td>".$kat;
}
echo"</tr>";
for($j=0;$j<count($ar)-1;$j++){
$no=$j+1;
$color='#dddddd';
if($no%2==0){$color='#eeeeee';}
$KAL=$ar[$j];
echo"<tr bgcolor='$color'><td>$no<td>$KAL";
for($i=0;$i<$p;$i++){
$idk=$arIdKat[$i];
$kalimat=$KAL;
$r=getHitung($conn,$idk,$kalimat);//rand(0,1);
echo"<td>".$r;
}
echo"</tr>";
}//for
echo"</table>";
$ark=explode(" ",$stemming);
echo"<br><br><b>Naive Bayes Classifier</b><table border='1' width='60%'>";
echo"<tr bgcolor='#bbbbbb'><td align='center'>No<td align='center'>Class</td>";
for($j=0;$j<count($ark);$j++){
echo"<td>".$ark[$j];
}
echo"</tr>";
$pan=$p;
for($i=0;$i<$pan;$i++){
$no=$i+1;
$idk=$arIdKat[$i];
$kat=$arKat[$i];
$jum=$arJum[$i];
$color='#dddddd';
if($i%2==0){$color='#eeeeee';}
$n=$tot;
$p=$jum/$tot;
$m=count($ark);
echo"<tr bgcolor='$color'><td>$no<td>$kat</td>";
$totc=$p;
$stotc="$p x ";
for($j=0;$j<$m;$j++){
$kata=$ark[$j];
$nc=getHitung($conn,$idk,$kata);
$ajum[$i][$j]=$nc;
$bob[$i][$j]=($nc+($m * $p))/($n+$m);
$totc *=$bob[$i][$j];
$stotc .=$bob[$i][$j]." x ";
echo"<td>"."($nc+($m * $p))/($n+$m)<br>=".$bob[$i][$j];
}
$arTotc[$i]=$totc;
$arSTotc[$i]=$stotc;
echo"</tr>";
}//for
echo"</table><br><br>";
echo"Perhitungan Probabilitas";
echo"<table border='1' width='100%'>";
echo"<tr bgcolor='#bbbbbb'><td align='center'>No<td align='center'>Kategori</td><td width='60%'>Formulas<td>Total</tr>";
for($i=0;$i<$pan;$i++){
$no=$i+1;
$kat=$arKat[$i];
$color='#dddddd';
if($i%2==0){$color='#eeeeee';}
$no=$i+1;
echo"<tr bgcolor='$color'>
<td align='center' valign='top'>$no
<td valign='top' align='left'>$kat</td>
<td valign='top'>".$arSTotc[$i]."<td valign='top'>".$arTotc[$i]."</tr>";
}
echo"</table><br>";
//bubblerost
for($x = 0; $x < $pan; $x++){
for($a = 0 ; $a < $pan - 1 ; $a++){
if($a < $pan ){
if($arTotc[$a] < $arTotc[$a + 1] ){
swap($arTotc, $a, $a+1);
swap($arSTotc, $a, $a+1);
swap($arKat, $a, $a+1);
}
}
}
}
$rekapitulasi="Pengurutan Probabilitas";
$rekapitulasi.="<table border=\"1\" width=\"100%\">";
$rekapitulasi.="<tr bgcolor=\"#bbbbbb\"><td align=\"center\">No<td align=\"left\">Kategori</td><td width=\"60%\">Formulas<td>Total</tr>";
$hs="";
for($i=0;$i<$pan;$i++){
$no=$i+1;
$kat=$arKat[$i];
$color='#dddddd';
if($i%2==0){$color='#eeeeee';}
if($i==0){$hs=$kat;}
$no=$i+1;
$rekapitulasi.="<tr bgcolor=\"$color\">
<td align=\"center\" valign=\"top\">$no
<td valign=\"top\" align=\"center\">$kat</td>
<td valign=\"top\">".$arSTotc[$i]."<td valign=\"top\">".$arTotc[$i]."</tr>";
}
$rekapitulasi.="</table>";
echo $rekapitulasi;
echo"<br><font color='green' size='24'>Kategori: $hs</font>";
$time_end = microtime(true);
$durasi = $time_end - $time_start;
$sql="update `$tb_datauji` set
`class`='$hs',
`normalisasi`='$stemming',
`durasi`='$durasi',
`rekapitulasi`='$rekapitulasi',
`status`='Proses',
`keterangan`=''
where `id_datauji`='$id_datauji'";
$ubah=process($conn,$sql);
function swap2(&$arr, $a, $b) {
$tmp = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $tmp;
}
function getUnik($a){
$b=array_unique($a);
$i=0;
for($m=0;$m<=count($a);$m++){
if($b[$m]==""){}
else{
$c[$i]=$b[$m];
$i++;
}
}
return $c;
}
function getHitung($conn,$idk,$kata){
$sql="select `id_datalatih` from `tb_datalatih` where `class`='$idk' and `normalisasi` like '%$kata%'";//kalimat
$rs=$conn->query($sql);
$jum= $rs->num_rows;
$rs->free();
return $jum;
}
?>
</div>
</div>
Langganan:
Posting Komentar (Atom)
Selisih Waktu Berjalan
Berikut adalah fungsi PHP yang menghitung selisih hari antara tanggal saat ini dengan tanggal 2024-08-22 . Jika tanggal saat ini kurang dari...
-
<?php // <a href='download.php?file=$file' title='Download File'>Download Dokumen</a> $direktori = ...
-
Teman2 ...mungkin sering dapat kode random..... btw ini ada share sedikit ttg membuat soal random.....yang beda disini adalah sy menggunaka...
-
<?php $sql="select `id_admin` from `$tbadmin` order by `id_admin` desc"; $jum= getJum($conn,$sql); $kd="ADM"...
Tidak ada komentar:
Posting Komentar