diff --git a/src/Camspiers/StatisticalClassifier/Document/Document.php b/src/Camspiers/StatisticalClassifier/Document/Document.php new file mode 100644 index 00000000..61fd173a --- /dev/null +++ b/src/Camspiers/StatisticalClassifier/Document/Document.php @@ -0,0 +1,84 @@ +exchangeArray($data); + } + } + + public function offsetSet($token, $count) + { + if (is_callable(static::$tokenNormalizer)) { + $token = call_user_func(static::$tokenNormalizer, $token); + } + + parent::offsetSet($token, $count); + } + + /** + * @param callable $normalizer + */ + public static function setDocumentNormalizer(callable $normalizer) + { + static::$documentNormalizer = $normalizer; + } + + /** + * @param callable $tokenizer + */ + public static function setTokenizer(callable $tokenizer) + { + static::$tokenizer = $tokenizer; + } + + /** + * @param callable $normalizer + */ + public static function setTokenNormalizer(callable $normalizer) + { + static::$tokenNormalizer = $normalizer; + } +} diff --git a/src/Camspiers/StatisticalClassifier/Document/DocumentInterface.php b/src/Camspiers/StatisticalClassifier/Document/DocumentInterface.php new file mode 100644 index 00000000..b9fa680c --- /dev/null +++ b/src/Camspiers/StatisticalClassifier/Document/DocumentInterface.php @@ -0,0 +1,20 @@ +tokenize($document); + } + /** * @{inheritdoc} */ diff --git a/tests/Camspiers/StatisticalClassifier/Document/DocumentTest.php b/tests/Camspiers/StatisticalClassifier/Document/DocumentTest.php new file mode 100644 index 00000000..d2702262 --- /dev/null +++ b/tests/Camspiers/StatisticalClassifier/Document/DocumentTest.php @@ -0,0 +1,73 @@ + 2, 'data' => 1, 'in' => 1, 'a' => 1, 'Some' => 1], + ], + [ + ['Some', 'document', 'data', 'in', 'a', 'document'], + ['document' => 2, 'data' => 1, 'in' => 1, 'a' => 1, 'Some' => 1], + ], + [ + new \ArrayObject(['Some', 'document', 'data', 'in', 'a', 'document']), + ['document' => 2, 'data' => 1, 'in' => 1, 'a' => 1, 'Some' => 1], + ], + [ + new \stdClass(), + 'InvalidArgumentException', + ], + ]; + } + + /** + * @covers \Camspiers\StatisticalClassifier\Document\Document::__construct + * @dataProvider constructDataProvider + * @param mixed $data + * @param string|array $expected + */ + public function testConstruct($data, $expected) + { + if (is_string($expected)) { + $this->setExpectedException($expected); + } + + $document = new Document($data); + + $this->assertEquals($document->getArrayCopy(), (array) $document); + $this->assertEquals($expected, $document->getArrayCopy()); + } + + public function testExchangeArrayUsesOffsetSet() + { + Document::setTokenNormalizer('strtolower'); + + $document = new Document(); + $document->Some = 1; + $document->document = 2; + $document->data = 1; + $document->in = 1; + $document->a = 1; + + $this->assertSame( + ['some' => 1, 'document' => 2, 'data' => 1, 'in' => 1, 'a' => 1], + (array) $document + ); + $this->assertCount(5, $document); // Token frequency, thus, document is counted only once + } + + public function testIsCountable() + { + $document = new Document(['Some', 'document', 'data', 'in', 'a', 'document']); + + $this->assertCount(5, $document); // Token frequency, thus, document is counted only once + } +}