php-ml/src/FeatureExtraction/StopWords.php

37 lines
783 B
PHP
Raw Normal View History

2016-06-14 09:54:04 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-06-14 09:54:04 +00:00
namespace Phpml\FeatureExtraction;
use Phpml\Exception\InvalidArgumentException;
class StopWords
{
/**
* @var array
*/
protected $stopWords = [];
2016-06-14 09:54:04 +00:00
public function __construct(array $stopWords)
{
$this->stopWords = array_fill_keys($stopWords, true);
}
public function isStopWord(string $token): bool
2016-06-14 09:54:04 +00:00
{
return isset($this->stopWords[$token]);
}
public static function factory(string $language = 'English'): self
2016-06-14 09:54:04 +00:00
{
$className = __NAMESPACE__."\\StopWords\\${language}";
2016-06-14 09:54:04 +00:00
if (!class_exists($className)) {
throw new InvalidArgumentException(sprintf('Can\'t find "%s" language for StopWords', $language));
2016-06-14 09:54:04 +00:00
}
return new $className();
}
}