Wednesday, November 17, 2010

Multiple and single words count algorithm

<?php
// Multiple and single words suggestion algorithm.


    $words="this is a programming language. the programming language is only for smart humans or aliens in the all universe. There are lot of programming language available. The most popular programming language Java. programming language Java is very poupular due to its robustness. programming language Java programming language Java programming language Java programming language Java testing testing testing testing testing testing testing testing testing testing testing";
   
    echo $words."<br>";
    $words=str_replace(array(".",",",";",":"),"",$words);
    $res=array();
    $wArr=explode(" ",$words);
    $nArr=explode(" ",$words);
   
    $res=getSingleCount($wArr);
    echo "<br>Single words suggestion<br>=========================<br>";
    for($i=0;$i<count($res);$i++)
        echo $res[$i][0]."=".$res[$i][1]."<br>";
   
    echo "<br>Double words suggestion? just use n-1 number, where n is equals to 2<br>=========================<br>";
    $res2=getMultiSuggest($nArr,1,$words);
    for($i=0;$i<count($res2);$i++)
        echo $res2[$i][0]."=".$res2[$i][1]."<br>";
   
   
    echo "<br><br>Triple words suggestion? just use n-1 number, where n is equals to 3<br>=========================<br>";
    $res3=getMultiSuggest($nArr,2,$words);
    for($i=0;$i<count($res3);$i++)
        echo $res3[$i][0]."=".$res3[$i][1]."<br>";
   
   
    echo "<br><br>Four words suggestion? just use n-1 number, where n is equals to 4<br>=========================<br>";
    $res3=getMultiSuggest($nArr,3,$words);
    for($i=0;$i<count($res3);$i++)
        echo $res3[$i][0]."=".$res3[$i][1]."<br>";
   
    function getSingleCount($wArr) {
        for($i=0;$i<count($wArr);$i++) {
            $c=0;
            if($wArr[$i]=="")
                continue;
            for($j=1+$i;$j<count($wArr);$j++) {
                if($wArr[$i]!="" && $wArr[$j]!="") {
                    if(strtolower ($wArr[$i])==strtolower ($wArr[$j])) {
                        $c++;
                        $wArr[$j]="";
                    }
                }
            }
            $res[]= array($wArr[$i] , ++$c);
        }
        return $res;
    }
   
    function getMultiSuggest($nArr,$num,$fullSent) {
        $tt=$fullSent;
        $res=array();
       
        for($i=0;$i<count($nArr)-1;$i++) {
            $sWords="";
            for($j=$i;$j<=$i+$num && $j<count($nArr); $j++) {
                $sWords.=$nArr[$j]. " ";
            }
            $sWords=trim($sWords);
            $m=count(explode(" ",$sWords));
            $w=preg_match_all("/\b".$sWords."\b/i", $tt,$out);
            if($m==$num+1){
                if($w>1) {
                    $found=FALSE;
                    if(count($res)>=0) {
                        for($k=0;$k<count($res);$k++) {
                            if($res[$k][0]==$sWords) {
                                $found=TRUE;
                                break;
                            }
                        }
                    }
                    if($found==FALSE){
                        $res[]= array($sWords , $w);
                    }
                }
            }
               
        }
        return $res;   
    }
?>

No comments:

Post a Comment