MultiAnalyzer::getWordNumber()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace PiedWeb\TextAnalyzer;
4
5
class MultiAnalyzer
6
{
7
    protected $onlyInSentence;
8
    protected $expressionMaxWords;
9
    protected $keepTrail;
10
11
    protected $words;
12
13
    /**
14
     * @var array contain the source texts
15
     */
16
    protected $text = [];
17
18 3
    public function __construct(
19
        bool $onlySentence = false,
20
        int $expressionMaxWords = 5,
21
        int $keepTrail = 3
22
    ) {
23 3
        $this->onlyInSentence = $onlySentence;
24 3
        $this->expressionMaxWords = $expressionMaxWords;
25 3
        $this->keepTrail = $keepTrail;
26 3
    }
27
28
    /**
29
     * @return Analysis
30
     */
31 3
    public function addContent(string $text)
32
    {
33 3
        $text = Analyzer::get(
34 3
            $text,
35 3
            $this->onlyInSentence,
36 3
            $this->expressionMaxWords,
37 3
            $this->keepTrail
38
        );
39
40 3
        $this->text[] = $text;
41
42 3
        return $text;
43
    }
44
45 3
    public function exec()
46
    {
47 3
        $mergedExpressions = [];
48
49 3
        foreach ($this->text as $text) {
50 3
            $expressions = $text->getExpressionsByDensity();
51 3
            foreach ($expressions as $expression => $density) {
52 3
                $mergedExpressions[$expression] =
53 3
                    (isset($mergedExpressions[$expression]) ? $mergedExpressions[$expression] : 0)
54 3
                    + $density
55
                ;
56
            }
57
        }
58
59 3
        arsort($mergedExpressions);
60
61 3
        return new Analysis($mergedExpressions, $this->getWordNumber(), $this->getTrails());
62
    }
63
64 3
    protected function getWordNumber()
65
    {
66 3
        $wn = 0;
67 3
        foreach ($this->text as $text) {
68 3
            $wn = $wn + $text->getWordNumber();
69
        }
70
71 3
        return $wn;
72
    }
73
74 3
    protected function getTrails()
75
    {
76 3
        $trails = [];
77
78 3
        foreach ($this->text as $text) {
79 3
            $trails = array_merge($trails, $text->getTrails());
80
        }
81
82 3
        return $trails;
83
    }
84
}
85