230 lines
5.0 KiB
PHP
Executable File
230 lines
5.0 KiB
PHP
Executable File
<?php
|
|
|
|
// Este script seleciona amostras representativas de treino e de teste de cada cluster gerado
|
|
|
|
// Diretorio de entrada
|
|
$diretoriotarefas= "indexados/";
|
|
|
|
// Função para gerar treino e teste por clustering
|
|
|
|
function gerartreinoteste($matrizind, $listaclusters, $labels, $dirdestino)
|
|
{
|
|
// Obter linha de cabecalho
|
|
$mcabecalho=file("arquivosindexacao/headtokens.csv");
|
|
$cabecalho = trim($mcabecalho[0]);
|
|
$cabecalho = $cabecalho."\n";
|
|
unset($mcabecalho);
|
|
|
|
// Abrir arquivos de treino e teste
|
|
|
|
$arqtreino= $dirdestino."treino.csv";
|
|
$arqteste= $dirdestino."teste.csv";
|
|
$rotulostreino = $dirdestino."ltreino.csv";
|
|
$rotulosteste = $dirdestino."lteste.csv";
|
|
|
|
$ftr= fopen($arqtreino,'w');
|
|
$fte= fopen($arqteste,'w');
|
|
$fltr= fopen($rotulostreino,'w');
|
|
$flte=fopen($rotulosteste,'w');
|
|
|
|
// Grava cabecalho
|
|
|
|
fwrite($ftr,$cabecalho);
|
|
fwrite($fte,$cabecalho);
|
|
|
|
// 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;
|
|
|
|
// 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 o teste (ruído)
|
|
// Se há mais de dois índices, sorteia 2/3. Se não sorteia só um índice
|
|
|
|
if ($ni<2)
|
|
{
|
|
$nt=0;
|
|
|
|
|
|
|
|
unset($vetindices[$nt]);
|
|
$vetindices=array_values($vetindices);
|
|
|
|
|
|
|
|
}
|
|
else
|
|
{
|
|
|
|
$nt=round((2/3)*$ni);
|
|
|
|
}
|
|
for($k=0;$k<$nt;$k++)
|
|
{
|
|
|
|
// Sorteia valor um item da matriz
|
|
$vs = array_rand($vetindices, 1);
|
|
|
|
|
|
// Grava linha sorteada da matriz no treino
|
|
|
|
$indv=$vetindices[$vs];
|
|
|
|
|
|
|
|
$linha= str_replace(' ', ';', $matrizind[$indv]);
|
|
$codigo=$labels[$indv];
|
|
|
|
// Valor atual da nota=0 e vetor de características
|
|
|
|
|
|
fwrite($ftr, $linha);
|
|
fwrite($fltr, $codigo);
|
|
|
|
// Remove a linha do indice que foi sorteada da matriz para não ser sorteada de novo
|
|
|
|
unset($vetindices[$vs]);
|
|
|
|
// Redefine os indices
|
|
|
|
$vetindices=array_values($vetindices);
|
|
|
|
|
|
|
|
}
|
|
// Grava os padroes restantes no arquivo de teste
|
|
|
|
|
|
foreach($vetindices as $indteste)
|
|
{
|
|
|
|
$linha= str_replace(' ', ';', $matrizind[$indteste]);
|
|
$codigo=$labels[$indteste];
|
|
|
|
fwrite($fte, $linha);
|
|
fwrite($flte, $codigo);
|
|
}
|
|
|
|
unset($vetindices);
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose($ftr);
|
|
fclose($fte);
|
|
fclose($fltr);
|
|
fclose($flte);
|
|
|
|
unset($v);
|
|
}
|
|
|
|
// Para cada tarefa
|
|
|
|
// Entrar no diretorio
|
|
|
|
$ponteiro1 = opendir($diretoriotarefas);
|
|
|
|
|
|
if (!(file_exists($diretoriotarefas)))
|
|
{
|
|
exit(1);
|
|
}
|
|
// Para cada subdiretorio tarefas, verifique se diretorio da questao já foi indexado
|
|
|
|
|
|
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";
|
|
|
|
|
|
// Geração de treino e teste por clustering
|
|
$x= file_exists($arquivo);
|
|
|
|
if (file_exists($arquivo) && file_exists($arqclusters))
|
|
{
|
|
|
|
$clusters=file($arqclusters);
|
|
$matrizindexada=file($arquivo);
|
|
$rotulos = file($arqrotulos);
|
|
|
|
// 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);
|
|
|
|
$dirresults=$diretoriotarefas.$tarefa."/";
|
|
|
|
gerartreinoteste($matrizindexada, $clusters, $rotulos, $dirresults);
|
|
|
|
// Destroi matrizes geradas
|
|
|
|
unset($clusters);
|
|
unset($matrizindexada);
|
|
unset($rotulos);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
?>
|
|
|
|
|