Commit e38a6476 authored by Arnaud Bey's avatar Arnaud Bey
Browse files

move lottery to appropriate controller

parent 9fbf32ea
......@@ -14,10 +14,9 @@ class LetterController extends Controller
* @Route("/get-letters/{id}", name="get_letters", options={"expose"=true})
* @Method("POST")
*/
public function getInflectionAction(Language $language)
public function getLettersAction(Language $language)
{
// $letters = $this->get('mw_manager.letter_language')->getWeightedLettersByLanguage($language);
$letters = $this->get('mw_manager.letter_language')->lottery($language, 4);
$letters = $this->get('mw_manager.grid')->lottery($language, 4);
return new JsonResponse($letters);
}
......
......@@ -15,7 +15,6 @@ use Symfony\Component\HttpFoundation\Request;
class GridManager
{
protected $em;
protected $letterLangManager;
protected $squareManager;
protected $foundableFormManager;
protected $tokenStorage;
......@@ -23,16 +22,14 @@ class GridManager
/**
* @DI\InjectParams({
* "entityManager" = @DI\Inject("doctrine.orm.entity_manager"),
* "letterLangManager" = @DI\Inject("mw_manager.letter_language"),
* "foundableFormManager" = @DI\Inject("mw_manager.foundableform"),
* "squareManager" = @DI\Inject("mw_manager.square"),
* "tokenStorage" = @DI\Inject("security.token_storage"),
* })
*/
public function __construct($entityManager, $letterLangManager, $foundableFormManager, $squareManager, $tokenStorage)
public function __construct($entityManager, $foundableFormManager, $squareManager, $tokenStorage)
{
$this->em = $entityManager;
$this->letterLangManager = $letterLangManager;
$this->foundableFormManager = $foundableFormManager;
$this->squareManager = $squareManager;
$this->tokenStorage = $tokenStorage;
......@@ -52,7 +49,7 @@ class GridManager
{
$grid = $this->newGrid($language, 4);
$letters = $this->letterLangManager->lottery($language, 4, $debug);
$letters = $this->lottery($language, 4, $debug);
if ($debug === true) {
echo "\nletters: ";
......@@ -265,7 +262,6 @@ class GridManager
//
// On calcule la position de la lettre suivante (autour de la lettre en cours)
//
// ligne du dessus
$yy = $y - 1;
if ($yy >= 0) {
......@@ -352,4 +348,195 @@ class GridManager
return;
}
public function lottery(Language $language, $side, $debug = false)
{
$fileBigrams = $language->getDirectory().DIRECTORY_SEPARATOR."bigrams.txt";
// On crée un tableau de bigrammes
// où chaque bigramme apparait autant de fois que son poids
$bigrams = [];
$lines = file($fileBigrams);
$lettersFromBigrams=[];
array_shift($lines);
foreach ($lines as $line) {
$tab = explode("\t", $line);
$string = $tab[0];
$poids = $tab[3];
$bigramLetters = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY);
foreach ($bigramLetters as $bigramLetter) {
$lettersFromBigrams[$bigramLetter] = 1;
}
for ($j = 0; $j < $poids; $j++) {
$bigrams[] = $string;
}
}
$countsBigram = array_count_values($bigrams);
// var_dump($countsBigram);
// die();
// // On initialise une grille vide.
$grid = [];
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
$grid[$i][$j] = null;
}
}
$emptySquares = $this->getEmptySquares($grid, $side);
//tant qu'il reste des cases vides
while (!empty($emptySquares)) {
$bigram = $bigrams[array_rand($bigrams)];
$emptySquare = $emptySquares[array_rand($emptySquares)];
$emptySquareI = $emptySquare[0];
$emptySquareJ = $emptySquare[1];
$chars = preg_split('//u', $bigram, -1, PREG_SPLIT_NO_EMPTY);
$firstLetter = $chars[0];
$secondLetter = $chars[1];
$flagBigram = false;
// on regarde si une lettre du bigram pioché est déjà présente et qu'elle a des adjacent vide.
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
if ($grid[$i][$j] == $firstLetter) {
$ajdacentsLetterBigram = $this->getAdjacentSquares($grid, $side, [$i, $j], true);
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->getAdjacentSquares($grid, $side, [$i, $j], true);
if (!empty($ajdacentsLetterBigram)) {
$ajdacentLetterBigram = $ajdacentsLetterBigram[array_rand($ajdacentsLetterBigram)];
$grid[$ajdacentLetterBigram[0]][$ajdacentLetterBigram[1]] = $firstLetter;
$flagBigram = true;
break 2;
}
}
}
}
if (!$flagBigram) {
$ajdacentEmptySquares = $this->getAdjacentSquares($grid, $side, $emptySquare, true);
if (empty($ajdacentEmptySquares)) {
$ajdacentSquares = $this->getAdjacentSquares($grid, $side, $emptySquare, false);
$letterToScore=[];
foreach ($lettersFromBigrams as $key => $value) {
$score=0;
foreach ($ajdacentSquares as $ajdacentSquare) {
$letter = $grid[$ajdacentSquare[0]][$ajdacentSquare[1]];
if (isset($countsBigram[$letter.$key])) {
$score += $countsBigram[$letter.$key];
} elseif (isset($countsBigram[$key.$letter])) {
$score += $countsBigram[$key.$letter];
}
}
$letterToScore[$key] = $score;
}
arsort($letterToScore);
reset($letterToScore);
$bestLetter = key($letterToScore);
$grid[$emptySquareI][$emptySquareJ] = $bestLetter;
} else {
$randomAdjacent = $ajdacentEmptySquares[array_rand($ajdacentEmptySquares)];
$grid[$emptySquareI][$emptySquareJ] = $firstLetter;
$grid[$randomAdjacent[0]][$randomAdjacent[1]] = $secondLetter;
}
}
$emptySquares = $this->getEmptySquares($grid, $side);
}
$letters = [];
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
$letters[] = $grid[$i][$j];
}
}
return $letters;
}
private function getAdjacentSquares($grid, $side, $square, $returnEmpty = true)
{
$emptyAdjacentSquares = [];
$i = $square[0];
$j = $square[1];
$left = $i - 1;
$right = $i + 1;
$top = $j - 1;
$bottom = $j + 1;
$lookLeft = ($left >= 0) ? true : false;
$lookTop = ($top >= 0) ? true : false;
$lookRight = ($right < $side) ? true : false;
$lookBottom = ($bottom < $side) ? true : false;
if ($lookTop) {
if ($lookLeft && $grid[$top][$left] === null) {
$emptyAdjacentSquares[] = [$top, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$top, $left];
}
if ($grid[$top][$j] === null) {
$emptyAdjacentSquares[] = [$top, $j];
}
$adjacentSquares[] = [$top, $j];
if ($lookRight && $grid[$top][$right] === null) {
$emptyAdjacentSquares[] = [$top, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$top, $right];
}
}
if ($lookRight && $grid[$i][$right] === null) {
$emptyAdjacentSquares[] = [$i, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$i, $right];
}
if ($lookBottom) {
if ($lookRight && $grid[$bottom][$right] === null) {
$emptyAdjacentSquares[] = [$bottom, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$bottom, $right];
}
$adjacentSquares[] = [$bottom, $j];
if ($grid[$bottom][$j] === null) {
$emptyAdjacentSquares[] = [$bottom, $j];
}
if ($lookLeft && $grid[$bottom][$left] === null) {
$emptyAdjacentSquares[] = [$bottom, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$bottom, $left];
}
}
if ($lookLeft && $grid[$i][$left] === null) {
$emptyAdjacentSquares[] = [$i, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$i, $left];
}
return ($returnEmpty) ? $emptyAdjacentSquares : $adjacentSquares;
}
private function getEmptySquares($grid, $side)
{
$emptySquares = [];
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
if ($grid[$i][$j] === null) {
$emptySquares[] = [$i, $j];
}
}
}
return $emptySquares;
}
}
<?php
namespace MagicWordBundle\Manager\Letter;
use JMS\DiExtraBundle\Annotation as DI;
use LexiconBundle\Entity\Language;
/**
* @DI\Service("mw_manager.letter_language")
*/
class LetterLanguageManager
{
protected $em;
/**
* @DI\InjectParams({
* "entityManager" = @DI\Inject("doctrine.orm.entity_manager"),
* })
*/
public function __construct($entityManager)
{
$this->em = $entityManager;
}
public function lottery(Language $language, $side, $debug = false)
{
$fileBigrams = $language->getDirectory().DIRECTORY_SEPARATOR."bigrams.txt";
// On crée un tableau de bigrammes
// où chaque bigramme apparait autant de fois que son poids
$bigrams = [];
$lines = file($fileBigrams);
$lettersFromBigrams=[];
array_shift($lines);
foreach ($lines as $line) {
$tab = explode("\t", $line);
$string = $tab[0];
$poids = $tab[3];
$bigramLetters = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY);
foreach ($bigramLetters as $bigramLetter) {
$lettersFromBigrams[$bigramLetter] = 1;
}
for ($j = 0; $j < $poids; $j++) {
$bigrams[] = $string;
}
}
$countsBigram = array_count_values($bigrams);
// var_dump($countsBigram);
// die();
// // On initialise une grille vide.
$grid = [];
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
$grid[$i][$j] = null;
}
}
$emptySquares = $this->getEmptySquares($grid, $side);
//tant qu'il reste des cases vides
while (!empty($emptySquares)) {
$bigram = $bigrams[array_rand($bigrams)];
$emptySquare = $emptySquares[array_rand($emptySquares)];
$emptySquareI = $emptySquare[0];
$emptySquareJ = $emptySquare[1];
$chars = preg_split('//u', $bigram, -1, PREG_SPLIT_NO_EMPTY);
$firstLetter = $chars[0];
$secondLetter = $chars[1];
$flagBigram = false;
// on regarde si une lettre du bigram pioché est déjà présente et qu'elle a des adjacent vide.
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
if ($grid[$i][$j] == $firstLetter) {
$ajdacentsLetterBigram = $this->getAdjacentSquares($grid, $side, [$i, $j], true);
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->getAdjacentSquares($grid, $side, [$i, $j], true);
if (!empty($ajdacentsLetterBigram)) {
$ajdacentLetterBigram = $ajdacentsLetterBigram[array_rand($ajdacentsLetterBigram)];
$grid[$ajdacentLetterBigram[0]][$ajdacentLetterBigram[1]] = $firstLetter;
$flagBigram = true;
break 2;
}
}
}
}
if (!$flagBigram) {
$ajdacentEmptySquares = $this->getAdjacentSquares($grid, $side, $emptySquare, true);
if (empty($ajdacentEmptySquares)) {
$ajdacentSquares = $this->getAdjacentSquares($grid, $side, $emptySquare, false);
$letterToScore=[];
foreach ($lettersFromBigrams as $key => $value) {
$score=0;
foreach ($ajdacentSquares as $ajdacentSquare) {
$letter = $grid[$ajdacentSquare[0]][$ajdacentSquare[1]];
if (isset($countsBigram[$letter.$key])) {
$score += $countsBigram[$letter.$key];
} elseif (isset($countsBigram[$key.$letter])) {
$score += $countsBigram[$key.$letter];
}
}
$letterToScore[$key] = $score;
}
arsort($letterToScore);
reset($letterToScore);
$bestLetter = key($letterToScore);
$grid[$emptySquareI][$emptySquareJ] = $bestLetter;
} else {
$randomAdjacent = $ajdacentEmptySquares[array_rand($ajdacentEmptySquares)];
$grid[$emptySquareI][$emptySquareJ] = $firstLetter;
$grid[$randomAdjacent[0]][$randomAdjacent[1]] = $secondLetter;
}
}
$emptySquares = $this->getEmptySquares($grid, $side);
}
$letters = [];
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
$letters[] = $grid[$i][$j];
}
}
return $letters;
}
private function getAdjacentSquares($grid, $side, $square, $returnEmpty = true)
{
$emptyAdjacentSquares = [];
$i = $square[0];
$j = $square[1];
$left = $i - 1;
$right = $i + 1;
$top = $j - 1;
$bottom = $j + 1;
$lookLeft = ($left >= 0) ? true : false;
$lookTop = ($top >= 0) ? true : false;
$lookRight = ($right < $side) ? true : false;
$lookBottom = ($bottom < $side) ? true : false;
if ($lookTop) {
if ($lookLeft && $grid[$top][$left] === null) {
$emptyAdjacentSquares[] = [$top, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$top, $left];
}
if ($grid[$top][$j] === null) {
$emptyAdjacentSquares[] = [$top, $j];
}
$adjacentSquares[] = [$top, $j];
if ($lookRight && $grid[$top][$right] === null) {
$emptyAdjacentSquares[] = [$top, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$top, $right];
}
}
if ($lookRight && $grid[$i][$right] === null) {
$emptyAdjacentSquares[] = [$i, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$i, $right];
}
if ($lookBottom) {
if ($lookRight && $grid[$bottom][$right] === null) {
$emptyAdjacentSquares[] = [$bottom, $right];
} elseif ($lookRight) {
$adjacentSquares[] = [$bottom, $right];
}
$adjacentSquares[] = [$bottom, $j];
if ($grid[$bottom][$j] === null) {
$emptyAdjacentSquares[] = [$bottom, $j];
}
if ($lookLeft && $grid[$bottom][$left] === null) {
$emptyAdjacentSquares[] = [$bottom, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$bottom, $left];
}
}
if ($lookLeft && $grid[$i][$left] === null) {
$emptyAdjacentSquares[] = [$i, $left];
} elseif ($lookLeft) {
$adjacentSquares[] = [$i, $left];
}
return ($returnEmpty) ? $emptyAdjacentSquares : $adjacentSquares;
}
private function getEmptySquares($grid, $side)
{
$emptySquares = [];
for ($i=0; $i < $side; $i++) {
for ($j=0; $j < $side; $j++) {
if ($grid[$i][$j] === null) {
$emptySquares[] = [$i, $j];
}
}
}
return $emptySquares;
}
}
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