计算单词的出现次数,并在同一短语中仅计数1

时间:2016-01-18 18:45:38

标签: php

我有一个问题,我需要计算给定数组中单词出现的次数。

如果单词在相同的短语中,我希望代码将单词('instagrammm')的计数增加1。

这是我的代码:

$output = array();
$buzzes = ['instagramm some text instagramm', 'instagramm some text' , 'instagramm some text' , 'instagramm some text'];
foreach ($buzzes as $buzz) {
    $flag = 0;
    $words = explode(" ",$buzz);
    foreach ($words as $word)
    {
        $word = mb_convert_case($word, MB_CASE_LOWER, "UTF-8");
        $word = preg_replace('/^[^A-Za-z0-9\-]|[^A-Za-z0-9\-]$/', '', $word);

        if(strlen($word) > 2){
            if (array_key_exists($word, $output)){
                    $output[$word]++;
            }else{
                $output[$word] = 1;
            }    
        } 
    }
}

以下是预期结果:

Array
(
    [instagramm] => 4
    [some] => 4
    [text] => 4
)

3 个答案:

答案 0 :(得分:1)

试试这个。使用较少的比较和分配会更快:

foreach ($buzzes as $buzz) {
    $words = array_unique(explode(' ',$buzz));
    foreach ($words as $word) {
        $word = mb_convert_case($word, MB_CASE_LOWER, "UTF-8");
        $word = preg_replace('/^[^A-Za-z0-9\-]|[^A-Za-z0-9\-]$/', '', $word);
        if(strlen($word) > 2){
            if (array_key_exists($word, $output)){
                $output[$word]++;
            }else{
                $output[$word] = 1;
            }
        }
    }
}

看看:

// My version
Took: 0.178099 
Array
(
    [instagramm] => 4
    [some] => 4
    [text] => 4
)

// accepted version
Took: 25.308847 
Array
(
    [instagramm] => 4
    [some] => 4
    [text] => 4
)

答案 1 :(得分:0)

似乎你认为你的代码中还有另一个条件。 只是用条件修改了你的代码。

 $output = array();
        $buzzes = array('instagramm some text instagramm', 'instagramm some text' , 'instagramm some text' , 'instagramm some text');
        foreach ($buzzes as $buzz) {
            $flag = 0;
            $words = explode(" ",$buzz);
            $wordArr    = array();
            foreach ($words as $word)
            {
                $word = mb_convert_case($word, MB_CASE_LOWER, "UTF-8");
                $word = preg_replace('/^[^A-Za-z0-9\-]|[^A-Za-z0-9\-]$/', '', $word);
                if(!in_array(strtolower($word),$wordArr)) {
                    if(strlen($word) > 2){
                        if (array_key_exists($word, $output)){
                                $output[$word]++;
                        }else{
                            $output[$word] = 1;
                        }    
                    }
                    $wordArr[]  = strtolower($word);
                }
            }
        }
print_r($output);   

答案 2 :(得分:0)

以下是代码的简化版本:

<form onsubmit="fakesubmit(e)">
  <p>
  Enter the object and your name, click on "send" the object will be on the email object, and your name will appears on the body of the email.
  </p>
   <input class="name_input" type="text" name="contact-object" id="contact-object" placeholder="The object"/>
  <input class="name_input" type="text" name="contact-name" id="contact-name" placeholder="Your name"/>

  <input type="submit" class="btn" value="send"/>
</form>

输出:

$output = array();
$buzzes = ['instagramm some text instagramm', 'instagramm some text' , 'instagramm some text' , 'instagramm some text'];

foreach( explode( ' ', implode( ' ', $buzzes ) ) as $word )
{
    $word = strtolower( trim( $word ) );

    if( !empty( $word ) )
    {
        if( isset( $output[ $word ] ) )
        {
            $output[ $word ]++;
        }
        else{ $output[ $word ] = 1; }
    }
}