Test Failed
Branch master (42e418)
by Pol
01:47
created

NGramsCyclicSpec::it_can_get_ngram_from_a_string()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace spec\drupol\phpngrams;
4
5
use drupol\phpngrams\NGramsCyclic;
6
use PhpSpec\ObjectBehavior;
7
8
class NGramsCyclicSpec extends ObjectBehavior
9
{
10
    public function it_is_initializable()
11
    {
12
        $this->shouldHaveType(NGramsCyclic::class);
13
    }
14
15
    public function it_can_get_ngram_from_a_string()
16
    {
17
        $result = [
18
            'he',
19
            'el',
20
            'll',
21
            'lo',
22
            'oh',
23
        ];
24
25
        $this->ngrams('hello', 2)->shouldIterateAs(new \ArrayIterator($result));
26
    }
27
28
    public function it_can_get_ngram_from_a_string_with_big_n()
29
    {
30
        $result = [
31
            'hello',
32
            'elloh',
33
            'llohe',
34
            'lohel',
35
            'ohell',
36
        ];
37
38
        $this->ngrams('hello', 10)->shouldIterateAs(new \ArrayIterator($result));
39
    }
40
41
    public function it_can_get_ngram_from_an_array()
42
    {
43
        $result = [
44
            ['h', 'e'],
45
            ['e', 'l'],
46
            ['l', 'l'],
47
            ['l', 'o'],
48
            ['o', 'h'],
49
        ];
50
51
        $this->ngrams(['h', 'e', 'l', 'l', 'o'], 2)->shouldIterateAs(new \ArrayIterator($result));
52
    }
53
54
    public function it_can_get_ngram_from_an_array_with_cycling()
55
    {
56
        $result = [
57
            ['h', 'e', 'l'],
58
            ['e', 'l', 'l'],
59
            ['l', 'l', 'o'],
60
            ['l', 'o', ' '],
61
            ['o', ' ', 'w'],
62
            [' ', 'w', 'o'],
63
            ['w', 'o', 'r'],
64
            ['o', 'r', 'l'],
65
            ['r', 'l', 'd'],
66
            ['l', 'd', 'h'],
67
            ['d', 'h', 'e'],
68
        ];
69
70
        $this->ngrams(['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'], 3)->shouldIterateAs(new \ArrayIterator($result));
71
    }
72
73
    public function it_can_get_ngram_from_an_array_with_big_n()
74
    {
75
        $result = [
76
            ['h', 'e', 'l', 'l', 'o'],
77
            ['e', 'l', 'l', 'o', 'h'],
78
            ['l', 'l', 'o', 'h', 'e'],
79
            ['l', 'o', 'h', 'e', 'l'],
80
            ['o', 'h', 'e', 'l', 'l'],
81
        ];
82
83
        $this->ngrams(['h', 'e', 'l', 'l', 'o'], 10)->shouldIterateAs(new \ArrayIterator($result));
84
    }
85
86
    public function it_can_calculate_the_frequency()
87
    {
88
        $input= 'Hold my beer';
89
        $ngrams = $this->getWrappedObject()->ngrams($input, 2);
90
        $this->frequency($ngrams, 'my')->shouldBe(1/12);
91
92
        $input= ['h', 'e', 'l', 'l', 'o'];
93
        $ngrams = $this->getWrappedObject()->ngrams($input, 2);
94
        $this->frequency($ngrams, ['l', 'l'])->shouldBe(1/5);
95
    }
96
}
97