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



我是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]数组值null?

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

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

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

删除空数组元素。

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

您的preg_replace正在用null替换单词,并且当您爆炸时,因为您的正则打开了,当您explode时,这些空值会在您的数组$a_ncwords中创建。

$a_ncwords = array_filter($a_ncwords);

最新更新