<?
echo "<script type='text/javascript' src='https://www.google.com/jsapi'></script>";

function db_aldina_ngram_connect()
{
$result= mysqli_connect("localhost","tagstudi_aldina","aldina1234567","tagstudi_dokumenta");
if(!$result)
	return false;
else
return $result;
}

function obrisi_brojeve()
{
$con = db_aldina_ngram_connect();
mysqli_query($con,"DELETE FROM `tklj_rec_u_recenicama` WHERE (LEFT(`rec`, 1) IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9','-'))");
mysqli_query($con,"DELETE FROM `tklj_parovi` WHERE (LEFT(`rec1`, 1) IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9','-'))");
mysqli_query($con,"DELETE FROM `kljucne` WHERE (LEFT(`rec`, 1) IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9','-',' '))");
}

function izdvojKljucne()
{
$con=db_aldina_ngram_connect();
mysqli_query($con, "set names 'utf8'");
$result=mysqli_query($con,"SELECT * FROM `kljucne` WHERE `hi`>50 AND `br_pojava`>10");
echo "<table border='1'><tr><th>Redni broj</th><th>Ključna rec</th></tr>";
$i=1;
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
	{
		echo "<tr><td>".$i."</td><td>".$row["rec"]."</td></tr>";
		$i++;
	}
echo "</table>";

}

function check_cosine(array $tokensA, array $tokensB) {
    $dotProduct = $normA = $normB = 0;
    $uniqueTokensA = $uniqueTokensB = array();
    $uniqueMergedTokens = array_unique(array_merge($tokensA, $tokensB));

    foreach ($tokensA as $token) $uniqueTokensA[$token] = 0;
    foreach ($tokensB as $token) $uniqueTokensB[$token] = 0;

    foreach ($uniqueMergedTokens as $token) {
        $x = isset($uniqueTokensA[$token]) ? 1 : 0;
        $y = isset($uniqueTokensB[$token]) ? 1 : 0;
        $dotProduct += $x * $y;
        $normA += $x;
        $normB += $y;
    }

    return ($normA * $normB) != 0
        ? $dotProduct / sqrt($normA * $normB)
        : 0;
}

function check_haming ($terms1, $terms2) {
    $counts1 = array_count_values($terms1);
    $totalScore = 0;
    foreach ($terms2 as $term) {
        if (isset($counts1[$term])) $totalScore += 1;
    }
    return $totalScore * 500 / (count($terms1) * count($terms2));
}

