GenerateGridCommand.php 5.35 KB
Newer Older
arnaudbey's avatar
add app    
arnaudbey committed
1
2
3
4
<?php

namespace MagicWordBundle\Command;

5
use LexiconBundle\Entity\Language;
arnaudbey's avatar
add app    
arnaudbey committed
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
arnaudbey's avatar
add app    
arnaudbey committed
8
9
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\Question;
Arnaud Bey's avatar
Arnaud Bey committed
11
use Symfony\Component\Filesystem\Filesystem;
arnaudbey's avatar
add app    
arnaudbey committed
12
13
14
15
16
17
18
19
20

class GenerateGridCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('magicword:generate-grid')
            ->setDescription('generate grids')
            ->addArgument('number', InputArgument::REQUIRED)
Sylvain Hatier's avatar
Sylvain Hatier committed
21
22
            ->addArgument('threshold', InputArgument::OPTIONAL)
            ->addArgument('minBigram', InputArgument::OPTIONAL);
arnaudbey's avatar
add app    
arnaudbey committed
23
24
25
26
27
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine')->getEntityManager('default');
28
        $helper = $this->getHelper('question');
arnaudbey's avatar
add app    
arnaudbey committed
29
        $gridManager = $this->getContainer()->get('mw_manager.grid');
30
31
32
33
34
35
36
37
38

        $question = new Question("Please enter the ID of a lexicon \n");
        $languages = $em->getRepository(Language::class)->findAll();
        foreach ($languages as $language) {
            $output->writeln('<comment>'.$language->getId()." > ".$language->getValue().'</comment>');
        }

        $idLexicon = $helper->ask($input, $output, $question);

arnaudbey's avatar
add app    
arnaudbey committed
39
40
        $number = $input->getArgument('number');
        $threshold = $input->getArgument('threshold');
Sylvain Hatier's avatar
Sylvain Hatier committed
41
        $minBigram = $input->getArgument('minBigram');
arnaudbey's avatar
add app    
arnaudbey committed
42
        $totalFormCount = 0;
43
        $totalFormCountAll = 0;
arnaudbey's avatar
add app    
arnaudbey committed
44
        $keptGrid = 0;
45
        $allGrid = 0;
46
47
        $best = 0;
        $worst = 1000;
Sylvain Hatier's avatar
Sylvain Hatier committed
48
        $minBigram = (!$minBigram) ? null : $minBigram;
arnaudbey's avatar
add app    
arnaudbey committed
49

Arnaud Bey's avatar
Arnaud Bey committed
50
51
52
53
        // STATS
        $globalFoundables = []; // stockage des foundables distincts
        $foundablesCountByGrid = []; // stockage du nombre de mot par grille

Arnaud Bey's avatar
WIP    
Arnaud Bey committed
54
        while ($keptGrid < $number) {
55
56
            $language = $em->getRepository('LexiconBundle:Language')->find($idLexicon);
            $languageName = $language->getValue();
arnaudbey's avatar
add app    
arnaudbey committed
57
            $timeStart = microtime(true);
Sylvain Hatier's avatar
Sylvain Hatier committed
58
            $grid = $gridManager->generate($language, true, $minBigram);
arnaudbey's avatar
add app    
arnaudbey committed
59
60
            $timeEnd = microtime(true);
            $executionTime = round($timeEnd - $timeStart, 2);
Arnaud Bey's avatar
Arnaud Bey committed
61
62
63
64
65
66
67
68
69
70
            $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;
            }

71
            $totalFormCountAll += $formCount;
72
73
            $best = ($formCount > $best) ? $formCount : $best;
            $worst = ($formCount < $worst) ? $formCount : $worst;
arnaudbey's avatar
add app    
arnaudbey committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
            if ($threshold && $formCount < $threshold) {
                $output->writeln('<comment>A grid has been generated but does not contains enough forms ('.$formCount.'). (generated in '.$executionTime.' sec.)</comment>');
                $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++;
89
                $output->writeln('<info>('.$keptGrid.') A grid has been generated and contains '.$formCount.' forms (generated in '.$executionTime.' sec.)</info>');
arnaudbey's avatar
add app    
arnaudbey committed
90
91
92
93

                $totalFormCount += $formCount;
            }
            $em->clear();
94
            $allGrid++;
95
            $averageAll = round($totalFormCountAll / $allGrid);
Arnaud Bey's avatar
Arnaud Bey committed
96
            $countDistinctFoundables = count($globalFoundables);
97
            $output->writeln('Average form count for all '.$allGrid.' grids : <error>'.$averageAll.'</error>');
98
99
            $output->writeln('Best grid : <error>'.$best.'</error>');
            $output->writeln('Worst grid : <error>'.$worst.'</error>');
Arnaud Bey's avatar
Arnaud Bey committed
100
            $output->writeln('Distinct foundables count : <error>'.$countDistinctFoundables.'</error>');
101
            $output->writeln('');
arnaudbey's avatar
add app    
arnaudbey committed
102
103
        }

Arnaud Bey's avatar
Arnaud Bey committed
104
        $countDistinctFoundables = count($globalFoundables);
arnaudbey's avatar
add app    
arnaudbey committed
105
        $average = round($totalFormCount / $keptGrid);
106
107
        $output->writeln('<info>################### DONE ! ############################################</info>');
        $output->writeln('Average form count: for '. $keptGrid .' kept grids : <error>'.$average.'</error>');
108
        $averageAll = round($totalFormCountAll / $allGrid);
109
110
111
        $output->writeln('Average form count for all '. $allGrid .' grids : <error>'.$averageAll.'</error>');
        $output->writeln('Best grid : <error>'.$best.'</error>');
        $output->writeln('Worst grid : <error>'.$worst.'</error>');
Arnaud Bey's avatar
Arnaud Bey committed
112
        $output->writeln('Nombre de formes différentes : <error>'.$countDistinctFoundables.'</error>');
113
        $output->writeln('<info>#######################################################################</info>');
arnaudbey's avatar
add app    
arnaudbey committed
114
115
    }
}