为什么停止词删除为空? (php)

why stop word removal be null? (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] 数组值为空?

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

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

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

删除空数组元素。

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

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

$a_ncwords = array_filter($a_ncwords);