Passed
Push — develop ( 169afe...f2bd80 )
by Jens
02:39
created

Tokenizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A tokenize() 0 7 2
A addTokenToVector() 0 10 3
A getTokenVector() 0 4 1
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 21-2-2017
5
 * Time: 16:23
6
 */
7
8
namespace library\search;
9
10
/**
11
 * Class Tokenizer
12
 * @package library\search
13
 */
14
class Tokenizer
15
{
16
	protected $inputString;
17
	protected $tokenVector = array();
18
19
	/**
20
	 * Tokenizer constructor.
21
	 *
22
	 * @param string $string Should preferably be parsed wit \library\search\CharacterFilter
23
	 * @see \library\search\CharacterFilter
24
	 */
25
	public function __construct($string)
26
	{
27
		$this->inputString = $string;
28
		$this->tokenize();
29
	}
30
31
	protected function tokenize()
32
	{
33
		$tokens = explode(' ', $this->inputString);
34
		foreach ($tokens as $token) {
35
			$this->addTokenToVector($token);
36
		}
37
	}
38
39
	protected function addTokenToVector($token)
40
	{
41
		if (!empty($token)) {
42
			if (isset($this->tokenVector[$token])) {
43
				$this->tokenVector[$token] += 1;
44
			} else {
45
				$this->tokenVector[$token] = 1;
46
			}
47
		}
48
	}
49
50
	/**
51
	 * @return array
52
	 */
53
	public function getTokenVector()
54
	{
55
		return $this->tokenVector;
56
	}
57
58
59
}