【问题标题】:why stop word removal be null? (php)为什么停用词删除为空? (php)
【发布时间】:2018-02-08 12:59:06
【问题描述】:

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

【问题讨论】:

    标签: php regex codeigniter nlp text-processing


    【解决方案1】:

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

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

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

    【讨论】:

    • 使用回调函数和不使用有什么区别?真的很好奇
    • 如果没有提供回调,所有等于 FALSE 的数组条目将被删除。例如,我添加了一个回调。
    • 如果没有回调,“假”值将从数组中删除(例如 false""NULL0)。使用此回调,仅删除 NULL 值。在这种情况下,这并不重要。
    【解决方案2】:

    删除空数组元素。

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

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

    $a_ncwords = array_filter($a_ncwords);
    

    【讨论】:

    • 我试过了。这个简单的代码解决了我的问题。 :) 谢谢先生。
    • 这个(和另一个答案)肯定不能回答这个问题:为什么index[0]index[1] 数组值是空的?
    • 另一个答案也解决了我的问题,但我需要基本代码先生。感谢您的回复。我已经投票给另一个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多