Frequency::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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