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

Word Order Randomizer Transformer. #15

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
22 changes: 22 additions & 0 deletions docs/transformers/word-randomizer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<span style="float:right;"><a href="https://github.com/RubixML/ML/blob/master/src/Transformers/WordOrderRandomizer.php">[source]</a></span>

# Word Randomizer
Splits the given text based on a separator and then shuffles it.

**Interfaces:** [Transformer](api.md#transformer)

**Data Type Compatibility:** Categorical

## Parameters
| # | Name | Default | Type | Description |
|---|-----------|---------|--------|-----------------------------------------------------------------|
| 1 | separator | ' ' | string | Should the transformer split the string based on ' ' character? |

## Example
```php
use Rubix\ML\Transformers\WordOrderRandomizer;
$transformer = new WordOrderRandomizer();
```

## Additional Methods
This transformer does not have any additional methods.
85 changes: 85 additions & 0 deletions src/Transformers/WordOrderRandomizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Rubix\ML\Transformers;

use Rubix\ML\DataType;

use function is_string;
use function array_walk;

/**
* Word Order Randomizer
*
* This transformer shuffles the words in a given string.
*
* @category Machine Learning
* @package Rubix/ML
* @author Stylianos Tzourelis
*/
class WordOrderRandomizer implements Transformer
{
/**
* The separator to split the string with.
*
* @var string
*/
protected string $separator;

/**
* @param string $separator
*/
public function __construct(string $separator = ' ')
{
$this->separator = $separator;
}

/**
* Return the data types that this transformer is compatible with.
*
* @internal
*
* @return list<\Rubix\ML\DataType>
*/
public function compatibility() : array
{
return DataType::all();
}

/**
* Transform the dataset in place.
*
* @param array<mixed[]> $samples
*/
public function transform(array &$samples) : void
{
array_walk($samples, [$this, 'randomize']);
}

/**
* Randomize the text in a sample.
*
* @param list<mixed> $sample
*/
private function randomize(array &$sample) : void
{
foreach ($sample as &$value) {
if (is_string($value) && !empty($this->separator)) {
$value = explode($this->separator, $value);
shuffle($value);
$value = implode($this->separator, $value);
}
}
}

/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return 'Word Order Randomizer';
}
}
61 changes: 61 additions & 0 deletions tests/Transformers/WordOrderRandomizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Rubix\ML\Tests\Transformers;

use Rubix\ML\Datasets\Unlabeled;
use Rubix\ML\Transformers\Transformer;
use PHPUnit\Framework\TestCase;
use Rubix\ML\Transformers\WordOrderRandomizer;

/**
* @group Transformers
* @covers \Rubix\ML\Transformers\WordOrderRandomizer
*/
class WordOrderRandomizerTest extends TestCase
{
/**
* @var \Rubix\ML\Datasets\Unlabeled
*/
protected $dataset;

/**
* @var \Rubix\ML\Transformers\WordOrderRandomizer
*/
protected $transformer;

/**
* @before
*/
protected function setUp() : void
{
$this->dataset = Unlabeled::quick([
['Red dining chair.'],
['Blue,cotton,pillow'],
]);

$this->transformer = new WordOrderRandomizer();
}

/**
* @test
*/
public function build() : void
{
$this->assertInstanceOf(WordOrderRandomizer::class, $this->transformer);
$this->assertInstanceOf(Transformer::class, $this->transformer);
}

/**
* @test
*/
public function transform() : void
{
$this->dataset->apply($this->transformer);

foreach (explode(' ', 'Red dining chair.') as $word) {
$this->assertTrue(str_contains($this->dataset->samples()[0][0], $word));
}

$this->assertEquals(['Blue,cotton,pillow'], $this->dataset->samples()[1]);
}
}