Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New document class #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/Camspiers/StatisticalClassifier/Document/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
namespace Camspiers\StatisticalClassifier\Document;

use Camspiers\StatisticalClassifier\Normalizer\Token;
use Camspiers\StatisticalClassifier\Tokenizer\Word;

class Document
extends \ArrayObject
implements DocumentInterface
{
/**
* @var callable $normalizer
*/
public static $documentNormalizer;

/**
* @var callable $tokenizer
*/
public static $tokenizer;

/**
* @var callable $normalizer
*/
public static $tokenNormalizer;

/**
* @param string|array|Traversable $data
* @throws \InvalidArgumentException
*/
public function __construct($data = null)
{
parent::setFlags(parent::ARRAY_AS_PROPS);

if (! empty($data)) {
if (is_scalar($data)) {
if (is_callable(static::$documentNormalizer)) {
$data = call_user_func(static::$documentNormalizer, $data);
}

$data = call_user_func(static::$tokenizer ?: new Word(), $data);
} elseif (! is_array($data) and ! $data instanceof \Traversable) {
$type = gettype($data);
throw new \InvalidArgumentException("Data must be either a string or an array or a traversable, $type given.");
}

$data = array_count_values((array) $data);

$this->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;
}
}
20 changes: 20 additions & 0 deletions src/Camspiers/StatisticalClassifier/Document/DocumentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Camspiers\StatisticalClassifier\Document;

interface DocumentInterface
{
/**
* @param callable $normalizer
*/
public static function setDocumentNormalizer(callable $normalizer);

/**
* @param callable $tokenizer
*/
public static function setTokenizer(callable $tokenizer);

/**
* @param callable $normalizer
*/
public static function setTokenNormalizer(callable $normalizer);
}
5 changes: 5 additions & 0 deletions src/Camspiers/StatisticalClassifier/Tokenizer/Word.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
class Word implements TokenizerInterface
{
public function __invoke($document)
{
return $this->tokenize($document);
}

/**
* @{inheritdoc}
*/
Expand Down
73 changes: 73 additions & 0 deletions tests/Camspiers/StatisticalClassifier/Document/DocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace Camspiers\StatisticalClassifier\Document;

/**
* @backupStaticAttributes disabled
*/
class DocumentTest extends \PHPUnit_Framework_TestCase
{
public function constructDataProvider()
{
return [
[
'Some document data in a document',
['document' => 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
}
}