duanmeng2842 2016-08-26 11:55
浏览 124
已采纳

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

I have this code where I want to get the most frequent words from a text. I use several array functions such as str_word_count(), array_count_values(), and finally array_filter(). Here is the code as follows:

<?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);
?>

It's working fine. Now I want to pass this code into a class.

Here is the code of the class and instantiation:

<?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);


?>

The problem is with the array_filter() function, because I get an empty array. Nothing is stored inside the property protected $filtered_words=[]; when using the same array_filter() function. Why? How do I fix this?

  • 写回答

2条回答 默认 最新

  • drzrzzkh462254 2016-08-26 12:11
    关注

    Your problem is in your constructor - you haven't made the code do the same as in the stand-alone version.

    In the last line:

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

    The result of array_count_values isn't assigned to anything, so arsort sorts the temporary array, and then doesn't save it.

    Change your constructor to:

    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);
    }
    

    Then it will match the non-oop version of the code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败