Skip to content

Commit eff075c

Browse files
committed
Add hash tag extract
1 parent eb0755b commit eff075c

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/Extracts/HashTag.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace TextAnalysis\Extracts;
4+
use TextAnalysis\Interfaces\IExtractStrategy;
5+
6+
/**
7+
* check if the given token is a hash tag
8+
* @author yooper
9+
*/
10+
class HashTag implements IExtractStrategy
11+
{
12+
/**
13+
*
14+
* @var int
15+
*/
16+
protected $minLength = 3;
17+
18+
public function __construct(int $minLength = 3)
19+
{
20+
$this->minLength = $minLength;
21+
}
22+
23+
/**
24+
*
25+
* @param string $token
26+
* @return false|string
27+
*/
28+
public function filter($token)
29+
{
30+
// don't count the hash tag sign -1
31+
if($token[0] === '#' && strlen($token)-1 >= $this->getMinLength()) {
32+
return $token;
33+
}
34+
return false;
35+
}
36+
37+
public function getMinLength() : int
38+
{
39+
return $this->minLength;
40+
}
41+
42+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tests\TextAnalysis\Extracts;
4+
5+
use TextAnalysis\Extracts\HashTag;
6+
7+
/**
8+
* Test if hashtag extraction is working
9+
* @author yooper
10+
*/
11+
class HashTagTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testHashTag()
14+
{
15+
$extract = new HashTag();
16+
$this->assertFalse($extract->filter("testing"));
17+
$this->assertEquals('#holiday', $extract->filter('#holiday'));
18+
$this->assertFalse($extract->filter('#DA'));
19+
}
20+
21+
public function testMinLengthHashTag()
22+
{
23+
$extract = new HashTag(2);
24+
$this->assertEquals('#DA', $extract->filter('#DA'));
25+
$this->assertFalse($extract->filter('#1'));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)