Skip to content

Commit 14b6f78

Browse files
authored
Merge pull request #27 from cicnavi/master
Declare helper function only if it does not already exist
2 parents 9b973f2 + 0307fc7 commit 14b6f78

File tree

4 files changed

+144
-118
lines changed

4 files changed

+144
-118
lines changed

src/helpers/helpers.php

Lines changed: 90 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,109 @@
55
* make it easier to access core functionality
66
*/
77

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+
}
1820
}
1921

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+
}
2934
}
3035

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+
}
3947
}
4048

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+
}
5060
}
5161

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+
}
6274
}
6375

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+
}
7387
}
7488

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+
}
8399
}
84100

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+
}
93112
}
94113

src/helpers/print.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
* Some print functions to make things easier to read
55
*/
66

7-
/**
8-
* Print out the strings in the array
9-
* @param array $strings
10-
* @param string $prefix
11-
* @param string $newline
12-
*/
13-
function print_array(array $strings, $prefix = '* ', $newline = PHP_EOL)
14-
{
15-
array_walk($strings, function($string) use ($newline, $prefix){
16-
echo $prefix.$string.$newline;
17-
});
7+
if (! function_exists('print_array')) {
8+
/**
9+
* Print out the strings in the array
10+
*
11+
* @param array $strings
12+
* @param string $prefix
13+
* @param string $newline
14+
*/
15+
function print_array( array $strings, $prefix = '* ', $newline = PHP_EOL ) {
16+
array_walk( $strings, function ( $string ) use ( $newline, $prefix ) {
17+
echo $prefix . $string . $newline;
18+
} );
19+
}
1820
}
1921

src/helpers/simplified.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44
* Short cut functions calls
55
*/
66

7-
/**
8-
*
9-
* @param array $tokens
10-
* @param string $separator
11-
* @return array
12-
*/
13-
function bigrams(array $tokens, $separator = ' ')
14-
{
15-
return \TextAnalysis\NGrams\NGramFactory::create($tokens, 2, $separator);
7+
if (! function_exists('bigrams')) {
8+
/**
9+
*
10+
* @param array $tokens
11+
* @param string $separator
12+
*
13+
* @return array
14+
*/
15+
function bigrams( array $tokens, $separator = ' ' ) {
16+
return \TextAnalysis\NGrams\NGramFactory::create( $tokens, 2, $separator );
17+
}
1618
}
1719

18-
/**
19-
*
20-
* @param array $tokens
21-
* @param string $separator
22-
* @return array
23-
*/
24-
function trigrams(array $tokens, $separator = ' ')
25-
{
26-
return \TextAnalysis\NGrams\NGramFactory::create($tokens, 3, $separator);
20+
if (! function_exists('trigrams')) {
21+
/**
22+
*
23+
* @param array $tokens
24+
* @param string $separator
25+
*
26+
* @return array
27+
*/
28+
function trigrams( array $tokens, $separator = ' ' ) {
29+
return \TextAnalysis\NGrams\NGramFactory::create( $tokens, 3, $separator );
30+
}
2731
}
2832

src/helpers/storage.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
<?php
22

3-
/**
4-
* Base function for getting the storage path to the different directories.
5-
* @return string
6-
*/
7-
function get_storage_path($subDirName = null)
8-
{
9-
$path = dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR;
10-
11-
if(!empty($path)) {
12-
$path .= $subDirName;
13-
}
14-
15-
if(!is_dir($path)) {
16-
throw new Exception("Path {$path} does not exist");
17-
}
18-
19-
return realpath($path).DIRECTORY_SEPARATOR;
20-
3+
if (! function_exists('get_storage_path')) {
4+
/**
5+
* Base function for getting the storage path to the different directories.
6+
* @return string
7+
*/
8+
function get_storage_path( $subDirName = null ) {
9+
$path = dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR;
10+
11+
if ( ! empty( $path ) ) {
12+
$path .= $subDirName;
13+
}
14+
15+
if ( ! is_dir( $path ) ) {
16+
throw new Exception( "Path {$path} does not exist" );
17+
}
18+
19+
return realpath( $path ) . DIRECTORY_SEPARATOR;
20+
21+
}
2122
}
2223

0 commit comments

Comments
 (0)