Skip to content

Commit

Permalink
#7. Add CustomSet words and phrases container + coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jul 1, 2018
1 parent c2fe70a commit 5e0a8da
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 55 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ An additional option that u may need in particular situations is to replace word
```
By default pattern is 5 dashes, so u can call only `$this->text->setReplaceable(true);` before any processor to achieve replacement with default settings.

### Creating custom data-set
```php
$customSet = new CustomSet();
$customSet->setWords([
'0.9' => ['weird']
]);
$this->text->setString('This weird text should be detected');
$this->words->setText($this->text);
$this->words->setDataSet($customSet);
$this->words->processWords();
echo $this->words->getScore(); // output: 0.9
```

### Run tests
In root directory (in console) run the following:
```php
Expand Down
137 changes: 88 additions & 49 deletions clover.xml
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1530446422">
<project timestamp="1530446422">
<coverage generated="1530450677">
<project timestamp="1530450677">
<package name="detox\core">
<file name="/srv/detox/src/core/Phrases.php">
<class name="detox\core\Phrases" namespace="detox\core">
<metrics complexity="6" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="10" coveredstatements="10" elements="11" coveredelements="11"/>
</class>
<line num="10" type="method" name="processPhrases" visibility="public" complexity="6" crap="6" count="2"/>
<line num="13" type="stmt" count="2"/>
<line num="18" type="stmt" count="2"/>
<line num="19" type="stmt" count="2"/>
<line num="20" type="stmt" count="2"/>
<line num="21" type="stmt" count="2"/>
<line num="22" type="stmt" count="2"/>
<line num="23" type="stmt" count="2"/>
<line num="28" type="stmt" count="2"/>
<line num="10" type="method" name="processPhrases" visibility="public" complexity="6" crap="6" count="3"/>
<line num="13" type="stmt" count="3"/>
<line num="18" type="stmt" count="3"/>
<line num="19" type="stmt" count="3"/>
<line num="20" type="stmt" count="3"/>
<line num="21" type="stmt" count="3"/>
<line num="22" type="stmt" count="3"/>
<line num="23" type="stmt" count="3"/>
<line num="28" type="stmt" count="3"/>
<line num="29" type="stmt" count="2"/>
<line num="31" type="stmt" count="2"/>
<line num="31" type="stmt" count="3"/>
<metrics loc="31" ncloc="22" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="10" coveredstatements="10" elements="11" coveredelements="11"/>
</file>
<file name="/srv/detox/src/core/Words.php">
<class name="detox\core\Words" namespace="detox\core" fullPackage="detox" package="detox">
<metrics complexity="23" methods="11" coveredmethods="10" conditionals="0" coveredconditionals="0" statements="44" coveredstatements="42" elements="55" coveredelements="52"/>
<metrics complexity="25" methods="13" coveredmethods="11" conditionals="0" coveredconditionals="0" statements="47" coveredstatements="44" elements="60" coveredelements="55"/>
</class>
<line num="33" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="7"/>
<line num="35" type="stmt" count="7"/>
<line num="36" type="stmt" count="7"/>
<line num="37" type="stmt" count="7"/>
<line num="42" type="method" name="processWords" visibility="public" complexity="6" crap="6" count="3"/>
<line num="45" type="stmt" count="3"/>
<line num="50" type="stmt" count="3"/>
<line num="51" type="stmt" count="3"/>
<line num="52" type="stmt" count="3"/>
<line num="53" type="stmt" count="3"/>
<line num="54" type="stmt" count="3"/>
<line num="55" type="stmt" count="3"/>
<line num="60" type="stmt" count="3"/>
<line num="33" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="9"/>
<line num="35" type="stmt" count="9"/>
<line num="36" type="stmt" count="9"/>
<line num="37" type="stmt" count="9"/>
<line num="42" type="method" name="processWords" visibility="public" complexity="6" crap="6" count="4"/>
<line num="45" type="stmt" count="4"/>
<line num="50" type="stmt" count="4"/>
<line num="51" type="stmt" count="4"/>
<line num="52" type="stmt" count="4"/>
<line num="53" type="stmt" count="4"/>
<line num="54" type="stmt" count="4"/>
<line num="55" type="stmt" count="4"/>
<line num="60" type="stmt" count="4"/>
<line num="61" type="stmt" count="2"/>
<line num="63" type="stmt" count="3"/>
<line num="63" type="stmt" count="4"/>
<line num="68" type="method" name="processPatterns" visibility="public" complexity="7" crap="7.10" count="2"/>
<line num="70" type="stmt" count="2"/>
<line num="71" type="stmt" count="2"/>
Expand All @@ -58,13 +58,13 @@
<line num="96" type="method" name="setData" visibility="public" complexity="1" crap="1" count="1"/>
<line num="98" type="stmt" count="1"/>
<line num="99" type="stmt" count="1"/>
<line num="104" type="method" name="getScore" visibility="public" complexity="1" crap="1" count="5"/>
<line num="106" type="stmt" count="5"/>
<line num="104" type="method" name="getScore" visibility="public" complexity="1" crap="1" count="7"/>
<line num="106" type="stmt" count="7"/>
<line num="112" type="method" name="setScore" visibility="public" complexity="1" crap="1" count="3"/>
<line num="114" type="stmt" count="3"/>
<line num="115" type="stmt" count="3"/>
<line num="121" type="method" name="addLowSpaces" visibility="protected" complexity="1" crap="1" count="7"/>
<line num="123" type="stmt" count="7"/>
<line num="121" type="method" name="addLowSpaces" visibility="protected" complexity="1" crap="1" count="9"/>
<line num="123" type="stmt" count="9"/>
<line num="129" type="method" name="replace" visibility="protected" complexity="1" crap="1" count="3"/>
<line num="132" type="stmt" count="3"/>
<line num="133" type="stmt" count="3"/>
Expand All @@ -73,42 +73,81 @@
<line num="139" type="stmt" count="1"/>
<line num="140" type="stmt" count="1"/>
<line num="142" type="stmt" count="1"/>
<line num="148" type="method" name="setText" visibility="public" complexity="1" crap="1" count="7"/>
<line num="150" type="stmt" count="7"/>
<line num="151" type="stmt" count="7"/>
<line num="148" type="method" name="setText" visibility="public" complexity="1" crap="1" count="9"/>
<line num="150" type="stmt" count="9"/>
<line num="151" type="stmt" count="9"/>
<line num="156" type="method" name="getText" visibility="public" complexity="1" crap="1" count="3"/>
<line num="158" type="stmt" count="3"/>
<metrics loc="159" ncloc="109" classes="1" methods="11" coveredmethods="10" conditionals="0" coveredconditionals="0" statements="44" coveredstatements="42" elements="55" coveredelements="52"/>
<line num="164" type="method" name="getDataSet" visibility="public" complexity="1" crap="2" count="0"/>
<line num="166" type="stmt" count="0"/>
<line num="172" type="method" name="setDataSet" visibility="public" complexity="1" crap="1" count="2"/>
<line num="174" type="stmt" count="2"/>
<line num="175" type="stmt" count="2"/>
<metrics loc="175" ncloc="119" classes="1" methods="13" coveredmethods="11" conditionals="0" coveredconditionals="0" statements="47" coveredstatements="44" elements="60" coveredelements="55"/>
</file>
</package>
<package name="detox\dataset">
<file name="/srv/detox/src/dataset/CustomSet.php">
<class name="detox\dataset\CustomSet" namespace="detox\dataset">
<metrics complexity="9" methods="5" coveredmethods="4" conditionals="0" coveredconditionals="0" statements="15" coveredstatements="14" elements="20" coveredelements="18"/>
</class>
<line num="16" type="method" name="getWords" visibility="public" complexity="1" crap="1" count="1"/>
<line num="18" type="stmt" count="1"/>
<line num="25" type="method" name="setWords" visibility="public" complexity="1" crap="1" count="1"/>
<line num="27" type="stmt" count="1"/>
<line num="28" type="stmt" count="1"/>
<line num="29" type="stmt" count="1"/>
<line num="34" type="method" name="getPhrases" visibility="public" complexity="1" crap="1" count="1"/>
<line num="36" type="stmt" count="1"/>
<line num="43" type="method" name="setPhrases" visibility="public" complexity="1" crap="1" count="1"/>
<line num="45" type="stmt" count="1"/>
<line num="46" type="stmt" count="1"/>
<line num="47" type="stmt" count="1"/>
<line num="53" type="method" name="checkDict" visibility="private" complexity="5" crap="5.07" count="2"/>
<line num="55" type="stmt" count="2"/>
<line num="56" type="stmt" count="2"/>
<line num="57" type="stmt" count="0"/>
<line num="59" type="stmt" count="2"/>
<line num="60" type="stmt" count="2"/>
<line num="61" type="stmt" count="2"/>
<line num="65" type="stmt" count="2"/>
<metrics loc="65" ncloc="47" classes="1" methods="5" coveredmethods="4" conditionals="0" coveredconditionals="0" statements="15" coveredstatements="14" elements="20" coveredelements="18"/>
</file>
<file name="/srv/detox/src/dataset/EnglishSet.php">
<class name="detox\dataset\EnglishSet" namespace="detox\dataset">
<metrics complexity="2" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="2" coveredstatements="2" elements="4" coveredelements="4"/>
</class>
<line num="61" type="method" name="getWords" visibility="public" complexity="1" crap="1" count="3"/>
<line num="63" type="stmt" count="3"/>
<line num="66" type="method" name="getPhrases" visibility="public" complexity="1" crap="1" count="2"/>
<line num="68" type="stmt" count="2"/>
<metrics loc="69" ncloc="62" classes="1" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="2" coveredstatements="2" elements="4" coveredelements="4"/>
<line num="69" type="method" name="getPhrases" visibility="public" complexity="1" crap="1" count="2"/>
<line num="71" type="stmt" count="2"/>
<metrics loc="72" ncloc="62" classes="1" methods="2" coveredmethods="2" conditionals="0" coveredconditionals="0" statements="2" coveredstatements="2" elements="4" coveredelements="4"/>
</file>
</package>
<file name="/srv/detox/src/dataset/SetContract.php">
<metrics loc="11" ncloc="11" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</file>
<package name="detox\exceptions">
<file name="/srv/detox/src/exceptions/BadDictException.php">
<class name="detox\exceptions\BadDictException" namespace="detox\exceptions">
<metrics complexity="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</class>
<metrics loc="7" ncloc="7" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</file>
</package>
<package name="detox\source">
<file name="/srv/detox/src/source/Text.php">
<class name="detox\source\Text" namespace="detox\source">
<metrics complexity="11" methods="11" coveredmethods="11" conditionals="0" coveredconditionals="0" statements="17" coveredstatements="17" elements="28" coveredelements="28"/>
</class>
<line num="13" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="9"/>
<line num="15" type="stmt" count="9"/>
<line num="16" type="stmt" count="9"/>
<line num="18" type="method" name="setString" visibility="public" complexity="1" crap="1" count="8"/>
<line num="20" type="stmt" count="8"/>
<line num="21" type="stmt" count="8"/>
<line num="23" type="method" name="getString" visibility="public" complexity="1" crap="1" count="8"/>
<line num="25" type="stmt" count="8"/>
<line num="13" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="11"/>
<line num="15" type="stmt" count="11"/>
<line num="16" type="stmt" count="11"/>
<line num="18" type="method" name="setString" visibility="public" complexity="1" crap="1" count="10"/>
<line num="20" type="stmt" count="10"/>
<line num="21" type="stmt" count="10"/>
<line num="23" type="method" name="getString" visibility="public" complexity="1" crap="1" count="10"/>
<line num="25" type="stmt" count="10"/>
<line num="31" type="method" name="setReplaceChars" visibility="public" complexity="1" crap="1" count="3"/>
<line num="33" type="stmt" count="3"/>
<line num="34" type="stmt" count="3"/>
Expand All @@ -124,14 +163,14 @@
<line num="66" type="stmt" count="3"/>
<line num="71" type="method" name="getPrefix" visibility="public" complexity="1" crap="1" count="4"/>
<line num="73" type="stmt" count="4"/>
<line num="79" type="method" name="isReplaceable" visibility="public" complexity="1" crap="1" count="7"/>
<line num="81" type="stmt" count="7"/>
<line num="79" type="method" name="isReplaceable" visibility="public" complexity="1" crap="1" count="9"/>
<line num="81" type="stmt" count="9"/>
<line num="87" type="method" name="setReplaceable" visibility="public" complexity="1" crap="1" count="3"/>
<line num="89" type="stmt" count="3"/>
<line num="90" type="stmt" count="3"/>
<metrics loc="90" ncloc="66" classes="1" methods="11" coveredmethods="11" conditionals="0" coveredconditionals="0" statements="17" coveredstatements="17" elements="28" coveredelements="28"/>
</file>
</package>
<metrics files="5" loc="360" ncloc="270" classes="4" methods="25" coveredmethods="24" conditionals="0" coveredconditionals="0" statements="73" coveredstatements="71" elements="98" coveredelements="95"/>
<metrics files="7" loc="451" ncloc="334" classes="5" methods="32" coveredmethods="29" conditionals="0" coveredconditionals="0" statements="91" coveredstatements="87" elements="123" coveredelements="116"/>
</project>
</coverage>
16 changes: 16 additions & 0 deletions src/core/Words.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,20 @@ public function getText() : Text
{
return $this->text;
}

