Passed
Push — master ( 474905...ba9227 )
by Dev
01:17
created

ExtractExpression::getWordNumber()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace rOpenDev\ExtractExpression;
4
5
// curl: automatic set referrer
6
// pomme de terre ne marche pas
7
8
class ExtractExpression
9
{
10
    public $onlyInSentence = false;
11
    public $expressionMaxWords = 5;
12
13
    /**
14
     * @var int Minimum value for wich one we keep a trail. Set to 0 to disabled keeping trail
15
     **/
16
    public $keepTrail = 5;
17
18
    protected $words;
19
20
    /**
21
     * @var array contain the source texts
22
     **/
23
    protected $text = [];
24
    /**
25
     * @var array textKey => [expression => number]
26
     **/
27
    protected $expressions = [];
28
29
    protected $mergedExpressions;
30
31
    /**
32
     * @var array containing the number of word analyzed for a specific source text
33
     **/
34
    protected $wordNumber = [];
35
36
    public function __construct()
37
    {
38
    }
39
40
    public function addContent(string $text)
41
    {
42
        $text = new ExtractExpressions($text);
43
        $text->onlyInSentence = $this->onlyInSentence;
44
        $text->expressionMaxWords = $this->expressionMaxWords;
45
        $text->keepTrail = $this->keepTrail;
46
        $text->extract();
47
48
        $this->text[] = $text;
49
50
        return $this;
51
    }
52
53
    public function exec()
54
    {
55
        $mergedExpressions = [];
56
57
        foreach ($this->text as $text) {
58
            $expressions = $text->getExpressionsByDensity();
59
            foreach ($expressions as $expression => $density) {
60
                $mergedExpressions[$expression] = (isset($mergedExpressions[$expression]) ? $mergedExpressions[$expression] : 0) + $density;
61
            }
62
        }
63
64
        arsort($mergedExpressions);
65
66
        $this->mergedExpressions = $mergedExpressions;
67
    }
68
69
    public function getExpressions(?int $number = null)
70
    {
71
        if (null === $this->mergedExpressions) {
72
            $this->exec();
73
        }
74
75
        return !$number ? $this->mergedExpressions : array_slice($this->getExpressions(), 0, $number);
76
    }
77
78
    public function getWordNumber()
79
    {
80
        if (null === $this->mergedExpressions) {
81
            $this->exec();
82
        }
83
84
        $wn = 0;
85
        foreach ($this->text as $text) {
86
            $wn = $wn + $text->getWordNumber();
87
        }
88
89
        return $wn;
90
    }
91
92
    /**
93
     * @return array containing sentence where we can find expresion
94
     */
95
    public function getTrail(string $expression)
96
    {
97
        if (null === $this->mergedExpressions) {
98
            $this->exec();
99
        }
100
        $trail = [];
101
102
        foreach ($this->text as $text) {
103
            $trail = array_merge($trail, $tex->getTrail($expression));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tex seems to be never defined.
Loading history...
104
        }
105
106
        return $trail;
107
    }
108
109
    public function getTrails()
110
    {
111
        $trail = [];
112
113
        foreach ($this->text as $text) {
114
            $trail = array_merge($trail, $text->getTrails());
115
        }
116
117
        return $trail;
118
    }
119
}
120