php-ml/src/Phpml/FeatureExtraction/TokenCountVectorizer.php

217 lines
4.5 KiB
PHP
Raw Normal View History

<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
namespace Phpml\FeatureExtraction;
use Phpml\Tokenization\Tokenizer;
use Phpml\Transformer;
class TokenCountVectorizer implements Transformer
{
/**
* @var Tokenizer
*/
private $tokenizer;
/**
* @var StopWords
*/
private $stopWords;
/**
* @var float
*/
private $minDF;
/**
* @var array
*/
private $vocabulary;
/**
* @var array
*/
private $frequencies;
/**
* @param Tokenizer $tokenizer
* @param StopWords $stopWords
* @param float $minDF
*/
2016-12-06 07:55:52 +00:00
public function __construct(Tokenizer $tokenizer, StopWords $stopWords = null, float $minDF = 0.0)
{
$this->tokenizer = $tokenizer;
$this->stopWords = $stopWords;
$this->minDF = $minDF;
$this->vocabulary = [];
$this->frequencies = [];
}
/**
* @param array $samples
*/
public function fit(array $samples)
{
2016-06-16 22:33:48 +00:00
$this->buildVocabulary($samples);
}
/**
* @param array $samples
*/
public function transform(array &$samples)
{
2016-06-16 22:33:48 +00:00
foreach ($samples as &$sample) {
$this->transformSample($sample);
}
2016-06-16 22:33:48 +00:00
$this->checkDocumentFrequency($samples);
}
/**
* @return array
*/
public function getVocabulary()
{
return array_flip($this->vocabulary);
}
/**
* @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-06-16 22:33:48 +00:00
private function transformSample(string &$sample)
{
$counts = [];
2016-06-16 22:33:48 +00:00
$tokens = $this->tokenizer->tokenize($sample);
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-06-16 22:33:48 +00:00
++$counts[$index];
}
}
foreach ($this->vocabulary as $index) {
if (!isset($counts[$index])) {
$counts[$index] = 0;
}
}
ksort($counts);
2016-06-16 22:33:48 +00:00
$sample = $counts;
}
/**
* @param string $token
*
2016-06-16 22:33:48 +00:00
* @return int|bool
*/
2016-06-16 22:33:48 +00:00
private function getTokenIndex(string $token)
{
if ($this->isStopWord($token)) {
return false;
}
2016-06-16 22:33:48 +00:00
return isset($this->vocabulary[$token]) ? $this->vocabulary[$token] : false;
}
/**
* @param string $token
*/
private function addTokenToVocabulary(string $token)
{
if ($this->isStopWord($token)) {
return;
}
if (!isset($this->vocabulary[$token])) {
$this->vocabulary[$token] = count($this->vocabulary);
}
}
/**
* @param string $token
*
* @return bool
*/
private function isStopWord(string $token): bool
{
return $this->stopWords && $this->stopWords->isStopWord($token);
}
/**
* @param string $token
*/
private function updateFrequency(string $token)
{
if (!isset($this->frequencies[$token])) {
$this->frequencies[$token] = 0;
}
++$this->frequencies[$token];
}
/**
* @param array $samples
2016-11-20 21:53:17 +00:00
*
* @return array
*/
2016-06-16 22:33:48 +00:00
private function checkDocumentFrequency(array &$samples)
{
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);
}
}
}
/**
* @param array $sample
* @param array $beyondMinimum
*/
2016-06-16 22:33:48 +00:00
private function resetBeyondMinimum(array &$sample, array $beyondMinimum)
{
foreach ($beyondMinimum as $index) {
$sample[$index] = 0;
}
}
/**
* @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;
}
}