Commit 65b78b0b authored by Sylvain Hatier's avatar Sylvain Hatier Committed by Arnaud Bey
Browse files

WIP : Delete Lexicon Command

parent 087bb2b4
<?php
namespace LexiconBundle\Command;
use LexiconBundle\Manager\ImportManager;
use LexiconBundle\Entity\Language;
use LexiconBundle\Entity\Root;
use LexiconBundle\Entity\Word;
use LexiconBundle\Entity\WordStart;
use MagicWordBundle\Entity\Grid;
use MagicWordBundle\Entity\Game;
use MagicWordBundle\Entity\Round;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
class DeleteLexiconCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('lexicon:delete')
->setDescription('Delete lexicon from db & bigram.txt files')
->addArgument('id_lexicon', InputArgument::REQUIRED, 'id of lexicon you want to delete');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>############ SUPPRESSION LANGUE #############</info>');
$em = $this->getContainer()->get('doctrine')->getEntityManager('default');
$idLexicon = $input->getArgument('id_lexicon');
if ($idLexicon) {
$language = $em->getRepository(Language::class)->find($idLexicon);
echo 'You passed an argument: ' . $idLexicon . "\n\n";
// delete games
$em->getRepository(Game::class)->deleteByLanguage($language);
$output->writeln('<comment>Suppression game ok</comment>');
// delete grid
$grids = $em->getRepository(Grid::class)->findByLanguage($language);
foreach ($grids as $grid) {
$em->remove($grid);
}
$output->writeln('<comment>Suppression grilles ok</comment>');
$words = $em->getRepository(Word::class)->deleteByLanguage($language);
$output->writeln('<comment>Suppression words ok</comment>');
$roots = $em->getRepository(Root::class)->deleteByLanguage($language);
$output->writeln('<comment>Suppression roots ok</comment>');
$wordstarts = $em->getRepository(WordStart::class)->deleteByLanguage($language->getId());
$output->writeln('<comment>Suppression wordstarts ok</comment>');
$em->remove($language);
$output->writeln('<comment>Suppression langage ok</comment>');
$em->flush();
//suppression fichier bigram
$pathProject = $this->getContainer()->getParameter('languageDir');
$bigramPath = $pathProject."/data/lexicons/".$language->getValue().DIRECTORY_SEPARATOR."bigrams.txt";
$fileSystem = new Filesystem();
$fileSystem->remove($bigramPath);
$output->writeln('<comment>Suppression fichier bigrammes</comment>');
$output->writeln('<info>########### SUPPRESSION LANGUE OK ############</info>');
} else {
echo 'Argument missing';
}
}
}
......@@ -17,22 +17,48 @@ class BigramManager
{
public function generateBigrams($bigrams, $pathLexiconDir)
{
//hash enregistrant les bigrams déjà écrit à dans $string2print
$grambigrams = [];
//Impression des keys
$string2print="Bigrammes\tFreq\tPoids\tArrondi\n";
arsort($bigrams);
$max=max($bigrams);
$nbTranches=100;#nombre de tranches pour les poids de bigram (on divise $max par $nbtranches et le résultat vaut poids)
$max = max($bigrams);
$nbTranches = 100;#nombre de tranches pour les poids de bigram (on divise $max par $nbtranches et le résultat vaut poids)
$factorBigram=$max/$nbTranches;
foreach ($bigrams as $key => $value) {
$poids=$value/$factorBigram;
$poidsRound=1;
if ($poids>1) {
$poidsRound=round($poids);
$yek = $this->mb_strrev($key);
if (!in_array($key, $grambigrams)) {
$freqInverse=0;
if (isset($bigrams[$yek])) {
$freqInverse=$bigrams[$yek];
}
$poids = ($value+$freqInverse)/$factorBigram;
$poidsRound = 1;
if ($poids>1) {
$poidsRound=round($poids);
}
$string2print .= $key."\t".$value."\t".$poids."\t".$poidsRound."\n";
$grambigrams[] = $key;
$grambigrams[] = $yek;
}
$string2print.=$key."\t".$value."\t".$poids."\t".$poidsRound."\n";
}
file_put_contents($pathLexiconDir."/bigrams.txt", $string2print);
return;
}
public function mb_strrev($string, $encoding = null)
{
if ($encoding === null) {
$encoding = mb_detect_encoding($string);
}
$length = mb_strlen($string, $encoding);
$reversed = '';
while ($length-- > 0) {
$reversed .= mb_substr($string, $length, 1, $encoding);
}
return $reversed;
}
}
......@@ -34,6 +34,7 @@ class LetterManager
$letter->setValue($letterValue);
$letter->setPoints(isset($weights[$letterValue]) ? $weights[$letterValue] : 1);
// $string2print="Lettre OK ctype_alpha = ".$letterValue."\n";
$this->em->persist($letter);
}
}
......
......@@ -40,7 +40,7 @@ class Game
private $description;
/**
* @ORM\OneToMany(targetEntity="Round", mappedBy="game")
* @ORM\OneToMany(targetEntity="Round", mappedBy="game", cascade={"remove"})
* @ORM\OrderBy({"displayOrder" = "ASC"})
*/
private $rounds;
......
......@@ -29,13 +29,13 @@ class Grid implements \JsonSerializable
private $side = 4;
/**
* @ORM\OneToMany(targetEntity="FoundableForm", mappedBy="grid")
* @ORM\OneToMany(targetEntity="FoundableForm", mappedBy="grid", cascade={"remove"})
* @ORM\OrderBy({"points" = "DESC"})
*/
private $foundableForms;
/**
* @ORM\OneToMany(targetEntity="Square", mappedBy="grid")
* @ORM\OneToMany(targetEntity="Square", mappedBy="grid", cascade={"remove"})
*/
protected $squares;
......@@ -49,10 +49,6 @@ class Grid implements \JsonSerializable
*/
private $json;
public function __construct()
{
$this->squares = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id.
......@@ -64,40 +60,6 @@ class Grid implements \JsonSerializable
return $this->id;
}
/**
* Add square.
*
* @param \MagicWordBundle\Entity\Square $square
*
* @return Grid
*/
public function addSquare(\MagicWordBundle\Entity\Square $square)
{
$this->squares[] = $square;
return $this;
}
/**
* Remove square.
*
* @param \MagicWordBundle\Entity\Square $square
*/
public function removeSquare(\MagicWordBundle\Entity\Square $square)
{
$this->squares->removeElement($square);
}
/**
* Get squares.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSquares()
{
return $this->squares;
}
/**
* Set language.
*
......@@ -234,4 +196,48 @@ class Grid implements \JsonSerializable
{
return $this->side;
}
/**
* Constructor
*/
public function __construct()
{
$this->foundableForms = new \Doctrine\Common\Collections\ArrayCollection();
$this->squares = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add square.
*
* @param \MagicWordBundle\Entity\Square $square
*
* @return Grid
*/
public function addSquare(\MagicWordBundle\Entity\Square $square)
{
$this->squares[] = $square;
return $this;
}
/**
* Remove square.
*
* @param \MagicWordBundle\Entity\Square $square
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeSquare(\MagicWordBundle\Entity\Square $square)
{
return $this->squares->removeElement($square);
}
/**
* Get squares.
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSquares()
{
return $this->squares;
}
}
......@@ -64,7 +64,7 @@ class Round implements \JsonSerializable
private $displayOrder;
/**
* @ORM\ManyToOne(targetEntity="Grid")
* @ORM\ManyToOne(targetEntity="Grid", cascade={"remove"})
*/
private $grid;
......
......@@ -27,9 +27,10 @@ class Square
protected $letter;
/**
* @ORM\ManyToOne(targetEntity="Grid", inversedBy="squares", cascade={"persist"})
* @ORM\ManyToOne(targetEntity="Grid", inversedBy="squares", cascade={"remove"})
*/
protected $grid;
private $grid;
/**
* Get id.
......@@ -42,50 +43,46 @@ class Square
}
/**
* Set grid.
* Set letter.
*
* @param \MagicWordBundle\Entity\Grid $grid
* @param \LexiconBundle\Entity\Letter|null $letter
*
* @return Square
*/
public function setGrid(\MagicWordBundle\Entity\Grid $grid = null)
public function setLetter(\LexiconBundle\Entity\Letter $letter = null)
{
$this->grid = $grid;
$this->letter = $letter;
return $this;
}
/**
* Get grid.
* Get letter.
*
* @return \MagicWordBundle\Entity\Grid
* @return \LexiconBundle\Entity\Letter|null
*/
public function getGrid()
public function getLetter()
{
return $this->grid;
return $this->letter;
}
/**
* Set letter.
*
* @param \LexiconBundle\Entity\Letter|null $letter
* Set grid.
*
* @return Square
*/
public function setLetter(\LexiconBundle\Entity\Letter $letter = null)
public function setGrid(\MagicWordBundle\Entity\Grid $grid = null)
{
$this->letter = $letter;
$this->grid = $grid;
return $this;
}
/**
* Get letter.
* Get grid.
*
* @return \LexiconBundle\Entity\Letter|null
*/
public function getLetter()
public function getGrid()
{
return $this->letter;
return $this->grid;
}
}
......@@ -55,8 +55,10 @@ class GridManager
$letters = $this->letterLangManager->lottery($language, 4);
foreach ($letters as $letter) {
$grid->addSquare($this->squareManager->create($letter, $grid));
$square = $this->squareManager->create($letter, $grid);
$grid->addSquare($square);
}
$words = $this->findWords($grid);
$this->foundableFormManager->populateFoundables($words, $grid);
......
<?php
namespace MagicWordBundle\Manager\Letter;
use JMS\DiExtraBundle\Annotation as DI;
use MagicWordBundle\Entity\Bigram\Grille;
/**
* @DI\Service("mw_manager.bigram")
*/
class BigramManager
{
protected $fileLocator;
/**
* @DI\InjectParams({
* "fileLocator" = @DI\Inject("file_locator"),
* })
*/
public function __construct($fileLocator)
{
$this->fileLocator = $fileLocator;
}
public function generate()
{
$g1 = new Grille();
$file = $this->fileLocator->locate('files/freq_bigrammes_dico_ABU_poids.txt');
$g1->generGrillePonderation($file);
$tabCases = $g1->getTabCases();
$letters = [];
foreach ($tabCases as $tabCase) {
$letters[] = $tabCase->getVal();
}
return $letters;
}
}
......@@ -57,13 +57,35 @@ class LetterLanguageManager
$chars = preg_split('//u', $bigram, -1, PREG_SPLIT_NO_EMPTY);
$firstLetter = $chars[0];
$secondLetter = $chars[1];
$flagBigram = false;
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
if ($grid[$i][$j] == $firstLetter) {
$ajdacentsLetterBigram = $this->getEmptyAdjacentSquares($grid, $side, [$i, $j]);
if (!empty($ajdacentsLetterBigram)) {
$ajdacentLetterBigram = $ajdacentsLetterBigram[array_rand($ajdacentsLetterBigram)];
$grid[$ajdacentLetterBigram[0]][$ajdacentLetterBigram[1]] = $secondLetter;
$flagBigram = true;
break 2;
}
} elseif ($grid[$i][$j] == $secondLetter) {
$ajdacentsLetterBigram = $this->getEmptyAdjacentSquares($grid, $side, [$i, $j]);
if (!empty($ajdacentsLetterBigram)) {
$ajdacentLetterBigram = $ajdacentsLetterBigram[array_rand($ajdacentsLetterBigram)];
$grid[$ajdacentLetterBigram[0]][$ajdacentLetterBigram[1]] = $firstLetter;
$flagBigram = true;
break 2;
}
}
}
}
if (!$flagBigram) {
$ajdacentEmptySquares = $this->getEmptyAdjacentSquares($grid, $side, $emptySquare);
$randomAdjacent = (!empty($ajdacentEmptySquares)) ? $ajdacentEmptySquares[array_rand($ajdacentEmptySquares)] : null;
$ajdacentEmptySquares = $this->getEmptyAdjacentSquares($grid, $side, $emptySquare);
$randomAdjacent = (!empty($ajdacentEmptySquares)) ? $ajdacentEmptySquares[array_rand($ajdacentEmptySquares)] : null;
$grid[$emptySquareI][$emptySquareJ] = $firstLetter;
$grid[$randomAdjacent[0]][$randomAdjacent[1]] = ($randomAdjacent !== null) ? $secondLetter : null;
$grid[$emptySquareI][$emptySquareJ] = $firstLetter;
$grid[$randomAdjacent[0]][$randomAdjacent[1]] = ($randomAdjacent !== null) ? $secondLetter : null;
}
$emptySquares = $this->getEmptySquares($grid, $side);
}
......
......@@ -24,7 +24,8 @@ class SquareManager
public function create($letter, $grid)
{
$letter = $this->em->getRepository("LexiconBundle:Letter")->findOneBy(["value" => $letter, "language" => $grid->getLanguage()]);
$language = $grid->getLanguage();
$letter = $this->em->getRepository("LexiconBundle:Letter")->findOneBy(["value" => $letter, "language" => $language]);
$square = new Square();
$square->setLetter($letter);
......
......@@ -4,6 +4,7 @@ namespace MagicWordBundle\Repository;
use LexiconBundle\Entity\Language;
use MagicWordBundle\Entity\Player;
use MagicWordBundle\Entity\Game;
/**
* GameRepository.
......@@ -13,6 +14,17 @@ use MagicWordBundle\Entity\Player;
*/
class GameRepository extends \Doctrine\ORM\EntityRepository
{
public function deleteByLanguage(Language $language)
{
return $this->createQueryBuilder('g')
->delete(Game::class, 'g')
->andWhere('g.language = :val')
->setParameter('val', $language)
->getQuery()
->getResult()
;
}
public function findNotPlayedForTraining(Player $user, Language $language)
{
$em = $this->_em;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment