setName('magicword:generate-grid')
->setDescription('generate grids')
->addArgument('number', InputArgument::REQUIRED)
->addArgument('threshold', InputArgument::OPTIONAL)
->addArgument('minBigram', InputArgument::OPTIONAL);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getEntityManager('default');
$helper = $this->getHelper('question');
$gridManager = $this->getContainer()->get('mw_manager.grid');
$question = new Question("Please enter the ID of a lexicon \n");
$languages = $em->getRepository(Language::class)->findAll();
foreach ($languages as $language) {
$output->writeln(''.$language->getId()." > ".$language->getValue().'');
}
$idLexicon = $helper->ask($input, $output, $question);
$number = $input->getArgument('number');
$threshold = $input->getArgument('threshold');
$minBigram = $input->getArgument('minBigram');
$totalFormCount = 0;
$totalFormCountAll = 0;
$keptGrid = 0;
$allGrid = 0;
$best = 0;
$worst = 1000;
$minBigram = (!$minBigram) ? null : $minBigram;
// STATS
$globalFoundables = []; // stockage des foundables distincts
$foundablesCountByGrid = []; // stockage du nombre de mot par grille
while ($keptGrid < $number) {
$language = $em->getRepository('LexiconBundle:Language')->find($idLexicon);
$languageName = $language->getValue();
$timeStart = microtime(true);
$grid = $gridManager->generate($language, true, $minBigram);
$timeEnd = microtime(true);
$executionTime = round($timeEnd - $timeStart, 2);
$foundables = $grid->getFoundableForms();
$formCount = $foundables ? count($foundables) : 0;
// STATS
$foundablesCountByGrid[] = $formCount;
foreach ($foundables as $foundable) {
$form = $foundable->getForm();
$globalFoundables[$form] = (isset($globalFoundables[$form])) ? $globalFoundables[$form] + 1 : 1;
}
$totalFormCountAll += $formCount;
$best = ($formCount > $best) ? $formCount : $best;
$worst = ($formCount < $worst) ? $formCount : $worst;
if ($threshold && $formCount < $threshold) {
$output->writeln('A grid has been generated but does not contains enough forms ('.$formCount.'). (generated in '.$executionTime.' sec.)');
$gridId = $grid->getId();
$squares = $em->getRepository('MagicWordBundle:Square')->findByGrid($gridId);
$foundableForms = $em->getRepository('MagicWordBundle:FoundableForm')->findByGrid($gridId);
foreach ($squares as $square) {
$em->remove($square);
}
foreach ($foundableForms as $foundableForm) {
$em->remove($foundableForm);
}
$em->remove($grid);
$em->flush();
} else {
$keptGrid++;
$output->writeln('('.$keptGrid.') A grid has been generated and contains '.$formCount.' forms (generated in '.$executionTime.' sec.)');
$totalFormCount += $formCount;
}
$em->clear();
$allGrid++;
$averageAll = round($totalFormCountAll / $allGrid);
$countDistinctFoundables = count($globalFoundables);
$output->writeln('Average form count for all '.$allGrid.' grids : '.$averageAll.'');
$output->writeln('Best grid : '.$best.'');
$output->writeln('Worst grid : '.$worst.'');
$output->writeln('Distinct foundables count : '.$countDistinctFoundables.'');
$output->writeln('');
}
$countDistinctFoundables = count($globalFoundables);
$average = round($totalFormCount / $keptGrid);
$output->writeln('################### DONE ! ############################################');
$output->writeln('Average form count: for '. $keptGrid .' kept grids : '.$average.'');
$averageAll = round($totalFormCountAll / $allGrid);
$output->writeln('Average form count for all '. $allGrid .' grids : '.$averageAll.'');
$output->writeln('Best grid : '.$best.'');
$output->writeln('Worst grid : '.$worst.'');
$output->writeln('Nombre de formes différentes : '.$countDistinctFoundables.'');
$output->writeln('#######################################################################');
}
}