php array_filter用作函数,但不作为方法在类中

时间:2016-08-26 11:55:18

标签: php array-filter

我有这个代码,我想从文本中获取最常用的单词。 我使用了几个数组函数,例如str_word_count()array_count_values(),最后是array_filter()。 以下代码如下:

<?php
/*Get the most frequent words with PHP*/

/*1. Assign the text to a variable*/
$text='NOT long ago, many parents wondered at what age they should give their child full access to the car keys. Nowadays, parents face a trickier question: At what age should a child own a smartphone?

The smartphone, after all, is the key to unfettered access to the internet and the many benefits and dangers that come with it. But unlike driving a car, which is legal in some states starting at the age of 16, there is no legal guideline for a parent to determine when a child may be ready for a smartphone.

The topic is being increasingly debated as children get smartphones at an ever younger age. On average, children are getting their first smartphones around age 10, according to the research firm Influence Central, down from age 12 in 2012. For some children, smartphone ownership starts even sooner — including second graders as young as 7, according to internet safety experts.';    
echo $text;
/*2. Set to lowercase*/
$text = strtolower($text);
/*3. Separate the text into words, into an array. Accept foreign ascii characters*/
$words = str_word_count($text,1,'áâæàåãäçéêèëíîìïñóôòøõöœúûùüÿ¿¡');
echo '<br><hr><h3>Words:</h3>';
if(is_array($words)){echo '<p>$words is indeed an array</p>';}
var_dump($words);
/*3. Get each word's occurrences'*/
$words = array_count_values($words);
echo '<br><hr><h3>Words occurrences:</h3>';
var_dump($words);
/*4. Order according to occurrences*/
echo '<br><hr>';
arsort($words);
var_dump($words);
echo '<br><hr>';
/*5. Defining the stopwords*/
//Stopwords:
$stopwords = ['the','to','it','is','a','at','for','as'];
/*6. Filter out the stopwords and those words with a frequence not more than 2 occurrences*/
$palabras = array_filter($words, function($word,$index)use($stopwords){
    if(!in_array($index,$stopwords)){
        if($word>2){
            return true;
        }
    }
},ARRAY_FILTER_USE_BOTH);

echo '<p>Now the most frequent words are (without the stop words):</p>';
var_dump($palabras);
?>

它工作正常。现在我想把这段代码传递给一个类。

以下是类和实例化的代码:

<?php
/*Class to get the most frequent words from a text*/
namespace Models\Helpers\TextMining;

class Word
{
    protected $text;
    protected $words=[];
    protected $filtered_words=[];
    protected $stopwords = [];
    protected $lang;
    protected $active;
    protected $ascii = 'áâæàåãäçéêèëíîìïñóôòøõöœúûùüÿ¿¡';

    public function __construct($text,$lang = 'es',$active = true)
    {
        $this->text = strtolower($text);
        $this->words = str_word_count($this->text,1,$this->ascii);
        $this->lang = $lang;
        $this->active = $active;
        $this->stopwords = ['the','to','it','is','a','at','for','as'];
        arsort(array_count_values($this->words));
    }

    /*Show stopwords*/
    public function getStopwords(){
        return $this->stopwords;
    }
    /*Show the words from the text*/
    public function getWords()
    {
        return $this->words;
    }

    /*Filter out the stopwords from the text and those words with an occurrence not greater than 2*/
    public function applyStopwords2text()
    {
        $stopwords = $this->getStopwords();
        $words = $this->getWords();

        $palabras = array_filter($words, function($word,$index)use($stopwords){
            if(!in_array($index,$stopwords)){
                if($word>2){
                    return true;
                }
            }
        },ARRAY_FILTER_USE_BOTH);

        $this->filtered_words = $palabras;
        return $palabras;
    }
}

/***************/
    $text='NOT long ago, many parents wondered at what age they should give their child full access to the car keys. Nowadays, parents face a trickier question: At what age should a child own a smartphone?

The smartphone, after all, is the key to unfettered access to the internet and the many benefits and dangers that come with it. But unlike driving a car, which is legal in some states starting at the age of 16, there is no legal guideline for a parent to determine when a child may be ready for a smartphone.

The topic is being increasingly debated as children get smartphones at an ever younger age. On average, children are getting their first smartphones around age 10, according to the research firm Influence Central, down from age 12 in 2012. For some children, smartphone ownership starts even sooner — including second graders as young as 7, according to internet safety experts.';

    $word = new Word($text,'es',true);
    $stopwords = $word->getStopwords();
    var_dump($stopwords);
    //var_dump($word);
    //die();
    $words = $word->applyStopwords2text();

    var_dump($words);


?>

问题在于array_filter()函数,因为我得到一个空数组。使用相同的protected $filtered_words=[];函数时,属性array_filter()中没有任何内容存储。为什么? 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

似乎你在构造函数方法中犯了一个小错误。试试这个:

public function __construct($text,$lang = 'es',$active = true)
{
    $this->text = strtolower($text);
    $this->words = str_word_count($this->text,1,$this->ascii);
    $this->lang = $lang;
    $this->active = $active;
    $this->stopwords = ['the','to','it','is','a','at','for','as'];
    $this->words = array_count_values($this->words);
    arsort($this->words);
}

如您所见,array_count_values的结果未分配给$this->$words,因此在调用过滤器功能时,您仍然拥有旧单词数组。

答案 1 :(得分:2)

您的问题出在您的构造函数中 - 您没有使代码与独立版本中的代码相同。

在最后一行:

arsort(array_count_values($this->words));

array_count_values的结果未分配给任何内容,因此arsort对临时数组进行排序,然后不保存它。

将构造函数更改为:

public function __construct($text,$lang = 'es',$active = true)
{
    $this->text = strtolower($text);
    $this->words = str_word_count($this->text,1,$this->ascii);
    $this->lang = $lang;
    $this->active = $active;
    $this->stopwords = ['the','to','it','is','a','at','for','as'];
    $this->words = array_count_values($this->words);
    arsort($this->words);
}

然后它将匹配代码的非oop版本。