当前位置: 首页 > 编程笔记 >

PHP函数实现从一个文本字符串中提取关键字的方法

潘慈
2023-03-14
本文向大家介绍PHP函数实现从一个文本字符串中提取关键字的方法,包括了PHP函数实现从一个文本字符串中提取关键字的方法的使用技巧和注意事项,需要的朋友参考一下

本文实例讲述了PHP函数实现从一个文本字符串中提取关键字的方法。分享给大家供大家参考。具体分析如下:

这是一个函数定位接收一个字符串作为参数(连同其他配置可选参数),并且定位该字符串中的所有关键字(出现最多的词),返回一个数组或一个字符串由逗号分隔的关键字。功能正常工作,但我正在改进,因此,感兴趣的朋友可以提出改进意见。

/**
 * Finds all of the keywords (words that appear most) on param $str 
 * and return them in order of most occurrences to less occurrences.
 * @param string $str The string to search for the keywords.
 * @param int $minWordLen[optional] The minimun length (number of chars) of a word to be considered a keyword.
 * @param int $minWordOccurrences[optional] The minimun number of times a word has to appear 
 * on param $str to be considered a keyword.
 * @param boolean $asArray[optional] Specifies if the function returns a string with the 
 * keywords separated by a comma ($asArray = false) or a keywords array ($asArray = true).
 * @return mixed A string with keywords separated with commas if param $asArray is true, 
 * an array with the keywords otherwise.
 */
function extract_keywords($str, $minWordLen = 3, $minWordOccurrences = 2, $asArray = false)
{
  function keyword_count_sort($first, $sec)
  {
    return $sec[1] - $first[1];
  }
  $str = preg_replace('/[^\\w0-9 ]/', ' ', $str);
  $str = trim(preg_replace('/\s+/', ' ', $str));
  $words = explode(' ', $str);
  $keywords = array();
  while(($c_word = array_shift($words)) !== null)
  {
    if(strlen($c_word) <= $minWordLen) continue;
    $c_word = strtolower($c_word);
    if(array_key_exists($c_word, $keywords)) $keywords[$c_word][1]++;
    else $keywords[$c_word] = array($c_word, 1);
  }
  usort($keywords, 'keyword_count_sort');
  $final_keywords = array();
  foreach($keywords as $keyword_det)
  {
    if($keyword_det[1] < $minWordOccurrences) break;
    array_push($final_keywords, $keyword_det[0]);
  }
  return $asArray ? $final_keywords : implode(', ', $final_keywords);
}
//How to use
//Basic lorem ipsum text to extract the keywords
$text = "
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Curabitur eget ipsum ut lorem laoreet porta a non libero. 
Vivamus in tortor metus. Suspendisse potenti. Curabitur 
metus nisi, adipiscing eget placerat suscipit, suscipit 
vitae felis. Integer eu odio enim, sed dignissim lorem. 
In fringilla molestie justo, vitae varius risus lacinia ac. 
Nulla porttitor justo a lectus iaculis ut vestibulum magna 
egestas. Ut sed purus et nibh cursus fringilla at id purus.
";
//Echoes: lorem, suscipit, metus, fringilla, purus, justo, eget, vitae, ipsum, curabitur, adipiscing
echo extract_keywords($text);

希望本文所述对大家的php程序设计有所帮助。

 类似资料:
  • 问题内容: 我必须从关键字后的SQL字段中的文本中提取内容。例如,如果我在表中有一个名为description的字段,并且该字段的表内容为: asdasf关键字狗 aeee关键字cat ffffaa关键字狼 我想提取并保存“关键字”之后的文本(在本例中为“狗,猫和狼”),然后将其保存在视图中,或者简单地通过选择显示它。谢谢你。 问题答案: 这是一个使用示例: 另一个例子: 结果: 请注意,此字符串

  • 问题内容: 我想从包含数字和字母的字符串中提取数字: 我想在这里获取号码或任何其他号码。 问题答案:

  • 问题内容: 我想在API中提供自动字符串格式,例如: 可以替换为格式化字符串中标注的属性值。 如何从Python格式字符串中提取关键字参数: 问题答案: 您可以使用类的一个字符串,解析出的领域,与方法: 演示: 您可以进一步解析这些字段名称。为此,您可以使用方法(Python 2)/函数(Python 3)(此内部实现细节未公开;在内部使用)。此函数返回名称的 第一部分 ,将在传递给的参数中查找该

  • 问题内容: 如果我有一个字符串,例如“此附件的长度为22 in。x 15 in。”,则我想抓取“ 22”(字符串中的第一个int值)。另外,我不知道int值的大小(字符数),因此我需要找到整个第一个int值,直到字符串中的下一个特殊字符/空格为止。 知道我该怎么做吗? 问题答案: 假设字符串中确实有一个数字,则可以使用:

  • 问题内容: 我在JavaScript中有一个字符串,例如“#box2”,我只想从中获得“ 2”。 尝试过: 它仍然在警报中返回#box2,我该如何使其正常工作? 它需要容纳末尾附加的任何长度的数字。 问题答案: 对于此特定示例, 在一般情况下: 由于这个答案由于某种原因而受到欢迎,因此有一个好处:正则表达式生成器。

  • 问题内容: 是否可以在PHP数组中使用数字字符串作为键,而无需将其转换为整数? 版画 我想要 问题答案: 没有; 不,这不对: 从手册: 键可以是整数或字符串。如果键是整数的标准表示,它将被解释为整数(即​​“ 8”将被解释为8,而“ 08”将被解释为“ 08”)。 附录 由于下面的评论,我认为指出该行为 与 JavaScript对象键 相似 但不 相同 很有趣。