1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\drupol\phpngrams; |
4
|
|
|
|
5
|
|
|
use drupol\phpngrams\NGrams; |
6
|
|
|
use PhpSpec\ObjectBehavior; |
7
|
|
|
|
8
|
|
|
class NGramsSpec extends ObjectBehavior |
9
|
|
|
{ |
10
|
|
|
public function it_is_initializable() |
11
|
|
|
{ |
12
|
|
|
$this->shouldHaveType(NGrams::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
|
|
|
]; |
23
|
|
|
|
24
|
|
|
$this->ngrams('hello', 2)->shouldIterateAs(new \ArrayIterator($result)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function it_can_get_ngram_from_a_string_with_big_n() |
28
|
|
|
{ |
29
|
|
|
$result = [ |
30
|
|
|
'hello', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
$this->ngrams('hello', 10)->shouldIterateAs(new \ArrayIterator($result)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function it_can_get_ngram_from_an_array() |
37
|
|
|
{ |
38
|
|
|
$result = [ |
39
|
|
|
['h', 'e'], |
40
|
|
|
['e', 'l'], |
41
|
|
|
['l', 'l'], |
42
|
|
|
['l', 'o'], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
$this->ngrams(['h', 'e', 'l', 'l', 'o'], 2)->shouldIterateAs(new \ArrayIterator($result)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function it_can_get_ngram_from_an_array_without_cycling() |
49
|
|
|
{ |
50
|
|
|
$result = [ |
51
|
|
|
['h', 'e', 'l'], |
52
|
|
|
['e', 'l', 'l'], |
53
|
|
|
['l', 'l', 'o'], |
54
|
|
|
['l', 'o', ' '], |
55
|
|
|
['o', ' ', 'w'], |
56
|
|
|
[' ', 'w', 'o'], |
57
|
|
|
['w', 'o', 'r'], |
58
|
|
|
['o', 'r', 'l'], |
59
|
|
|
['r', 'l', 'd'], |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
$this->ngrams(['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'], 3)->shouldIterateAs(new \ArrayIterator($result)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function it_can_get_ngram_from_an_array_with_big_n() |
66
|
|
|
{ |
67
|
|
|
$result = [ |
68
|
|
|
['h', 'e', 'l', 'l', 'o'], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
$this->ngrams(['h', 'e', 'l', 'l', 'o'], 10)->shouldIterateAs(new \ArrayIterator($result)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function it_can_calculate_the_frequency() |
75
|
|
|
{ |
76
|
|
|
$input= 'Hold my beer'; |
77
|
|
|
$ngrams = $this->getWrappedObject()->ngrams($input, 2); |
78
|
|
|
$this->frequency($ngrams, 'my')->shouldBe(1/11); |
79
|
|
|
|
80
|
|
|
$input= ['h', 'e', 'l', 'l', 'o']; |
81
|
|
|
$ngrams = $this->getWrappedObject()->ngrams($input, 2); |
82
|
|
|
$this->frequency($ngrams, ['l', 'l'])->shouldBe(1/4); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|