Completed
Push — master ( 0e6f4c...e299fb )
by Pol
08:00 queued 06:04
created

NGramsTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
cbo 0
dl 0
loc 67
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A ngramsArray() 0 4 1
A ngramsString() 0 6 2
A frequency() 0 8 1
B doNgrams() 0 16 5
1
<?php
2
3
namespace drupol\phpngrams;
4
5
trait NGramsTrait
6
{
7
    /**
8
     * @param array $data
9
     * @param int $n
10
     * @param bool $cyclic
11
     *
12
     * @return \Generator
13
     */
14 3
    public function ngramsArray(array $data, $n = 1, $cyclic = true)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
15
    {
16 3
        return $this->doNgrams($data, $n, $cyclic);
17
    }
18
19
    /**
20
     * @param string $data
21
     * @param int $n
22
     * @param bool $cyclic
23
     *
24
     * @return \Generator
25
     */
26 4
    public function ngramsString($data, $n = 1, $cyclic = true)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
27
    {
28 4
        foreach ($this->doNgrams(str_split($data), $n, $cyclic) as $data) {
29 4
            yield implode('', $data);
30
        }
31 4
    }
32
33
    /**
34
     * @param \Generator $ngrams
35
     * @param string $substring
36
     *
37
     * @return float|int
38
     */
39 1
    public function frequency(\Generator $ngrams, $substring)
40
    {
41 1
        $ngrams = iterator_to_array($ngrams);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $ngrams. This often makes code more readable.
Loading history...
42
43 1
        return count(array_filter($ngrams, function ($n) use ($substring) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
44 1
            return $n === $substring;
45 1
        }))/count($ngrams);
46
    }
47
48
    /**
49
     * @param $data
50
     * @param $n
51
     * @param $cyclic
52
     *
53
     * @return \Generator
54
     */
55 7
    private function doNgrams($data, $n = 1, $cyclic = true)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
56
    {
57 7
        $dataLength = count($data);
58
59 7
        $n = $n > $dataLength ? $dataLength : $n;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $n. This often makes code more readable.
Loading history...
60
61 7
        $length = (false === $cyclic ? $dataLength - $n + 1 : $dataLength);
62
63 7
        for ($j = 0; $j < $length; $j++) {
64 7
            $ngrams = [];
65 7
            for ($i = $j; $i < $n + $j; $i++) {
66 7
                $ngrams[] = $data[$i%$dataLength];
67
            }
68 7
            yield $ngrams;
69
        }
70 7
    }
71
}
72