|
5 | 5 | * make it easier to access core functionality
|
6 | 6 | */
|
7 | 7 |
|
8 |
| - |
9 |
| -/** |
10 |
| - * Normalize the tokens |
11 |
| - * @param array $tokens |
12 |
| - * @param string|function $normalizer |
13 |
| - * @return array |
14 |
| - */ |
15 |
| -function normalize_tokens(array $tokens, $normalizer = 'strtolower') : array |
16 |
| -{ |
17 |
| - return array_map($normalizer, $tokens); |
| 8 | +if (! function_exists('normalize_tokens')) { |
| 9 | + /** |
| 10 | + * Normalize the tokens |
| 11 | + * |
| 12 | + * @param array $tokens |
| 13 | + * @param string|function $normalizer |
| 14 | + * |
| 15 | + * @return array |
| 16 | + */ |
| 17 | + function normalize_tokens( array $tokens, $normalizer = 'strtolower' ): array { |
| 18 | + return array_map( $normalizer, $tokens ); |
| 19 | + } |
18 | 20 | }
|
19 | 21 |
|
20 |
| -/** |
21 |
| - * Tokenize text into an array of tokens |
22 |
| - * @param string $text |
23 |
| - * @param string $tokenizerClassName |
24 |
| - * @return array |
25 |
| - */ |
26 |
| -function tokenize(string $text, string $tokenizerClassName = \TextAnalysis\Tokenizers\GeneralTokenizer::class) : array |
27 |
| -{ |
28 |
| - return (new $tokenizerClassName())->tokenize($text); |
| 22 | +if (! function_exists('tokenize')) { |
| 23 | + /** |
| 24 | + * Tokenize text into an array of tokens |
| 25 | + * |
| 26 | + * @param string $text |
| 27 | + * @param string $tokenizerClassName |
| 28 | + * |
| 29 | + * @return array |
| 30 | + */ |
| 31 | + function tokenize( string $text, string $tokenizerClassName = \TextAnalysis\Tokenizers\GeneralTokenizer::class ): array { |
| 32 | + return ( new $tokenizerClassName() )->tokenize( $text ); |
| 33 | + } |
29 | 34 | }
|
30 | 35 |
|
31 |
| -/** |
32 |
| - * Shortcut for getting a freq distribution instance |
33 |
| - * @param array $tokens |
34 |
| - * @return \TextAnalysis\Analysis\FreqDist |
35 |
| - */ |
36 |
| -function freq_dist(array $tokens) : \TextAnalysis\Analysis\FreqDist |
37 |
| -{ |
38 |
| - return new \TextAnalysis\Analysis\FreqDist($tokens); |
| 36 | +if (! function_exists('freq_dist')) { |
| 37 | + /** |
| 38 | + * Shortcut for getting a freq distribution instance |
| 39 | + * |
| 40 | + * @param array $tokens |
| 41 | + * |
| 42 | + * @return \TextAnalysis\Analysis\FreqDist |
| 43 | + */ |
| 44 | + function freq_dist( array $tokens ): \TextAnalysis\Analysis\FreqDist { |
| 45 | + return new \TextAnalysis\Analysis\FreqDist( $tokens ); |
| 46 | + } |
39 | 47 | }
|
40 | 48 |
|
41 |
| -/** |
42 |
| - * |
43 |
| - * @param array $tokens |
44 |
| - * @param type $lexicalDiversityClassName |
45 |
| - * @return float |
46 |
| - */ |
47 |
| -function lexical_diversity(array $tokens, $lexicalDiversityClassName = \TextAnalysis\LexicalDiversity\Naive::class) : float |
48 |
| -{ |
49 |
| - return (new $lexicalDiversityClassName())->getDiversity($tokens); |
| 49 | +if (! function_exists('lexical_diversity')) { |
| 50 | + /** |
| 51 | + * |
| 52 | + * @param array $tokens |
| 53 | + * @param type $lexicalDiversityClassName |
| 54 | + * |
| 55 | + * @return float |
| 56 | + */ |
| 57 | + function lexical_diversity( array $tokens, $lexicalDiversityClassName = \TextAnalysis\LexicalDiversity\Naive::class ): float { |
| 58 | + return ( new $lexicalDiversityClassName() )->getDiversity( $tokens ); |
| 59 | + } |
50 | 60 | }
|
51 | 61 |
|
52 |
| -/** |
53 |
| - * |
54 |
| - * @param array $tokens |
55 |
| - * @param type $nGramSize |
56 |
| - * @param type $separator |
57 |
| - * @return array |
58 |
| - */ |
59 |
| -function ngrams(array $tokens, $nGramSize = 2, $separator = ' ') : array |
60 |
| -{ |
61 |
| - return \TextAnalysis\NGrams\NGramFactory::create($tokens, $nGramSize, $separator); |
| 62 | +if (! function_exists('ngrams')) { |
| 63 | + /** |
| 64 | + * |
| 65 | + * @param array $tokens |
| 66 | + * @param type $nGramSize |
| 67 | + * @param type $separator |
| 68 | + * |
| 69 | + * @return array |
| 70 | + */ |
| 71 | + function ngrams( array $tokens, $nGramSize = 2, $separator = ' ' ): array { |
| 72 | + return \TextAnalysis\NGrams\NGramFactory::create( $tokens, $nGramSize, $separator ); |
| 73 | + } |
62 | 74 | }
|
63 | 75 |
|
64 |
| -/** |
65 |
| - * |
66 |
| - * @param string $haystack |
67 |
| - * @param string $needle |
68 |
| - * @return bool |
69 |
| - */ |
70 |
| -function starts_with(string $haystack, string $needle) : bool |
71 |
| -{ |
72 |
| - return \TextAnalysis\Utilities\Text::startsWith($haystack, $needle); |
| 76 | +if (! function_exists('starts_with')) { |
| 77 | + /** |
| 78 | + * |
| 79 | + * @param string $haystack |
| 80 | + * @param string $needle |
| 81 | + * |
| 82 | + * @return bool |
| 83 | + */ |
| 84 | + function starts_with( string $haystack, string $needle ): bool { |
| 85 | + return \TextAnalysis\Utilities\Text::startsWith( $haystack, $needle ); |
| 86 | + } |
73 | 87 | }
|
74 | 88 |
|
75 |
| -/** |
76 |
| - * @param string $haystack |
77 |
| - * @param string $needle |
78 |
| - * @return bool |
79 |
| - */ |
80 |
| -function ends_with(string $haystack, string $needle) : bool |
81 |
| -{ |
82 |
| - return \TextAnalysis\Utilities\Text::endsWith($haystack, $needle); |
| 89 | +if (! function_exists('ends_with')) { |
| 90 | + /** |
| 91 | + * @param string $haystack |
| 92 | + * @param string $needle |
| 93 | + * |
| 94 | + * @return bool |
| 95 | + */ |
| 96 | + function ends_with( string $haystack, string $needle ): bool { |
| 97 | + return \TextAnalysis\Utilities\Text::endsWith( $haystack, $needle ); |
| 98 | + } |
83 | 99 | }
|
84 | 100 |
|
85 |
| -/** |
86 |
| - * Returns an instance of the TextCorpus |
87 |
| - * @param string $text |
88 |
| - * @return \TextAnalysis\Corpus\TextCorpus |
89 |
| - */ |
90 |
| -function text(string $text) : \TextAnalysis\Corpus\TextCorpus |
91 |
| -{ |
92 |
| - return new \TextAnalysis\Corpus\TextCorpus($text); |
| 101 | +if (! function_exists('text')) { |
| 102 | + /** |
| 103 | + * Returns an instance of the TextCorpus |
| 104 | + * |
| 105 | + * @param string $text |
| 106 | + * |
| 107 | + * @return \TextAnalysis\Corpus\TextCorpus |
| 108 | + */ |
| 109 | + function text( string $text ): \TextAnalysis\Corpus\TextCorpus { |
| 110 | + return new \TextAnalysis\Corpus\TextCorpus( $text ); |
| 111 | + } |
93 | 112 | }
|
94 | 113 |
|
0 commit comments