Skip to content

Commit

Permalink
Added unique keyword retrieval and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Donatello-za committed Sep 15, 2016
1 parent 50544bc commit d515d32
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
51 changes: 47 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This particular package intends to include the following benefits over the origi

## Version

1.0.2 Beta
1.0.3 Beta

## Special Thanks

Expand Down Expand Up @@ -225,6 +225,49 @@ Array

## Example 3

Creates a new instance of RakePlus and extract the unique keywords from the phrases.

```php

use DonatelloZa\RakePlus\RakePlus;

$text = "Criteria of compatibility of a system of linear Diophantine equations, " .
"strict inequations, and nonstrict inequations are considered. Upper bounds " .
"for components of a minimal set of solutions and algorithms of construction " .
"of minimal generating sets of solutions for all types of systems are given.";

$keywords = RakePlus::create($text)->keywords();
print_r($keywords);

Array
(
[0] => criteria
[1] => compatibility
[2] => system
[3] => linear
[4] => diophantine
[5] => equations
[6] => strict
[7] => inequations
[8] => nonstrict
[9] => considered
[10] => upper
[11] => bounds
[12] => components
[13] => minimal
[14] => set
[15] => solutions
[16] => algorithms
[17] => construction
[18] => generating
[19] => sets
[20] => types
[21] => systems
)
```

## Example 4

Creates a new instance of RakePlus without using the static RakePlus::create method.

```php
Expand All @@ -244,7 +287,7 @@ $phrases = (new RakePlus($text))->get();

```

## Example 4
## Example 5

You can provide custom stopwords in four different ways:

Expand All @@ -271,7 +314,7 @@ $rake = RakePlus::create($text, $stopwords);

```

## Example 5
## Example 6

You can specify the minimum number of characters that a phrase\keyword
must be and if less than the minimum it will be filtered out. The
Expand Down Expand Up @@ -306,7 +349,7 @@ Array

```

## Example 6
## Example 7

You can specify whether phrases\keywords that consists of a numeric
number only should be filtered out or not. The default is to filter out
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.2-dev"
"dev-master": "1.0.3-dev"
}
},
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions src/RakePlus.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ public function scores()
return $this->phrase_scores;
}

/**
* Returns only the unique keywords within the
* phrases instead of the full phrases itself.
*
* @return array
*/
public function keywords()
{
$keywords = [];
$phrases = $this->get();

foreach ($phrases as $phrase) {
$words = explode(' ', $phrase);
foreach ($words as $word) {
$keywords[$word] = true;
}
}

return array_keys($keywords);
}

/**
* Sorts the phrases by score, use 'asc' or 'desc' to specify a
* sort order.
Expand Down
35 changes: 35 additions & 0 deletions tests/RakePlusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,39 @@ public function testMinLengthScores()
$this->assertEquals($scores['minimal generating sets'], 8.5);
$this->assertEquals($scores['linear diophantine equations'], 9);
}

public function testKeywords()
{
$text = "Criteria of compatibility of a system of linear Diophantine equations, " .
"strict inequations, and nonstrict inequations are considered. Upper bounds " .
"for components of a minimal set of solutions and algorithms of construction " .
"of minimal generating sets of solutions for all types of systems are given.";

$keywords = RakePlus::create($text)->keywords();

$this->assertCount(22, $keywords);

$this->assertContains('criteria', $keywords);
$this->assertContains('compatibility', $keywords);
$this->assertContains('system', $keywords);
$this->assertContains('linear', $keywords);
$this->assertContains('diophantine', $keywords);
$this->assertContains('equations', $keywords);
$this->assertContains('strict', $keywords);
$this->assertContains('inequations', $keywords);
$this->assertContains('nonstrict', $keywords);
$this->assertContains('considered', $keywords);
$this->assertContains('upper', $keywords);
$this->assertContains('bounds', $keywords);
$this->assertContains('components', $keywords);
$this->assertContains('minimal', $keywords);
$this->assertContains('set', $keywords);
$this->assertContains('solutions', $keywords);
$this->assertContains('algorithms', $keywords);
$this->assertContains('construction', $keywords);
$this->assertContains('generating', $keywords);
$this->assertContains('sets', $keywords);
$this->assertContains('types', $keywords);
$this->assertContains('systems', $keywords);
}
}

0 comments on commit d515d32

Please sign in to comment.