Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Mathieu Loiseau
MagicWord
Commits
65b78b0b
Commit
65b78b0b
authored
May 07, 2019
by
Sylvain Hatier
Committed by
Arnaud Bey
Jul 19, 2019
Browse files
WIP : Delete Lexicon Command
parent
087bb2b4
Changes
12
Hide whitespace changes
Inline
Side-by-side
application/src/LexiconBundle/Command/DeleteLexiconCommand.php
0 → 100644
View file @
65b78b0b
<?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'
;
}
}
}
application/src/LexiconBundle/Manager/BigramManager.php
View file @
65b78b0b
...
...
@@ -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
\t
Freq
\t
Poids
\t
Arrondi
\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
;
}
}
application/src/LexiconBundle/Manager/LetterManager.php
View file @
65b78b0b
...
...
@@ -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
);
}
}
...
...
application/src/MagicWordBundle/Entity/Game.php
View file @
65b78b0b
...
...
@@ -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
;
...
...
application/src/MagicWordBundle/Entity/Grid.php
View file @
65b78b0b
...
...
@@ -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
;
}
}
application/src/MagicWordBundle/Entity/Round.php
View file @
65b78b0b
...
...
@@ -64,7 +64,7 @@ class Round implements \JsonSerializable
private
$displayOrder
;
/**
* @ORM\ManyToOne(targetEntity="Grid")
* @ORM\ManyToOne(targetEntity="Grid"
, cascade={"remove"}
)
*/
private
$grid
;
...
...
application/src/MagicWordBundle/Entity/Square.php
View file @
65b78b0b
...
...
@@ -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 \
MagicWord
Bundle\Entity\
Grid $grid
* @param \
Lexicon
Bundle\Entity\
Letter|null $letter
*
* @return Square
*/
public
function
set
Grid
(
\
MagicWordBundle\Entity\Grid
$grid
=
null
)
public
function
set
Letter
(
\
LexiconBundle\Entity\Letter
$letter
=
null
)
{
$this
->
grid
=
$grid
;
$this
->
letter
=
$letter
;
return
$this
;
}
/**
* Get
grid
.
* Get
letter
.
*
* @return \
MagicWord
Bundle\Entity\
Grid
* @return \
Lexicon
Bundle\Entity\
Letter|null
*/
public
function
get
Grid
()
public
function
get
Letter
()
{
return
$this
->
grid
;
return
$this
->
letter
;
}
/**
* Set letter.
*
* @param \LexiconBundle\Entity\Letter|null $letter
* Set grid.
*
* @return Square
*/
public
function
set
Letter
(
\
LexiconBundle\Entity\Letter
$letter
=
null
)
public
function
set
Grid
(
\
MagicWordBundle\Entity\Grid
$grid
=
null
)
{
$this
->
letter
=
$letter
;
$this
->
grid
=
$grid
;
return
$this
;
}
/**
* Get
letter
.
* Get
grid
.
*
* @return \LexiconBundle\Entity\Letter|null
*/
public
function
get
Letter
()
public
function
get
Grid
()
{
return
$this
->
letter
;
return
$this
->
grid
;
}
}
application/src/MagicWordBundle/Manager/GridManager.php
View file @
65b78b0b
...
...
@@ -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
);
...
...
application/src/MagicWordBundle/Manager/Letter/BigramManager.php
deleted
100644 → 0
View file @
087bb2b4
<?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
;
}
}
application/src/MagicWordBundle/Manager/Letter/LetterLanguageManager.php
View file @
65b78b0b
...
...
@@ -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
);
}
...
...
application/src/MagicWordBundle/Manager/SquareManager.php
View file @
65b78b0b
...
...
@@ -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
);
...
...
application/src/MagicWordBundle/Repository/GameRepository.php
View file @
65b78b0b
...
...
@@ -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
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment