为什么停止删除单词是空的? (PHP)

时间:2017-08-30 17:44:41

标签: php regex codeigniter nlp text-processing

我是PHP的初学者NLP程序员。 我只是想讨论停用词的删除。

这是我的做法:

我有一个变量$words = "he's the young man";

的声明

然后我删除像这样的常用词

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

我爆炸了我的常用词

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

但是,当我打印$a_ncwords时,就像这样print_r($a_ncwords);

我得到这样的结果:

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

为什么index[0]index[1]数组值为空?

2 个答案:

答案 0 :(得分:3)

因为您要用空字符串替换单词。数组元素仍然存在,它们现在只是空的。

如果数组为空,则应将其从数组中删除。你可以这样做:

array_filter($ncwords, function($item) { return !is_null($item); });

答案 1 :(得分:2)

删除空数组元素。

安抚那些说没有回答你问题的人:

你的preg_replace正在用null替换单词,当你爆炸因为正则表达式已关闭时,那些空值会在你$a_ncwords时在数组explode中创建。

$a_ncwords = array_filter($a_ncwords);