2

我应该找到出现最多的句子中的单词。

这是我尝试过的,但它不起作用。

<?php
$str = "Hello friend, Hello good  good today!";
$time=array();
$cnt=str_word_count($str, 1);
$times=reset($cnt);
$count=0;
foreach($cnt as $val){
    $times=$val;    
    foreach($cnt as $val){
        if($times===$val){
            $count++;
            $times=$times . $count;     
        }       
    }
    $count=0;
}
print_r($cnt);
print_r($times);    
4

3 回答 3

4

这是我的看法。有点长,因为我包含了所有评论并解释了所有内容:

清理字符串是可选的,但强烈推荐。它将防止像“今天”与“今天”这样的故障。

<?php
$str   = "Hello friend, Hello good  good today!";
$count = array();
/*
 * Remove all common special characters (so that "today" is equal to "today,"
 * Also lowercase the entire string, so that "Hello" is equal to "hello".
 */
$str = preg_replace("/[.,!?:(){}\[\]@#$%\^&\*\-_]/", " ", $str);
$str = strtolower($str);
/*
 * Split by spaces.
 * The reason I'm using preg_split instead of explode is because there can be multiple spaces in succession
 * And we don't want excess empty array elements.
 */
$words = preg_split("/\s+/", $str, -1, PREG_SPLIT_NO_EMPTY);

/*
 * Iterate the words...
 */
foreach ($words as $word) {
    /*
     * If this is the first time we encounter the word...
     */
    if (!isset($count[$word])) {
        /*
         * Set its count to one, and skip the rest of the loop
         */
        $count[$word] = 1;
        continue;
    }
    /*
     * Increase the count of the word by one (won't be reached if first encounter
     * Which means it would only happen if we already met the word.
     */
    $count[$word]++;
}
/*
 * Reverse sort with associative keys kept.
 */
arsort($count);

/*
 * Show me the money!
 */
var_dump($count);

一个较短的版本,使用 PHP 的原生函数:

$str = "Hello friend, Hello good  good today!";

//Import words into array
$words = str_word_count($str, 1);

//Count same values
$count = array_count_values($words);

//Ascending sort
arsort($count);

var_dump($count);
于 2013-09-07T07:46:52.330 回答
0

利用:

<?php

$str = "Hello friend, Hello good  good today!";

//$str = strtolower($str); // use this line for case insensitive words

$cnt=str_word_count($str, 1);

$word_counts = array_count_values($cnt);

arsort($word_counts);

$max_val = 0; 
$max_appears = array();
foreach($word_counts as $key=>$value) {
    if($value>=$max_val) {
        $max_appears[$key] = $value;
        $max_val = $value;  
    }
}
print_r($max_appears);

您也可以将此数组打印到字符串

foreach($max_appears as $key=>$value) {
    echo $key." appears ".$value." times<br>";
}
于 2013-09-07T07:53:50.740 回答
-1
<?php
$str = "Hello friend, Hello good  good today!";
$count = array();
$words = array();
$cnt = str_word_count($str, 1);
foreach ($cnt as $val){
    $i = 0;
    $term = false;
    foreach ($words as $word){
        if ($word === $val){
            $count[$i]++;
            $term = true;
            break;
        }
        $i++;
    }
    if ($term)
        continue;
    $words[$i] = $val;
    $count[$i] = 1;
}
print_r($cnt);
print_r('<br/>');
$max = -1;
$resultWords = array();
$resultCount = array();
$i = 0;
foreach ($count as $c){
    if ($max == $c){
        $resultWords[] = $words[$i];
        $resultCount[] = $c;
    }else if ($max < $c){
        $resultWords = array();
        $resultCount = array();
        $resultWords[] = $words[$i];
        $resultCount[] = $c;
        $max = $c;
    }
    $i++;
}
foreach ($resultWords as $result){
    print_r($result.' '.$max.'<br/>');
}
?>

尝试使用我上面的代码。希望它会奏效。如果最经常出现的词只有一个词,那么将显示该词以及该词的出现次数。如果有多个单词,只要出现的次数就会显示所有单词。

于 2013-09-07T07:48:13.583 回答