Skip to content

Commit 9b96d25

Browse files
authored
Merge pull request #75 from yooper/fixed_pr_issues
Fixed pr issues
2 parents 08cf657 + 37b5a28 commit 9b96d25

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"php": ">=7.4",
2424
"yooper/stop-words": "~1",
2525
"symfony/console": ">= 4.4",
26-
"wamania/php-stemmer": "~1",
26+
"wamania/php-stemmer": "^1.0 || ^2.0 || ^3.0",
2727
"yooper/nicknames": "~1"
2828
},
2929
"require-dev": {

src/Stemmers/SnowballStemmer.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace TextAnalysis\Stemmers;
55

66
use TextAnalysis\Interfaces\IStemmer;
7+
use Wamania\Snowball\StemmerFactory;
8+
79

810
/**
911
* A wrapper around PHP native snowball implementation
@@ -18,17 +20,27 @@ class SnowballStemmer implements IStemmer
1820
* @var \Wamania\Snowball\Stem
1921
*/
2022
protected $stemmer;
21-
22-
public function __construct($stemmerType = 'English')
23+
24+
/**
25+
* @throws \Wamania\Snowball\NotFoundException
26+
*/
27+
public function __construct(string $stemmerType = 'English')
2328
{
24-
$className = self::BASE_NAMESPACE.$stemmerType;
25-
if(!class_exists($className)) {
26-
throw new \RuntimeException("Class {$stemmerType} does not exist");
29+
$version = (int)\Composer\InstalledVersions::getVersion('wamania/php-stemmer')[0];
30+
if($version === 1){
31+
$className = self::BASE_NAMESPACE.$stemmerType;
32+
if(!class_exists($className)) {
33+
throw new \RuntimeException("Class {$stemmerType} does not exist");
34+
}
35+
$this->stemmer = new $className();
36+
}
37+
// support version 2 and above
38+
else {
39+
$this->stemmer = StemmerFactory::create (strtolower($stemmerType));
2740
}
28-
$this->stemmer = new $className();
2941
}
30-
31-
public function stem($token)
42+
43+
public function stem($token) : string
3244
{
3345
return $this->stemmer->stem($token);
3446
}

tests/TextAnalysis/Comparisons/CosineSimilarityComparisonTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testIdentical()
1616
$text1 = ["hiking" , "camping", "swimming"];
1717
$text2 = ["hiking" , "camping", "swimming"];
1818
$compare = new CosineSimilarityComparison();
19-
$this->assertEquals(1.0, $compare->similarity($text1, $text2));
19+
$this->assertEquals(1.0, round($compare->similarity($text1, $text2), 1));
2020

2121
}
2222

tests/TextAnalysis/Sentiment/VaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testBoostExclamationPoints()
4444
$this->assertEquals(0, $vader->boostExclamationPoints(['empty']));
4545
$this->assertEquals(0.292, $vader->boostExclamationPoints(array_fill(0,1,'!')));
4646
$this->assertEquals(0.584, $vader->boostExclamationPoints(array_fill(0,2,'!')));
47-
$this->assertEquals(0.876, $vader->boostExclamationPoints(array_fill(0,3,'!')));
47+
$this->assertEquals(0.876, round($vader->boostExclamationPoints(array_fill(0,3,'!')), 5));
4848
$this->assertEquals(1.168, $vader->boostExclamationPoints(array_fill(0,4,'!')));
4949
$this->assertEquals(1.168, $vader->boostExclamationPoints(array_fill(0,5,'!')));
5050
}

0 commit comments

Comments
 (0)