【问题标题】:Top 10 keywords PHP in a string字符串中的前 10 个 PHP 关键字
【发布时间】:2016-07-11 11:35:15
【问题描述】:

当我的目标是呈现字符串中的前 10 个单词时,我制作了一个复杂的关键字数组。

b) 我只想介绍一个重要的词,而不是像“The,That,to,a...”这样的词。

完整代码:

$str= $db_tag;
    $tok = strtok($str, ", ");
    $subStrStart = 0;

    while ($tok !== false) {
        preg_match_all("/\b" . preg_quote($tok, "/") . "\b/", substr($str, $subStrStart), $m);
        if(count($m[0]) >= 10)
            echo "'" . $tok . "' found more than 10 times, exaclty: " . count($m[0]) . "<br>";
        $subStrStart += strlen($tok);
        $tok = strtok(", ");
    }    

我的字符串:

$db_tag="The,Economy,Could,Be,Given,A,Post,Brexit,Vote,Vote,Vote,Vote,Boost,This,Week,As,Expectations,Mount,That,The,Bank,Bank,Bank,Bank,Bank,Of,England,England,England,England,England,Will,Cut,Economy,Economy,Economy,Brexit,Brexit,Brexit,Brexit";

提前致谢。

【问题讨论】:

  • 你应该分解你的字符串并使用数组函数。

标签: php arrays string keyword


【解决方案1】:

试试这个:

$db_tag = "The,Economy,Could,Be,Given,A,Post,Brexit,Vote,Vote,Vote,Vote,Boost,This,Week,As,Expectations,Mount,That,The,Bank,Bank,Bank,Bank,Bank,Of,England,England,England,England,England,Will,Cut,Economy,Economy,Economy,Brexit,Brexit,Brexit,Brexit";

$stopWords = array(
    "the", "to", "in", "a", "of", "is", "that", "will", "and", "be"
);

// Convert to array and filter out stopwords.
$words = array_filter(function ($value) {
    return !in_array($value, $stopwords);
}, explode(',', $db_tag));

$counts = array_count_values($words);
asort($counts);
$topTen = array_reverse(array_slice($counts, -10, null, true));

var_dump($topTen);

你应该看到:

php > var_dump($topTen);
array(10) {
  ["England"]=>
  int(5)
  ["Bank"]=>
  int(5)
  ["Brexit"]=>
  int(5)
  ["Economy"]=>
  int(4)
  ["Vote"]=>
  int(4)
  ["The"]=>
  int(2)
  ["Post"]=>
  int(1)
  ["Given"]=>
  int(1)
  ["A"]=>
  int(1)
  ["Could"]=>
  int(1)
}

首先,我们用explode() 将字符串拆分成一个数组。然后,我们返回一个带有 array_count_values() 的唯一数组值数组,与它们在字符串中出现的次数相关联。

接下来,我们使用 asort() 按值对数组进行就地排序。然后,我们使用array_slice() 从数组中切出最后10 个元素(最高的元素),然后使用array_reverse() 将其反转,以降序排列(可选)。

【讨论】:

  • 非常感谢您。我有两个问题。 A. 有一种方法可以将末尾的 $topTen 更改为 $db_tag 之类的字符串(仅带有标记词) B. 有没有一种方法可以过滤掉诸如“THE, IS, ARE, OF , THAT”之类的词? tnx @will
  • @alainmardo 为 B 创建一个包含您要排除的字词的黑名单
  • 关键字是什么意思?你不想要计数吗?
  • 你好。我想要计数,我想要返回字符串(用于在文章中显示关键字)。
  • array(10) { ["The"]=> int(64) ["To"]=> int(48) ["In"]=> int(20) ["A" ]=> int(19) ["Of"]=> int(19) ["Is"]=> int(16) ["That"]=> int(14) ["Will"]=> int(13 ) ["And"]=> int(13) ["Be"]=> int(12) } 和另一篇文章他给了我这个输出......有一种方法可以删除像“THE,TO,IS. ..” tnx 再次@Will
【解决方案2】:

如果“Top 10”是指字符串中的“10 个最常用词”,用逗号, 分隔,您可以这样做:

$string = "The,Economy,Could,Be,Given,A,Post,Brexit,Vote,Vote,Vote,Vote,Boost,This,Week,As,Expectations,Mount,That,The,Bank,Bank,Bank,Bank,Bank,Of,England,England,England,England,England,Will,Cut,Economy,Economy,Economy,Brexit,Brexit,Brexit,Brexit";

//Create array of words split by ","
$words = explode(",",$string);

//Create an empty array to hold data
$wordData = [];

foreach($words as $word){
    //Convert to lower case (for uniformity)
    $word = strtolower($word);

    //Add to an array if doesn't exist; if it does,
    //add to the number
    if(isset($wordData[$word])){
        $wordData[$word]++;
    } else $wordData[$word] = 1;
}

//Order $wordData array by number
arsort($wordData);

