Test Setup Failed
Pull Request — master (#350)
by Pol
02:31
created

NGramTokenizer::tokenize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\Tokenization;
6
7
use drupol\phpngrams\NGrams;
8
9
/**
10
 * Class NGramTokenizer
11
 */
12
class NGramTokenizer extends WordTokenizer
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function tokenize(string $text): array
18
    {
19
        $words = parent::tokenize($text);
20
21
        $ngramsFactory = new NGrams();
22
23
        $ngram_dataset = [];
24
        foreach ($words as $word) {
25
            $length = strlen($word);
26
27
            for ($i = 1; $i <= $length; $i++) {
28
                foreach ($ngramsFactory->ngrams(str_split($word), $i) as $ngram) {
29
                    $ngram_dataset[] = implode('', $ngram);
30
                }
31
            }
32
        }
33
34
        return $ngram_dataset;
35
    }
36
}
37