Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"voku/portable-utf8": "^5.4|^6.0"
},
"require-dev":{
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^9.0",
"rector/rector": "^2.4"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 7 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
colors="true"
beStrictAboutChangesToGlobalState="true"
Expand All @@ -9,16 +9,14 @@
beStrictAboutTodoAnnotatedTests="true"
verbose="true"
bootstrap="vendor/autoload.php">

<coverage>
<include>
<directory>src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="functional">
<directory suffix=".php">test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
24 changes: 24 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/test',
])
// uncomment to reach your current PHP version
->withPhpSets(
php85: true
)
->withPreparedSets(
codeQuality: true,
deadCode: true,
typeDeclarations: true,
)
->withComposerBased(
phpunit: true,
)
;
2 changes: 2 additions & 0 deletions src/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Wamania\Snowball;

class NotFoundException extends \Exception
Expand Down
22 changes: 10 additions & 12 deletions src/Stemmer/Catalan.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Catalan extends Stem
/**
* {@inheritdoc}
*/
public function stem($word)
public function stem($word): string
{
// we do ALL in UTF-8
if (!UTF8::is_utf8($word)) {
Expand All @@ -106,7 +106,7 @@ public function stem($word)

// Step 1b: Verb suffix
// Do step 1b if no ending was removed by step 1a.
if ($this->word == $word) {
if ($this->word === $word) {
$this->step1b();
}

Expand All @@ -123,21 +123,19 @@ public function stem($word)
* and delete it in R1.
*/

private function step0()
private function step0(): bool
{
if (($position = $this->search(static::$attached_pronoun)) !== false) {
if ($this->inR1($position)) {
$this->word = UTF8::substr($this->word, 0, $position);
return true;
}
if ($position = $this->search(static::$attached_pronoun) !== false && $this->inR1($position)) {
$this->word = UTF8::substr($this->word, 0, $position);
return true;
}
return false;
}

/**
* Step 1a: Standard suffix
*/
private function step1a()
private function step1a(): bool
{
// Run step 1a.2 before 1a.1, since they overlap on `cions` (1a.1) and `acions` (1a.2)
//
Expand Down Expand Up @@ -213,7 +211,7 @@ private function step1a()
* Search for the longest among the following suffixes in r1 and r2, and
* perform the action indicated.
*/
private function step1b()
private function step1b(): bool
{
// Step 1b.1
//
Expand Down Expand Up @@ -263,7 +261,7 @@ private function step1b()
* Search for the longest among the following suffixes in R1, and perform
* the action indicated.
*/
private function step2()
private function step2(): bool
{
// Step 2.1
// residual suffix
Expand Down Expand Up @@ -292,7 +290,7 @@ private function step2()
* And finally:
* Remove accents and l aggeminades
*/
private function finish()
private function finish(): void
{
$this->word = str_replace(
['á', 'é', 'í', 'ó', 'ú', 'à', 'è', 'ì', 'ò', 'ï', 'ü', '·'],
Expand Down
31 changes: 16 additions & 15 deletions src/Stemmer/Danish.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Danish extends Stem
/**
* All danish vowels
*/
protected static $vowels = array('a', 'e', 'i', 'o', 'u', 'y', 'æ', 'å', 'ø');
protected static $vowels = ['a', 'e', 'i', 'o', 'u', 'y', 'æ', 'å', 'ø'];

/**
* {@inheritdoc}
Expand Down Expand Up @@ -52,86 +52,87 @@ public function stem($word): string
* a b c d f g h j k l m n o p r t v y z å
*
* @param string $ending
* @return boolean
*/
private function hasValidSEnding($word)
private function hasValidSEnding(string $word): bool
{
$lastLetter = UTF8::substr($word, -1, 1);
return in_array($lastLetter, array('a', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 't', 'v', 'y', 'z', 'å'));
return in_array($lastLetter, ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 't', 'v', 'y', 'z', 'å']);
}

/**
* Step 1
* Search for the longest among the following suffixes in R1, and perform the action indicated.
*/
private function step1()
private function step1(): ?bool
{
// hed ethed ered e erede ende erende ene erne ere en heden eren er heder erer
// heds es endes erendes enes ernes eres ens hedens erens ers ets erets et eret
// delete
if ( ($position = $this->searchIfInR1(array(
if ( ($position = $this->searchIfInR1([
'erendes', 'erende', 'hedens', 'erede', 'ethed', 'heden', 'endes', 'erets', 'heder', 'ernes',
'erens', 'ered', 'ende', 'erne', 'eres', 'eren', 'eret', 'erer', 'enes', 'heds',
'ens', 'ene', 'ere', 'ers', 'ets', 'hed', 'es', 'et', 'er', 'en', 'e'
))) !== false) {
])) !== false) {
$this->word = UTF8::substr($this->word, 0, $position);
return true;
}

// s
// delete if preceded by a valid s-ending
if ( ($position = $this->searchIfInR1(array('s'))) !== false) {
if ( ($position = $this->searchIfInR1(['s'])) !== false) {
$word = UTF8::substr($this->word, 0, $position);
if ($this->hasValidSEnding($word)) {
$this->word = $word;
}
return true;
}
return null;
}

/**
* Step 2
* Search for one of the following suffixes in R1, and if found delete the last letter.
* gd dt gt kt
*/
private function step2()
private function step2(): void
{
if ($this->searchIfInR1(array('gd', 'dt', 'gt', 'kt')) !== false) {
if ($this->searchIfInR1(['gd', 'dt', 'gt', 'kt']) !== false) {
$this->word = UTF8::substr($this->word, 0, -1);
}
}

/**
* Step 3:
*/
private function step3()
private function step3(): ?bool
{
// If the word ends igst, remove the final st.
if ($this->search(array('igst')) !== false) {
if ($this->search(['igst']) !== false) {
$this->word = UTF8::substr($this->word, 0, -2);
}

// Search for the longest among the following suffixes in R1, and perform the action indicated.
// ig lig elig els
// delete, and then repeat step 2
if ( ($position = $this->searchIfInR1(array('elig', 'lig', 'ig', 'els'))) !== false) {
if ( ($position = $this->searchIfInR1(['elig', 'lig', 'ig', 'els'])) !== false) {
$this->word = UTF8::substr($this->word, 0, $position);
$this->step2();
return true;
}

// løst
// replace with løs
if ($this->searchIfInR1(array('løst')) !== false) {
if ($this->searchIfInR1(['løst']) !== false) {
$this->word = UTF8::substr($this->word, 0, -1);
}
return null;
}

/**
* Step 4: undouble
* If the word ends with double consonant in R1, remove one of the consonants.
*/
private function step4()
private function step4(): bool
{
$length = UTF8::strlen($this->word);
if (!$this->inR1(($length-1))) {
Expand Down
Loading
Loading