我想问一下如何解决我的问题 我已经有了代码:
for ($i = 0; $i < count($kataDasar); $i++) {
for ($j = 0; $j < count($kataDasar1); $j++) {
$jml = 0;
if ($kataDasar[$i] == $kataDasar1[$j]) {
$jml++;
}
}
$tf[$i][$j] = $jml;
}
问题是,当我使用变量$kataDasar
进行循环时,它会使$tf[$i][$j]
的值错误,它应该与其他变量$paragraf
循环,当我进行
for($i=0; $i<count($paragraf); $i++)
比较效果不好..任何人都可以帮我解决问题..
这里是我的完整代码
echo '<p><b>Pemecahan Steamming</b></p>';
print_r($kataDasar);
echo "<br/><br/>";
$kataDasar1 = Array();
foreach($kataDasar as $item){
if($item != ''){
if(!in_array($item, $kataDasar1) ){
$kataDasar1[] = $item;
}
}
}
print_r($kataDasar1);
echo "<br/><br/>";
//7. Perhitungan Bobot
$tf = Array();
$df = Array();
$q = Array();
for($i=0; $i<count($kataDasar); $i++){
for($j=0; $j<count($kataDasar1); $j++){
$jml = 0;
if ( $kataDasar[$i]== $kataDasar1[$j] ){
$jml++;
}
$tf[$i][$j] = $jml;
if(isset($df[$j])){
if($jml>0){
$df[$j] += 1;
}
}else{
if($jml>0){
$df[$j] = 1;
}else{
$df[$j] = 0;
}
}
$q[$j] = 0;
}
// }
}
$tmp_df = Array();
$tmp_df = $df;
for($i=0; $i<3; $i++){ //Get 3 nilai df terbesar
$max_df = max($tmp_df);
$key_df = array_search($max_df, $tmp_df);
$kata_q[] = $kataDasar1[$key_df];
$q[$key_df] = 1;
$tmp_df[$key_df] = 0;
}
echo '<p><b>Pembobotan DF dan Penentuan Query</b></p>';
echo "df: " . "<br/>";
print_r($df);
echo "<br/>kata_q: " . "<br/>";
print_r($kata_q);
echo "<br/> q: " . "<br/>";
print_r($q);
echo "<br/><br/>";
//exit();
$idf = Array(); $i=0;
foreach($df as $item){
echo "Kata: ". $kataDasar1[$i] .", Par: " . count($paragraf) . ", df:" . $item . "<br/>";
$idf[] = log10(count($paragraf) / $item);
$i++;
}
echo "<br/><br/>";
echo '<p><b>Hasil IDF</b></p>';
print_r($idf);
echo "<br/><br/>";
// exit();
$W = Array(); $W_q = Array();
for($i=0; $i<count($paragraf); $i++){
for($j=0; $j<count($kataDasar1); $j++){
$W[$i][$j] = $tf[$i][$j] * $idf[$j];
$W_q[$j] = $q[$j] * $idf[$j];
}
}
echo '<p><b>Nilai Query</b></p>';
print_r($W_q);
echo "<br/><br/>";
echo '<p><b>Nilai W</b></p>';
print_r($W);
$wdi = Array();
$vektor = Array(); $vektor_q = Array();
$tot_wdi = Array(); $tot_vektor = Array(); $tot_vektor_q = 0;
for($i=0; $i<count($paragraf); $i++){
$t_wdi = 0; $t_vektor = 0; $t_vektor_q = 0;
for($j=0; $j<count($kataDasar1); $j++){
$vektor[$i][$j] = $W[$i][$j] * $W[$i][$j];
$vektor_q[$j] = $W_q[$j] * $W_q[$j];
$wdi[$i][$j] = $vektor_q[$j] * $tf[$i][$j];
$t_wdi += $wdi[$i][$j];
$t_vektor += $vektor[$i][$j];
$t_vektor_q += $vektor_q[$j];
}
$tot_wdi[] = $t_wdi;
$tot_vektor[] = $t_vektor;
$tot_vektor_q = $t_vektor_q;
}
print_r($vektor);
print_r($tot_vektor);
答案 0 :(得分:0)
从您的代码中。您将$ kataDasar与增量变量循环为$ k并使用$ i访问它。您应该与正确的增量变量进行比较。这应该是正确的事情:
for($k=0; $k<count($kataDasar); $k++){
for($j=0; $j<count($kataDasar1); $j++){
$jml = 0;
if ( $kataDasar[$k]== $kataDasar1[$j] ){
$jml++;
}
// }
$tf[$i][$j] = $jml;