nirjharlo /
complete-google-seo-scan
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace NirjharLo\Cgss\Lib\Analysis\Lib; |
||||
| 4 | |||||
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
||||
| 6 | |||||
| 7 | /** |
||||
| 8 | * An object to extract most used pharases in an array of phrases, containing one or more words. |
||||
| 9 | * But, for now we are using one word elements only. |
||||
| 10 | * |
||||
| 11 | * 2 properties. |
||||
| 12 | * @property string $words Input words array |
||||
| 13 | * @property string $text Input words array |
||||
| 14 | */ |
||||
| 15 | class Keywords { |
||||
| 16 | |||||
| 17 | |||||
| 18 | public $words; |
||||
| 19 | public $text; |
||||
| 20 | |||||
| 21 | |||||
| 22 | //generate individual words from text content |
||||
| 23 | public function output() { |
||||
| 24 | |||||
| 25 | $one = $this->output_one(); |
||||
| 26 | $two = $this->fetch(2); |
||||
| 27 | $three = $this->fetch(3); |
||||
| 28 | $four = $this->fetch(4); |
||||
| 29 | $five = $this->fetch(5); |
||||
| 30 | $six = $this->fetch(6); |
||||
| 31 | $keys = array_merge( $six, $five, $four, $three, $two, $one ); |
||||
| 32 | return $keys; |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | //generate individual words from text content |
||||
| 36 | public function output_one() { |
||||
| 37 | |||||
| 38 | $one_arr = $this->fetch(1); |
||||
| 39 | $stop = $this->stop(); |
||||
| 40 | $output = array(); |
||||
| 41 | foreach ( $one_arr as $val => $key ) { |
||||
| 42 | if ( strlen( $val ) > 3 and ! in_array( $val, $stop ) ) { |
||||
| 43 | $output[$val] = $key; |
||||
| 44 | } |
||||
| 45 | } |
||||
| 46 | return array_slice( $output, 0, 10 ); |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | //generate individual words from text content |
||||
| 50 | public function fetch( $n ) { |
||||
| 51 | |||||
| 52 | //compare text and words to find keyword |
||||
| 53 | $keys = array(); |
||||
| 54 | $phrs = $this->prepare( $n ); |
||||
| 55 | $phrs_count = count($phrs); |
||||
| 56 | foreach( $phrs as $val ) { |
||||
| 57 | $key_num = substr_count( $this->text, $val ); |
||||
| 58 | $key_per = round( ( ( ( $key_num * $n ) / $phrs_count ) * 100 ), 3 ); |
||||
| 59 | if ( $key_num > 1 and $key_per > 0.2 and ! isset( $keys[$val] ) ) { |
||||
| 60 | $keys[$val] = $key_per; |
||||
| 61 | } |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | //arrange them to select top final 3 keyword |
||||
| 65 | if ( is_array( $keys ) ) { |
||||
| 66 | arsort( $keys ); |
||||
| 67 | if ( $n != 1 ) { |
||||
| 68 | |||||
| 69 | //remove keys without much significance |
||||
| 70 | $del_keys = array(); |
||||
| 71 | foreach ( $keys as $val => $key ) { |
||||
| 72 | $key_size = strlen( implode( '', explode( ' ', $val ) ) ) / 2; |
||||
| 73 | if ( $key_size <= $n ) { |
||||
| 74 | $del_keys[] = $val; |
||||
| 75 | } |
||||
| 76 | } |
||||
| 77 | $new_keys = array_diff( $keys, $del_keys ); |
||||
| 78 | |||||
| 79 | $key_top = array_slice( $new_keys, 0, 3 ); |
||||
| 80 | } else { |
||||
| 81 | $key_top = $keys; |
||||
| 82 | } |
||||
| 83 | } |
||||
| 84 | return $key_top; |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||||
| 85 | } |
||||
| 86 | |||||
| 87 | //generate individual words from text content |
||||
| 88 | public function prepare( $n ) { |
||||
| 89 | $i = 0; |
||||
|
0 ignored issues
–
show
|
|||||
| 90 | $part = array(); |
||||
| 91 | $w_input = $this->words; |
||||
| 92 | for( $i = 0; $i < $n; $i++ ) { |
||||
| 93 | |||||
| 94 | //create an array of words of pharses as sub-arrays |
||||
| 95 | $pieces = array_chunk( $w_input, $n ); |
||||
|
0 ignored issues
–
show
$w_input of type string is incompatible with the type array expected by parameter $array of array_chunk().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 96 | $last_one = array_pop( $pieces ); |
||||
| 97 | if ( is_array( $last_one ) and count( $last_one ) != $i ) { |
||||
| 98 | unset($pieces[count($pieces) - 1]); |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | //create phrase out of those sub array |
||||
| 102 | foreach( $pieces as $val ) { |
||||
| 103 | $part[] = implode( ' ', $val ); |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | //Offset input words first value to create more unique words |
||||
| 107 | $del_w_input = array_diff( $w_input, array( $w_input[0] ) ); |
||||
|
0 ignored issues
–
show
$w_input of type string is incompatible with the type array expected by parameter $array of array_diff().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 108 | $w_input = array_values($del_w_input); |
||||
| 109 | } |
||||
| 110 | return $part; |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | //array of stop words |
||||
| 114 | public function stop() { |
||||
| 115 | return array( 'from', 'that', 'this', 'what', 'when', 'where', 'have', 'they', 'just', 'your', 'most', 'their', 'some', 'then', 'there', 'them', 'make', 'ever', 'never', 'enough', 'should', 'would', 'could', 'also', 'such', 'shall', 'will', 'with' ); |
||||
| 116 | } |
||||
| 117 | } |
||||
| 118 | ?> |
||||
|
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. Loading history...
|
|||||
| 119 |