Frequency   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 19
dl 0
loc 65
ccs 19
cts 19
cp 1
rs 10
c 1
b 1
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 7 1
A getFrequency() 0 13 5
A getPercent() 0 10 4
1
<?php
2
/**
3
 * AnalyzerText package.
4
 *
5
 * @author  Peter Gribanov <[email protected]>
6
 */
7
8
namespace AnalyzerText\Analyzer;
9
10
/**
11
 * Анализатор частоты появления строк в тексте.
12
 *
13
 * @author  Peter Gribanov <[email protected]>
14
 */
15
class Frequency extends Analyzer
16
{
17
    /**
18
     * Список слов с частотой их появления.
19
     *
20
     * @var array
21
     */
22
    protected $frequencies = array();
23
24
    /**
25
     * Список слов с частотой их появления в процентах.
26
     *
27
     * @var array
28
     */
29
    protected $percent = array();
30
31
    /**
32
     * Очищает анализатор
33
     *
34
     * @return Frequency
35
     */
36 19
    public function clear()
37
    {
38 19
        $this->frequencies = array();
39 19
        $this->percent = array();
40 19
        parent::clear();
41
42 19
        return $this;
43
    }
44
45
    /**
46
     * Определяет частоту появления слов.
47
     *
48
     * @return array
49
     */
50 4
    public function getFrequency()
51
    {
52 4
        if (empty($this->frequencies) && $this->getText()->count()) {
53 4
            foreach ($this->getText() as $word) {
54 4
                if (!isset($this->frequencies[$word->getPlain()])) {
55 4
                    $this->frequencies[$word->getPlain()] = 0;
56
                }
57 4
                ++$this->frequencies[$word->getPlain()];
58
            }
59 4
            arsort($this->frequencies);
60
        }
61
62 4
        return $this->frequencies;
63
    }
64
65
    /**
66
     * Получение проуентное отнашение частоты слов из списка частот слов.
67
     *
68
     * @return array
69
     */
70 2
    public function getPercent()
71
    {
72 2
        if (empty($this->percent) && ($frequencies = $this->getFrequency())) {
73 2
            $ratio = max($frequencies) / 100;
74 2
            foreach ($frequencies as $word => $frequency) {
75 2
                $this->percent[$word] = $frequency / $ratio;
76
            }
77
        }
78
79 2
        return $this->percent;
80
    }
81
}
82