239 lines
5.0 KiB
PHP
Executable File
239 lines
5.0 KiB
PHP
Executable File
<?php
|
|
|
|
// Este script recebe como entradas a matriz de indexação de uma submissão e o arquivo de clusters
|
|
// A saida é a geracao de um diretorio chamado clusters em cada lista de exercicios
|
|
|
|
// Diretorio de entrada
|
|
$diretoriotarefas= "indexados/";
|
|
|
|
// Função para separar clusters
|
|
|
|
function separaclusters($matrizind, $listaclusters, $labels, $dirdestino)
|
|
{
|
|
//-------------------------------------------------------------------------------------------------
|
|
// Calcula numero de padroes
|
|
|
|
$np= count($listaclusters);
|
|
|
|
// Calcula número de clusters distintos
|
|
|
|
$v=array_unique($listaclusters);
|
|
|
|
$nclusters=count($v);
|
|
|
|
// Ordena o vetor de clusters
|
|
|
|
asort($listaclusters);
|
|
|
|
|
|
|
|
// Obtém os índices de cada padrão na lista dos clusters de cada padrao
|
|
|
|
$h=array_keys($listaclusters);
|
|
$listaclusters=array_values($listaclusters);
|
|
|
|
$i=0;
|
|
// Diretorio de ruidos
|
|
$dirrd = $dirdestino."rd/";
|
|
|
|
if (!(file_exists($dirrd)))
|
|
{
|
|
mkdir($dirrd);
|
|
}
|
|
// Matriz e rotulos dos ruidos
|
|
|
|
$arqmatrizrd = $dirrd."matriz.txt";
|
|
$arqrotulosrd= $dirrd."labels.txt";
|
|
|
|
// Abrir arquivos de ruidos
|
|
|
|
$fmr= fopen($arqmatrizrd,'w');
|
|
$frr= fopen($arqrotulosrd,'w');
|
|
|
|
|
|
// Linha de cabeçalho da matriz
|
|
|
|
|
|
$mcabecalho=file("arquivosindexacao/headtokens.csv");
|
|
$cabecalho = trim($mcabecalho[0]);
|
|
$cabecalho = $cabecalho."\n";
|
|
unset($mcabecalho);
|
|
|
|
// Para cada cluster distinto
|
|
|
|
asort($v);
|
|
|
|
foreach($v as $cluster)
|
|
{
|
|
|
|
|
|
$item = trim($cluster);
|
|
|
|
|
|
|
|
|
|
$j=0;
|
|
|
|
$vetindices=array();
|
|
//--------------------------------------------------------------
|
|
$itemcluster=trim($listaclusters[$i]);
|
|
|
|
while($i<$np && ($itemcluster==$item))
|
|
{
|
|
// Obtém o vetor correspondente na matriz indexada
|
|
|
|
$vetindices[$j] = $h[$i];
|
|
$i= $i + 1;
|
|
$j=$j + 1;
|
|
if($i<$np)
|
|
{
|
|
$itemcluster=trim($listaclusters[$i]);
|
|
}
|
|
}
|
|
// Sorteia os $ni indices de um cluster
|
|
|
|
$ni=count($vetindices);
|
|
|
|
//------------------------------------------------------------------
|
|
// Se há apenas um item no cluster, vai para cluster rd (ruidos)
|
|
// Se há mais de dois índices manda para diretrio do cluster
|
|
|
|
if ($ni>=2)
|
|
{
|
|
// Cria diretorio com indice do atual cluster $cluster
|
|
|
|
$dirclust = $dirdestino.$item."/";
|
|
|
|
if (!(file_exists($dirclust)))
|
|
{
|
|
mkdir($dirclust);
|
|
}
|
|
|
|
|
|
|
|
|
|
// Abrir arquivo de matriz e rotulos
|
|
|
|
$arqmatriz = $dirclust."matriz.txt";
|
|
$arqrotulos= $dirclust."labels.txt";
|
|
$fm= fopen($arqmatriz,'w');
|
|
$fr= fopen($arqrotulos,'w');
|
|
|
|
// Grava linha de cabeçalho
|
|
|
|
fwrite($fm, $cabecalho);
|
|
|
|
// Para cada indice dos padroes do cluster
|
|
|
|
foreach($vetindices as $indice)
|
|
{
|
|
$linha = $matrizind[$indice];
|
|
$rot= $labels[$indice];
|
|
fwrite($fm, $linha);
|
|
fwrite($fr, $rot);
|
|
}
|
|
|
|
// Fecha arquivos
|
|
|
|
fclose($fm);
|
|
fclose($fr);
|
|
}
|
|
else
|
|
{
|
|
if ($ni==1)
|
|
{
|
|
|
|
// grava em ruidos
|
|
$indice= $vetindices[0];
|
|
fwrite($fmr, $matrizind[$indice]);
|
|
fwrite($frr, $labels[$indice]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
unset($vetindices);
|
|
|
|
|
|
}
|
|
|
|
fclose($fmr);
|
|
fclose($frr);
|
|
|
|
}
|
|
|
|
|
|
// Entrar no diretorio de entrada
|
|
|
|
$ponteiro1 = opendir($diretoriotarefas);
|
|
|
|
|
|
if (!(file_exists($diretoriotarefas)))
|
|
{
|
|
exit(1);
|
|
}
|
|
|
|
// Para cada questao
|
|
|
|
|
|
while (($nometarefa = readdir($ponteiro1)))
|
|
{
|
|
|
|
if (($nometarefa<>".") && ($nometarefa<>".."))
|
|
{
|
|
$tarefa=trim($nometarefa);
|
|
$arquivo= $diretoriotarefas.$tarefa."/matrizindexada_".$tarefa.".txt";
|
|
$arqclusters=$diretoriotarefas.$tarefa."/clusters.txt";
|
|
$arqrotulos=$diretoriotarefas.$tarefa."/$tarefa."."rlabel";
|
|
|
|
if (file_exists($arqclusters))
|
|
{
|
|
// Criar o diretorio clusters
|
|
$dirclusters = $diretoriotarefas.$tarefa."/clusters/";
|
|
|
|
if (!(file_exists($dirclusters)))
|
|
{
|
|
mkdir($dirclusters);
|
|
}
|
|
|
|
// Carregar matriz de indexacao
|
|
|
|
$matrizindexada= file($arquivo);
|
|
|
|
// Carregar arquivo de clusters
|
|
|
|
|
|
$clusters = file($arqclusters);
|
|
|
|
// Carregar arquivo de rotulos
|
|
|
|
$rotulos = file($arqrotulos);
|
|
|
|
// Redefinir matriz indexada
|
|
// Elimina linha que contém informações de número de linhas e de colunas e redefine os indices
|
|
|
|
unset($matrizindexada[0]);
|
|
|
|
$matrizindexada=array_values($matrizindexada);
|
|
|
|
// Executar separacao de clusters
|
|
|
|
separaclusters($matrizindexada, $clusters, $rotulos, $dirclusters);
|
|
|
|
unset($matrizindexada);
|
|
|
|
unset($clusters);
|
|
|
|
unset($rotulos);
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|
|
|
|
|