Skip to content
Snippets Groups Projects
Commit d160d551 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

fix #13854 : refactorings and ensure that it do not remove phonetix on terms without collision

parent 5ebc8941
Branches
Tags
5 merge requests!529Hotline 6.56,!512Master,!500Hotline 6.55,!482Dev #13854 phonetix collision,!459Dev #13854 phonetix collision
......@@ -34,12 +34,14 @@ class Class_MoteurRecherche_Terms {
$ix = $this->getIndexation();
$this->_expression = $expression;
foreach ($ix->getMots($this->_expression) as $term) {
$this->_raws[] = $term;
$this->analyseOne($term);
foreach ($ix->getMots($this->_expression) as $token) {
$this->_raws[] = $token;
$term = new Class_MoteurRecherche_Term($ix, $token);
if ($term->isValid())
$this->_terms[] = $term;
}
$this->clearPhonetixCollisions();
(new Class_MoteurRecherche_PhonetixCollector())->clean($this->_terms);
return $this;
}
......@@ -53,12 +55,11 @@ class Class_MoteurRecherche_Terms {
if (empty($this->_terms))
return '';
$against = [];
foreach ($this->_terms as $term) {
if (!$term['phonetix'])
unset($term['phonetix']);
$against[] = ($is_extended ? ' ' : '+') . '(' . implode(' ', $term) . ')';
}
$against = array_map(
function($term) use ($is_extended) {
return $term->asAgainstClause($is_extended);
},
$this->_terms);
return " AGAINST('". implode(' ', $against) ."'"
. ($is_extended ? '' : ' IN BOOLEAN MODE') . ')';
......@@ -70,54 +71,84 @@ class Class_MoteurRecherche_Terms {
}
protected function analyseOne($term) {
// if end with star remove before pluralize
protected function getIndexation() {
return (null !== $this->_indexation) ?
$this->_indexation : new Class_Indexation();
}
}
class Class_MoteurRecherche_Term {
protected $_raw, $_singular, $_plural, $_phonetix;
public function __construct($indexation, $term) {
$this->_raw = $term;
$star = '*' == substr($term, -1);
$term = $star ? substr($term, 0, strlen($term) - 1) : $term;
$ix = $this->getIndexation();
if (false === $plural = $ix->getPluriel($term))
if (false === $plural = $indexation->getPluriel($term))
return;
$this->_terms[] = ['singular' => $plural[0] . ($star ? '*': ''),
'plural' => $plural[1],
'phonetix' => $ix->phonetix($plural[0])];
$this->_singular = $plural[0] . ($star ? '*': '');
$this->_plural = $plural[1];
$this->_phonetix = $indexation->phonetix($plural[0]);
}
protected function clearPhonetixCollisions() {
// collect and count all phonetix
$collect = [];
foreach($this->_terms as $term) {
if (!$term['phonetix'])
continue;
array_key_exists($term['phonetix'], $collect) ?
$collect[$term['phonetix']]++ :
$collect[$term['phonetix']] = 1;
}
public function collectPhonetix($collector) {
if ($this->_phonetix)
$collector->add($this->_phonetix);
}
// unset phonetix with more than one appearance
foreach ($collect as $phonetix => $count)
if (1 < $count)
$this->clearPhonetix($phonetix);
public function disablePhonetix($value) {
if ($this->_phonetix == $value)
$this->_phonetix = false;
return $this;
}
protected function clearPhonetix($phonetix) {
foreach($this->_terms as &$term)
if ($term['phonetix'] == $phonetix)
$term['phonetix'] = false;
public function asAgainstClause($is_extended) {
return ($is_extended ? ' ' : '+')
. '(' . $this->_singular . ' ' . $this->_plural
. ($this->_phonetix ? ' ' . $this->_phonetix : '') .')';
}
public function isValid() {
return null !== $this->_singular;
}
}
class Class_MoteurRecherche_PhonetixCollector {
protected $_values = [];
public function clean($terms) {
array_walk($terms, function($term) { $term->collectPhonetix($this); });
foreach ($this->_values as $value => $count)
if (1 < $count)
array_walk(
$terms,
function($term) use ($value) {
$term->disablePhonetix($value);
});
}
protected function withTermsDo($terms, $closure) {
array_walk($terms, $closure);
return $this;
}
protected function getIndexation() {
if (null !== $this->_indexation)
return $this->_indexation;
return new Class_Indexation();
public function add($value) {
array_key_exists($value, $this->_values) ?
$this->_values[$value]++ :
$this->_values[$value] = 1;
}
}
?>
\ No newline at end of file
......@@ -749,10 +749,10 @@ class MoteurRecherchePhonetixCollisionTest extends MoteurRechercheTestCase {
public function shouldNotUsePhonetix() {
$this->mock_sql
->whenCalled('fetchOne')
->with("Select count(*) from notices Where MATCH(titres, auteurs, editeur, collection, matieres, dewey) AGAINST('+(JEAN JEANS) +(GENET GENETS)' IN BOOLEAN MODE)")
->with("Select count(*) from notices Where MATCH(titres, auteurs, editeur, collection, matieres, dewey) AGAINST('+(JEAN JEANS) +(GENET GENETS) +(PARCOUR PARCOURS PARKOUR)' IN BOOLEAN MODE)")
->answers(5000)
->beStrict();
$this->criteres_recherche->setParams(['expressionRecherche' => 'jean genet']);
$this->criteres_recherche->setParams(['expressionRecherche' => 'jean genet parcours']);
$retour = $this->moteur_recherche->lancerRecherche($this->criteres_recherche);
}
}
\ No newline at end of file
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