• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

PHP函数从一个文本字符串中提取关键字

PHP 水墨上仙 2804次浏览

这是一个函数定位接收一个字符串作为参数(连同其他配置可选参数),并且定位该字符串中的所有关键字(出现最多的词),返回一个数组或一个字符串由逗号分隔的关键字。功能正常工作,但我正在改进,因此,有任何的建议请评论。

/**
 * 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函数从一个文本字符串中提取关键字
喜欢 (0)
加载中……