function sinonimiKljucne()
{
$con=db_aldina_ngram_connect();
mysqli_query($con,"set names 'utf8'");
//$result=mysql_query("SELECT * FROM `kljucne` WHERE `br_pojava`>10");
$result=mysqli_query($con,"SELECT  `rec` , COUNT( DISTINCT  `id_dokumenta` ) AS  `broj_doc` 
FROM  `kljucne` 
GROUP BY  `rec` 
ORDER BY  `broj_doc` DESC LIMIT 50");
$i=0;
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
	{
		$kljucne1[$i]=substr($row["rec"],0,4);
		//$kljucne1[$i]=$row["rec"];
		//echo $kljucne1[$i]."<br/>";
		$i++;
	}


$kljuc = array_unique($kljucne1);

$i=0;
foreach ($kljuc as $key => $value){
    $kljucne[$i]=$value;
   // echo $kljucne[$i]."<br/>";
  $i++;
    }
$h=sizeof($kljucne);$s=0;
for($j=0;$j<$h;$j++)
{

mysqli_query($con,"set names 'utf8'");
$n=$kljucne[$j];
$m=$n."%";
$result=mysqli_query($con,"SELECT * FROM `tklj_parovi` WHERE `rec1` LIKE '$m'");
if (mysqli_num_rows($result)!=0)
{
$kljucne2[$s]=$n;
$s++;
//echo "<table border='1'><tr><th>Redni broj</th><th>Rec1</th><th>Rec2</th></tr>";
$k=0;
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
	{
		$mat[$n][$k]=$row["rec2"];$k++;
		//echo "<tr><td>".$k."</td><td>".$row["rec1"]."</td><td>".$row["rec2"]."</td></tr>";
	}
//echo "</table>";
}

}

$h=sizeof($kljucne2);
for($i=0;$i<$h;$i++)
{
$b=0;
//$a1=null;
//$a3=null;
//$a4=null;
echo "<table border='1'><tr><th>".$kljucne2[$i]."</th><th>Cosine Similarity Coefficient</th><th>Jaccard Coefficient</th></tr>";
for($j=0;$j<$h;$j++)
{
if($i!=$j)
{

$cc=round(check_cosine($mat[$kljucne2[$i]], $mat[$kljucne2[$j]]),4);
$jc=round(getSimilarityCoefficient($mat[$kljucne2[$i]], $mat[$kljucne2[$j]]),4);

if($cc>0.75)
{
//echo "<tr><td>".$kljucne2[$i]."</td><td>".$kljucne2[$j]."</td><td>".$cc."</td><td>".$jc."</td></tr>";
$a1[$i][$b]=$kljucne2[$j];
$a3[$i][$b]=$cc;
$a4[$i][$b]=$jc;

echo "<tr><td>".$a1[$i][$b]."</td><td>".$a3[$i][$b]."</td><td>".$a4[$i][$b]."</td></tr>";
$b++;

}
}

}

echo "</table>";

//crtajTabele($kljucne2[$i], $a1[$i], $a3[$i], $a4[$i], $h);

}


}

obrisi_brojeve();
sinonimiKljucne();

function getSimilarityCoefficient( array $item1, array $item2 ) {
	
	
	$arr_intersection = array_intersect( $item1, $item2 );
	$arr_union = array_merge( $item1, $item2 );
	$coefficient = count( $arr_intersection ) / count( $arr_union );
	
	return $coefficient;
}

function crtajTabele($a2, array $a1, array $a3, array $a4, $b)
{
echo "<script type='text/javascript'>";
for ($j=0;$j<$b;$j++)
{
echo "var grupa_tw= ".json_encode($a1).";

   var grupa_cos= ".json_encode($a3).";
   var grupa_jac= ".json_encode($a4).";
    var broj= ".sizeof($a1).";
   
     
      google.load('visualization', '1.0', {'packages':['corechart']});
      google.load('visualization', '1.0', {'packages':['table']});

      google.setOnLoadCallback(drawSort);

             function drawSort() {
             
             
 
  var data = new google.visualization.DataTable();
  data.addColumn('string', '".$a2."');
    data.addColumn('number', 'Cossinus Similarity');
    data.addColumn('number', 'Jaccard Coefficient');
  
 data.addRows(broj);
 
 for(var i=0;i<broj;i++){
 
 
var tw=grupa_tw[i];
  data.setCell(i,0,tw);
  
var cos=parseFloat(grupa_cos[i]);
    data.setCell(i,1,cos);
 var jac=parseFloat(grupa_jac[i]);
    data.setCell(i,2,jac);
 
}



  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1,2]);
 
 

  var table = new google.visualization.Table(document.getElementById('table_sort_div".$j."'));
  table.draw(view);
	
 /* var chart = new google.visualization.BarChart(document.getElementById('chart_sort_div".$j."'));
    var options = {'title':'".$a2."',
                     'width':500,
                     'height':500};
  chart.draw(view, options);*/


  google.visualization.events.addListener(table, 'sort',
      function(event) {
        data.sort([{column: event.column, desc: !event.ascending}]);
      //  chart.draw(view, options);
        
      });
}";}
    
 echo "</script>";
 for($j=0;$j<$b;$j++)
 {
 echo "<div id='table_sort_div".$j."'></div>";
   // <div id='chart_sort_div".$b."' float=right;></div>"; 
  }
 }

?>