2016-05-03 21:28:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types = 1);
|
|
|
|
|
|
|
|
namespace Phpml\FeatureExtraction;
|
|
|
|
|
|
|
|
use Phpml\Tokenization\Tokenizer;
|
2016-06-16 07:00:10 +00:00
|
|
|
use Phpml\Transformer;
|
2016-05-03 21:28:29 +00:00
|
|
|
|
2016-06-15 12:09:49 +00:00
|
|
|
class TokenCountVectorizer implements Transformer
|
2016-05-03 21:28:29 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Tokenizer
|
|
|
|
*/
|
|
|
|
private $tokenizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $minDF;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $vocabulary;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $frequencies;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Tokenizer $tokenizer
|
|
|
|
* @param float $minDF
|
|
|
|
*/
|
|
|
|
public function __construct(Tokenizer $tokenizer, float $minDF = 0)
|
|
|
|
{
|
|
|
|
$this->tokenizer = $tokenizer;
|
|
|
|
$this->minDF = $minDF;
|
|
|
|
$this->vocabulary = [];
|
|
|
|
$this->frequencies = [];
|
|
|
|
}
|
|
|
|
|
2016-06-16 22:08:10 +00:00
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
*/
|
|
|
|
public function fit(array $samples)
|
|
|
|
{
|
2016-06-16 22:33:48 +00:00
|
|
|
$this->buildVocabulary($samples);
|
2016-06-16 22:08:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 21:28:29 +00:00
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
*/
|
2016-06-16 08:01:40 +00:00
|
|
|
public function transform(array &$samples)
|
2016-05-03 21:28:29 +00:00
|
|
|
{
|
2016-06-16 22:33:48 +00:00
|
|
|
foreach ($samples as &$sample) {
|
|
|
|
$this->transformSample($sample);
|
2016-05-03 21:28:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 22:33:48 +00:00
|
|
|
$this->checkDocumentFrequency($samples);
|
2016-05-03 21:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getVocabulary()
|
|
|
|
{
|
|
|
|
return array_flip($this->vocabulary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-14 07:58:11 +00:00
|
|
|
* @param array $samples
|
|
|
|
*/
|
|
|
|
private function buildVocabulary(array &$samples)
|
|
|
|
{
|
|
|
|
foreach ($samples as $index => $sample) {
|
|
|
|
$tokens = $this->tokenizer->tokenize($sample);
|
|
|
|
foreach ($tokens as $token) {
|
|
|
|
$this->addTokenToVocabulary($token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 22:33:48 +00:00
|
|
|
* @param string $sample
|
2016-05-03 21:28:29 +00:00
|
|
|
*/
|
2016-06-16 22:33:48 +00:00
|
|
|
private function transformSample(string &$sample)
|
2016-05-03 21:28:29 +00:00
|
|
|
{
|
|
|
|
$counts = [];
|
2016-06-16 22:33:48 +00:00
|
|
|
$tokens = $this->tokenizer->tokenize($sample);
|
2016-06-14 07:58:11 +00:00
|
|
|
|
2016-05-03 21:28:29 +00:00
|
|
|
foreach ($tokens as $token) {
|
|
|
|
$index = $this->getTokenIndex($token);
|
2016-06-16 22:34:15 +00:00
|
|
|
if (false !== $index) {
|
2016-06-16 22:33:48 +00:00
|
|
|
$this->updateFrequency($token);
|
|
|
|
if (!isset($counts[$index])) {
|
|
|
|
$counts[$index] = 0;
|
|
|
|
}
|
2016-05-03 21:28:29 +00:00
|
|
|
|
2016-06-16 22:33:48 +00:00
|
|
|
++$counts[$index];
|
|
|
|
}
|
2016-05-03 21:28:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 07:58:11 +00:00
|
|
|
foreach ($this->vocabulary as $index) {
|
|
|
|
if (!isset($counts[$index])) {
|
|
|
|
$counts[$index] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 22:33:48 +00:00
|
|
|
$sample = $counts;
|
2016-05-03 21:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token
|
|
|
|
*
|
2016-06-16 22:33:48 +00:00
|
|
|
* @return int|bool
|
2016-06-14 07:58:11 +00:00
|
|
|
*/
|
2016-06-16 22:33:48 +00:00
|
|
|
private function getTokenIndex(string $token)
|
2016-06-14 07:58:11 +00:00
|
|
|
{
|
2016-06-16 22:33:48 +00:00
|
|
|
return isset($this->vocabulary[$token]) ? $this->vocabulary[$token] : false;
|
2016-06-14 07:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token
|
2016-05-03 21:28:29 +00:00
|
|
|
*/
|
2016-06-14 07:58:11 +00:00
|
|
|
private function addTokenToVocabulary(string $token)
|
2016-05-03 21:28:29 +00:00
|
|
|
{
|
|
|
|
if (!isset($this->vocabulary[$token])) {
|
|
|
|
$this->vocabulary[$token] = count($this->vocabulary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token
|
|
|
|
*/
|
|
|
|
private function updateFrequency(string $token)
|
|
|
|
{
|
|
|
|
if (!isset($this->frequencies[$token])) {
|
|
|
|
$this->frequencies[$token] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
++$this->frequencies[$token];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-06-16 22:33:48 +00:00
|
|
|
private function checkDocumentFrequency(array &$samples)
|
2016-05-03 21:28:29 +00:00
|
|
|
{
|
|
|
|
if ($this->minDF > 0) {
|
|
|
|
$beyondMinimum = $this->getBeyondMinimumIndexes(count($samples));
|
2016-06-16 22:33:48 +00:00
|
|
|
foreach ($samples as &$sample) {
|
|
|
|
$this->resetBeyondMinimum($sample, $beyondMinimum);
|
2016-05-03 21:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $sample
|
|
|
|
* @param array $beyondMinimum
|
|
|
|
*/
|
2016-06-16 22:33:48 +00:00
|
|
|
private function resetBeyondMinimum(array &$sample, array $beyondMinimum)
|
2016-05-03 21:28:29 +00:00
|
|
|
{
|
|
|
|
foreach ($beyondMinimum as $index) {
|
2016-06-14 07:58:11 +00:00
|
|
|
$sample[$index] = 0;
|
2016-05-03 21:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $samplesCount
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getBeyondMinimumIndexes(int $samplesCount)
|
|
|
|
{
|
|
|
|
$indexes = [];
|
|
|
|
foreach ($this->frequencies as $token => $frequency) {
|
|
|
|
if (($frequency / $samplesCount) < $this->minDF) {
|
|
|
|
$indexes[] = $this->getTokenIndex($token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $indexes;
|
|
|
|
}
|
|
|
|
}
|