Skip to content

Commit

Permalink
change transformer behavior to reference
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Jun 16, 2016
1 parent 15519ba commit 7c5e79d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 22 deletions.
6 changes: 1 addition & 5 deletions src/Phpml/FeatureExtraction/TfIdfTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ class TfIdfTransformer implements Transformer

/**
* @param array $samples
*
* @return array
*/
public function transform(array $samples): array
public function transform(array &$samples)
{
$this->countTokensFrequency($samples);

Expand All @@ -32,8 +30,6 @@ public function transform(array $samples): array
$feature = $feature * $this->idf[$index];
}
}

return $samples;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Phpml/FeatureExtraction/TokenCountVectorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public function __construct(Tokenizer $tokenizer, float $minDF = 0)

/**
* @param array $samples
*
* @return array
*/
public function transform(array $samples): array
public function transform(array &$samples)
{
$this->buildVocabulary($samples);

Expand All @@ -60,8 +58,6 @@ public function transform(array $samples): array
}

$samples = $this->checkDocumentFrequency($samples);

return $samples;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Phpml/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Pipeline implements Estimator

/**
* @param array|Transformer[] $transformers
* @param Estimator $estimator
* @param Estimator $estimator
*/
public function __construct(array $transformers = [], Estimator $estimator)
{
Expand Down Expand Up @@ -76,11 +76,11 @@ public function train(array $samples, array $targets)

/**
* @param array $samples
*
* @return mixed
*/
public function predict(array $samples)
{
return $this->estimator->predict($samples);
}

}
4 changes: 1 addition & 3 deletions src/Phpml/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ interface Transformer
{
/**
* @param array $samples
*
* @return array
*/
public function transform(array $samples): array;
public function transform(array &$samples);
}
3 changes: 2 additions & 1 deletion tests/Phpml/FeatureExtraction/TfIdfTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function testTfIdfTransformation()
];

$transformer = new TfIdfTransformer();
$transformer->transform($samples);

$this->assertEquals($tfIdfSamples, $transformer->transform($samples), '', 0.001);
$this->assertEquals($tfIdfSamples, $samples, '', 0.001);
}
}
9 changes: 6 additions & 3 deletions tests/Phpml/FeatureExtraction/TokenCountVectorizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public function testTokenCountVectorizerWithWhitespaceTokenizer()
];

$vectorizer = new TokenCountVectorizer(new WhitespaceTokenizer());
$vectorizer->transform($samples);

$this->assertEquals($tokensCounts, $vectorizer->transform($samples));
$this->assertEquals($tokensCounts, $samples);
$this->assertEquals($vocabulary, $vectorizer->getVocabulary());
}

Expand Down Expand Up @@ -68,8 +69,9 @@ public function testMinimumDocumentTokenCountFrequency()
];

$vectorizer = new TokenCountVectorizer(new WhitespaceTokenizer(), 0.5);
$vectorizer->transform($samples);

$this->assertEquals($tokensCounts, $vectorizer->transform($samples));
$this->assertEquals($tokensCounts, $samples);
$this->assertEquals($vocabulary, $vectorizer->getVocabulary());

// word at least once in all samples
Expand All @@ -86,7 +88,8 @@ public function testMinimumDocumentTokenCountFrequency()
];

$vectorizer = new TokenCountVectorizer(new WhitespaceTokenizer(), 1);
$vectorizer->transform($samples);

$this->assertEquals($tokensCounts, $vectorizer->transform($samples));
$this->assertEquals($tokensCounts, $samples);
}
}
4 changes: 1 addition & 3 deletions tests/Phpml/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

class PipelineTest extends \PHPUnit_Framework_TestCase
{

public function testPipelineConstruction()
{
$transformers = [
new TfIdfTransformer()
new TfIdfTransformer(),
];
$estimator = new SVC();

Expand All @@ -23,5 +22,4 @@ public function testPipelineConstruction()
$this->assertEquals($transformers, $pipeline->getTransformers());
$this->assertEquals($estimator, $pipeline->getEstimator());
}

}

0 comments on commit 7c5e79d

Please sign in to comment.