/**
* @return SetContract
*/
public function getDataSet() : SetContract
{
return $this->dataSet;
}

/**
* @param SetContract $dataSet
*/
public function setDataSet(SetContract $dataSet) : void
{
$this->dataSet = $dataSet;
}
}
66 changes: 66 additions & 0 deletions src/dataset/CustomSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace detox\dataset;


use detox\exceptions\BadDictException;

class CustomSet implements SetContract
{
private $words = [];
private $phrases = [];

/**
* @return array
*/
public function getWords() : array
{
return $this->words;
}

/**
* @param array $words
* @throws BadDictException
*/
public function setWords(array $words) : void
{
$this->checkDict($words);
$this->words = $words;
}

/**
* @return array
*/
public function getPhrases() : array
{
return $this->phrases;
}

/**
* @param array $phrases
* @throws BadDictException
*/
public function setPhrases(array $phrases) : void
{
$this->checkDict($phrases);
$this->phrases = $phrases;
}

/**
* @param array $dict
* @throws BadDictException
*/
private function checkDict(array $dict)
{
foreach ($dict as $score => $words) {
if (is_numeric($score) === false) {
throw new BadDictException('Keys of dict array must be string representation of float number, ex.: 0.9, 1.0 etc');
}
foreach ($words as $word) {
if (is_string($word) === false) {
throw new BadDictException('Values in dict sub-array must be of type string');
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/dataset/EnglishSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public function getWords() : array
return $this->words;
}

/**
* @return array
*/
public function getPhrases() : array
{
return $this->phrases;
Expand Down
8 changes: 8 additions & 0 deletions src/exceptions/BadDictException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace detox\exceptions;


class BadDictException extends \Exception
{
}
23 changes: 17 additions & 6 deletions tests/dataset/PhrasesTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
<?php
/**
* Created by PhpStorm.
* User: arthurkushman
* Date: 30.06.18
* Time: 13:40
*/

namespace detoxtests\dataset;


use detox\core\Phrases;
use detox\dataset\CustomSet;
use detox\dataset\EnglishSet;
use detox\source\Text;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -81,4 +76,20 @@ public function it_replaces_phrases()
$this->phrases->processPhrases();
$this->assertEquals('just pre____post dude', $this->phrases->getText()->getString());
}

/**
* @test
*/
public function it_sets_custom_dict()
{
$customSet = new CustomSet();
$customSet->setPhrases([
'0.8' => ['weird phrase']
]);
$this->text->setString('This weird phrase text should be detected');
$this->phrases->setText($this->text);
$this->phrases->setDataSet($customSet);
$this->phrases->processPhrases();
$this->assertEquals(0.8, $this->phrases->getScore());
}
}
Loading

0 comments on commit 5e0a8da

Please sign in to comment.