<?php
// Tokenizacija dijagnoza na celine i reci-naziv dijagnoze na srpskom

//funkcija stampa elemente niza
function stampaj_niz($niz)
{
    echo "BR:".$i;
    while($i<=count($niz))
    {
        echo $i.":".$niz[$i++]."||";
    }
}

//Izdvaja skup stop reci gde je dijakriticki simbol zamaenjen sa x ili y pri cemu se salje prvo slovo stop reci
function select_stop_words($letter)
{
   $conn = mysqli_connect("localhost","tagstudi_ukica","Ukica2019","tagstudi_ulfeta");
   if (!mysqli_connect_errno())
   {
       
  $sqlstop = "SELECT naziv FROM stop_rec_dist2 where naziv like '".$letter."' or naziv like '".$letter."%'";
  //echo  $sqlstop;
  $resultstop = $conn->query($sqlstop);  
  $i=0; 
  if ($resultstop->num_rows > 0) {
   
    while($row = $resultstop->fetch_assoc()) {
      $stopreci[$i]=$row["naziv"];
      $i++;
    }
     
    return $stopreci;
  }
   else
      {
          echo "Zero rows!";
      }}
  else
  {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      
  }
   
}
//proverava da li se rec nalazi u skupu skacenica
function select_skracenice($rec)
{
   $conn = mysqli_connect("localhost","tagstudi_ukica","Ukica2019","tagstudi_ulfeta");
   if (!mysqli_connect_errno())
   {
       
  $sqlskr = "SELECT zamena FROM skracenice where skracenica like '".$rec."'";
  $resultskr = $conn->query($sqlskr);  
  if ($resultskr->num_rows > 0) {
   
    while($row = $resultskr->fetch_assoc()) {
      $zamena=$row["zamena"];
      
    }
    return $zamena;
  }
   else
      {
          return null;
      }}
  else
  {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      
  }
   
}
//CISTI STOP RECI
function ocisti_od_stop_reci($celina_dijagnoza)
{
	
	$norm= explode(' ',$celina_dijagnoza);
         
	$p=0;$q=0;$bez_stop=NULL;
        while($p<count($norm))
        {
         $stop_rec=0;
            //if(strlen($norm[$p]>3))
            //{
                $slovo = substr($norm[$p],0,1);
                $stop=select_stop_words($slovo);
                
                for($k=0;$k<count($stop);$k++)
                {
                    if($norm[$p]==$stop[$k])
                    {
                   $stop_rec=1;$k=count($stop);
                    }
                    
                }
                //$j++;
              if($stop_rec!=1)
              {
               $bez_stop[$q]=$norm[$p];
               echo $bez_stop[$q++]."|";
              }
            //}
            /*else
            {
                $bez_stop[$q]=$norm[$p];
               echo $bez_stop[$q++]."|";
            }*/
            $p++;
        }
		return implode( " ", $bez_stop );
}
//Izdvaja skup dijagnoza
function select_all_dija()
{
   $conn = mysqli_connect("localhost","tagstudi_ukica","Ukica2019","tagstudi_ulfeta");
   
   if (!mysqli_connect_errno())
   {
   //IZLISTAVA SKUP Dijagnoza2  
  $sql = "SELECT Sifra,NazivD FROM Dijagnoza2 where Sifra>12157";
  $result = $conn->query($sql);

if ($result->num_rows > 0) {
 //CISCENJE PRAZNINA , SPECIJALNIH ZNAKOVA...
    while($row = $result->fetch_assoc()) {
        $Sifra=$row["Sifra"];
		$original = strtolower($row["NazivD"]);//PREBACENA SVA SLOVA U MALA
       /* $original="Anksiozni(izbegavajucyi)@, poremecyaj. licxnosti - jjj";
        $original1 = trim(preg_replace("/[^A-Za-z\-0-9,.;()]/", " ", $original));//uKLANJANJE SVEGA SEM BROJEVA SLOVA I - . ,;()
        $original2 = trim(preg_replace('/\s\s+/', '\s', $original1));//CICENJE VISESTRUKIH RAZMAKA
        $original3 = str_replace(',\s', '/', $original2);//OBELEZAVANJE SEPARATORA RECENICE
        $original4 = str_replace(',', '/', $original3);//OBELEZAVANJE SEPARATORA RECENICE
        $original5 = str_replace('.\s', '/', $original4);//OBELEZAVANJE KRAJA RECENICE
		$original6 = str_replace('.', '/', $original5);//OBELEZAVANJE KRAJA RECENICE
		$original7 = str_replace('(\s', '/', $original6);//OBELEZAVANJE KRAJA RECENICE
		$original8 = str_replace('(', '/', $original7);//OBELEZAVANJE KRAJA RECENICE
		$original9 = str_replace(')\s', '/', $original8);//OBELEZAVANJE KRAJA RECENICE
        $original10 = str_replace(')', '/', $original9);//OBELEZAVANJE KRAJA RECENICE
		$original11 = str_replace('-\s', '/', $original10);//OBELEZAVANJE KRAJA RECENICE
		$original12 = str_replace('-', '/', $original11);//OBELEZAVANJE KRAJA RECENICE
        $original13 = str_replace(';\s', '/', $original2);//OBELEZAVANJE KRAJA RECENICE
		$original14= str_replace(';', '/', $original13);//OBELEZAVANJE KRAJA RECENICE*/
		//$original1 = trim(preg_replace('/\s\s+/', '\s', $original));//CICENJE VISESTRUKIH RAZMAKA
        //$original2 = str_replace(' ', ';', $original1); //OBELEZAVANJE RAZMAKA ;
        $original3 = trim(preg_replace("/[^A-Za-z\-0-9,.;()]/", " ", $original));//uKLANJANJE SVEGA SEM BROJEVA SLOVA I - . ,;
        $original4 = str_replace(';', '.', $original3);//VRACANJE RAZMAKA
        $original5 = trim(preg_replace('/\s\s+/', '\s', $original4));//CICENJE VISESTRUKIH RAZMAKA
        $original61 = str_replace(',\s', '/', $original5);//OBELEZAVANJE SEPARATORA RECENICE
        $original6 = str_replace(',', '/', $original61);//OBELEZAVANJE SEPARATORA RECENICE
        $original71 = str_replace('.\s', '/', $original6);//OBELEZAVANJE KRAJA RECENICE
		$original7 = str_replace('.', '/', $original71);//OBELEZAVANJE KRAJA RECENICE
		$original81 = str_replace('(\s', '/', $original7);//OBELEZAVANJE KRAJA RECENICE
		$original8 = str_replace('(', '/', $original81);//OBELEZAVANJE KRAJA RECENICE
		$original91 = str_replace(')\s', '/', $original8);//OBELEZAVANJE KRAJA RECENICE
        $original9 = str_replace(')', '/', $original91);//OBELEZAVANJE KRAJA RECENICE
		 $original10 = str_replace('-\s', '/', $original9);//OBELEZAVANJE KRAJA RECENICE
		$original11 = str_replace('-', '/', $original10);//OBELEZAVANJE KRAJA RECENICE
		
		
    echo "<b>Originalna dijagnoza: </b>". $original."<br/>";
    echo "<b>Originalna dijagnoza: </b>". $original11."<br/>";
   
   	$array= explode('/',$original11);
     $i=0;
     while($i<count($array))
     {
		 $m1=$i+1;
		 //UPISUJE CELINU
		 if($array[$i]!="")
               {
            //echo $id." ".$m1." ".$array1[$ii]." "."<br/>";
			//Cistimo od stop reci  
             $celina_dijagnoza=ocisti_od_stop_reci($array[$i]);
             echo $celina_dijagnoza;
             //UPIS CELINE IZ DIJAGNOZE
              $sql = "INSERT INTO Dijagnoza_celina ( Sifra, idCeline, celinaD_norm) VALUES(".$Sifra.", ".$m1.", '".$celina_dijagnoza."')";
                if ($conn->query($sql) === TRUE) {
                //echo "OK<br/>";
                } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
                }
            
               }
         
         // $array1=nil;
         $array1= explode(' ',$celina_dijagnoza);
         $ii=0;
		 
         while($ii<count($array1))
           {
			   //UPISUJE REC
               if($array1[$ii]!="")
               {
            // echo $id." ".$m1." ".$array1[$ii]." "."<br/>";
             //UPIS RECI IZ CELINA
              $sql = "INSERT INTO Dijagnoza_rec (Sifra, idCeline, idRec, recD_norm) VALUES (".$Sifra.", ".$m1.",".$ii.", '".$array1[$ii]."')";
                if ($conn->query($sql) === TRUE) {
               // echo "OK<br/>";
                } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
                }
            
               }
               $ii++;
           }
		   
            $i++;
     }
     echo "<br/>";
	 
	}
}
     else
      {
          echo "Zero rows!";
      }}//KRAJ CITANJA REDOVA
  else
  {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      
  }
}
select_all_dija();

?>