Skip to content

Commit

Permalink
Upgrade to php-stemmer 2.0
Browse files Browse the repository at this point in the history
Refactoring stemmer integration by reducing the seperated classes and
using the stemmer factory to create a stemmer object.
This reduces maintenance because no change in Joomla! is required to
support new languages added by the upstream php-stemmer class
and we reduce complexity in Joomla! code base.
  • Loading branch information
HLeithner committed Jan 19, 2020
1 parent 8837da1 commit a2e5c36
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 1,864 deletions.
67 changes: 51 additions & 16 deletions administrator/components/com_finder/Indexer/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

defined('_JEXEC') or die;

use Exception;
use Joomla\String\StringHelper;
use Wamania\Snowball\NotFoundException;
use Wamania\Snowball\StemmerFactory;
use Wamania\Snowball\Stemmer\Stemmer;

/**
* Language support class for the Finder indexer package.
Expand Down Expand Up @@ -44,6 +48,43 @@ class Language
*/
public $spacer = ' ';

/**
* The stemmer object.
*
* @var Stemmer
* @since 4.0.0
*/
protected $stemmer = null;

/**
* Method to construct the language object.
*
* @since 4.0.0
*
* @throws Exception
*/
public function __construct($locale = null)
{
if ($locale !== null)
{
$this->language = $locale;
}

if ($this->language === null)
{
throw new Exception('Can\'t initial stemmer, locale is not set', 500);
}

try
{
$this->stemmer = StemmerFactory::create($this->language);
}
catch (NotFoundException $e)
{
// We don't have a stemmer for the language
}
}

/**
* Method to get a language support object.
*
Expand All @@ -60,26 +101,15 @@ public static function getInstance($language)
return self::$instances[$language];
}

if ($language == '*')
{
self::$instances[$language] = new self;
$locale = '*';

return self::$instances[$language];
}

$locale = Helper::getPrimaryLanguage($language);
$class = '\\Joomla\\Component\\Finder\\Administrator\\Indexer\\Language\\' . ucfirst($locale);

if (class_exists($class))
{
self::$instances[$language] = new $class;
}
else
if ($language !== '*')
{
self::$instances[$language] = new self;
self::$instances[$language]->language = $locale;
$locale = Helper::getPrimaryLanguage($language);
}

self::$instances[$language] = new self($locale);

return self::$instances[$language];
}

Expand Down Expand Up @@ -137,6 +167,11 @@ public function tokenise($input)
*/
public function stem($token)
{
if ($this->stemmer !== null)
{
return $this->stemmer->stem($token);
}

return $token;
}
}
62 changes: 0 additions & 62 deletions administrator/components/com_finder/Indexer/Language/Da.php

This file was deleted.

62 changes: 0 additions & 62 deletions administrator/components/com_finder/Indexer/Language/De.php

This file was deleted.

Loading

0 comments on commit a2e5c36

Please sign in to comment.