print_r($wordData);

这将输出:

数组 ( [英格兰] => 5 [银行] => 5 [英国脱欧] => 5 [投票] => 4 [经济] => 4 [该] => 2 [期望] => 1 [意志] => 1 [的] => 1 [那个] => 1 [安装] => 1 [这个] => 1 [作为] => 1 [周] => 1 [提升] => 1 [帖子] => 1 [A] => 1 [给定] => 1 [是] => 1 [可以] => 1 [切] => 1 )


过滤掉特定的词:

//Establish array of words to filter
$filterWords = ["the", "is", "are", "of", "that"];

//Remove those words from the array created earlier
foreach($filterWords as $fw){
    if(isset($wordData[$fw])) unset($wordData[$fw]);
}

print_r($wordData);

这将输出:

数组 ( [england] => 5 [bank] => 5 [brexit] => 5 [vote] => 4 [economy] => 4 [expectations] => 1 [will] => 1 [mount] => 1 [this] => 1 [as] => 1 [week] => 1 [boost] => 1 [post] => 1 [a] => 1 [given] => 1 [be] => 1 [可以] => 1 [削减] => 1 )

【讨论】:

  • 是的,非常感谢。我有两个问题。 A. 有一种方法可以将末尾的 $topTen 更改为 $db_tag 之类的字符串(仅带有标记词) B. 有没有一种方法可以过滤掉诸如“THE, IS, ARE, OF , THAT”之类的词? tnx 再次@ben
  • @alainmardo 我在答案中添加了该功能。我还添加了一行将所有字符串转换为小写,以便更轻松地管理字符串。希望这会有所帮助。
  • 完美@ben tnx。在 print_r 之后添加输出 ($worddata) 的任何方式...将数组中的单词带回新字符串 $new_string = "The,Economy,Could,Be,Given,A,Post,Brexit,Vote "; tnx 再次
  • 看看implode()$newString = implode(",",$wordData);
  • 嗨,本,内爆给我数字(计数)而不是关键字。 tnx 再次
【解决方案3】:

你可以使用explode和一个数组:

$db_tag="The,Economy,Could,Be,Given,A,Post,Brexit,Vote,Vote,Vote,Vote,Boost,This,Week,As,Expectations,Mount,That,The,Bank,Bank,Bank,Bank,Bank,Of,England,England,England,England,England,Will,Cut,Economy,Economy,Economy,Brexit,Brexit,Brexit,Brexit";
$array = array();
foreach (explode(',', $db_tag) as $val) 
{
    if(!isset($array[$val]))
    {
        $array[$val] = 1;
    }
    else
    {
        $array[$val]++;
    }
}
arsort($array);
print_r($array);

将输出:

Array
(
    [England] => 5
    [Bank] => 5
    [Brexit] => 5
    [Vote] => 4
    [Economy] => 4
    [The] => 2
    [Expectations] => 1
    [Will] => 1
    [Of] => 1
    [That] => 1
    [Mount] => 1
    [This] => 1
    [As] => 1
    [Week] => 1
    [Boost] => 1
    [Post] => 1
    [A] => 1
    [Given] => 1
    [Be] => 1
    [Could] => 1
    [Cut] => 1
)

【讨论】:

    【解决方案4】:

    使用波纹管函数从字符串中提取搜索关键字

    function getKeywords($string)
    {
        $string = "North Korea has recently introduced a sweeping new law which seeks to stamp out any kind of foreign influence - harshly punishing anyone caught with foreign films, clothing or even using slang. But why?Yoon Mi-so says she was 11 when she first saw a man executed for being caught with a South Korean drama.    His entire neighbourhood was ordered to watch. If you didn't, it would be classed as treason, she told the BBC from her home in Seoul.        The North Korean guards were making sure everyone knew the penalty for smuggling illicit videos was death. I have a strong memory of the man who was blindfolded, I can still see his tears flow down. That was traumatic for me. The blindfold was completely drenched in his tears. ";
        $vowels = ["a","e","i","o","u"];
        $ignore = ["th","thy","sh"];
        $string = str_replace($vowels, "", $string);
    
    //Create array of words split by ","
    $words = explode(" ",$string);
    
    //Create an empty array to hold data
    $wordData = [];
    
    foreach($words as $word){
        //Convert to lower case (for uniformity)
        $word = trim(strtolower($word));
        if(strlen($word)<3)
            continue;
        if(array_search($word, $ignore)>-1) continue;
        //Add to an array if doesn't exist; if it does,
        //add to the number
        if(isset($wordData[$word])){
            $wordData[$word]++;
        } else $wordData[$word] = 1;
    }
    
    //Order $wordData array by number
    arsort($wordData);
    
    $x = (array_keys($wordData));
    $result = "";
    $count = 0;
    
    foreach ($wordData as $key => $value) {
        $count++;
        $result .=$key . ",";
        if($count==10) break;
    }
    
    return $result;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2011-11-03
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多