0

I'm the beginner NLP programmer in PHP. I just want to discuss about the stop word removal.

this is my practice:

I have the following declaration of a variable $words = "he's the young man";

and then I remove the common words like this

 $common_words = $this->common_words();
 $ncwords = preg_replace('/\b('.implode('|',$common_words).')\b/','',$data); 
 // I have save the array common_words in another function

and I explode my no common words

$a_ncwords=explode(" ", $ncwords);

But, when I print $a_ncwords, like so print_r($a_ncwords);

I get a result like this:

Array ( [0] => [1] => [2] => young [3] => man )

why are the index[0] and index[1] array values null?

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
Gilang Pratama
  • 439
  • 6
  • 18

2 Answers2

3

Because you are replacing the words with an empty string. The array elements still exist, they are just empty now.

You should remove them from the array if they are empty. You can do this like so:

array_filter($ncwords, function($item) { return !is_null($item); });
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • What's the difference between using callback function vs not? Genuinely curious – GrumpyCrouton Aug 30 '17 at 17:57
  • 1
    If no callback is supplied, all entries of array equal to FALSE will be removed. I added a callback for example sake. – Stuart Aug 30 '17 at 17:58
  • 1
    Without the callback, "falsey" values would be removed from the array (e.g. `false`, `""`, `NULL`, `0`). With this callback, only `NULL` values are removed. In this case, it doesn't matter much. – salathe Aug 30 '17 at 18:00
2

Remove the empty array elements.

To appease those who said it didn't answer your question:

Your preg_replace is replacing words with null and when you explode because your regex is off, those null values are getting created in your array $a_ncwords when you explode.

$a_ncwords = array_filter($a_ncwords);
clearshot66
  • 2,292
  • 1
  • 8
  • 17