gpt4 book ai didi

php - 从文本中提取关键字和多词关键字 - PHP

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:42 25 4
gpt4 key购买 nike

我想知道是否有人知道从 PHP 文本 block 中提取最常出现的关键字/短语的最佳方法。

我想为我正在处理的应用程序构建自己的标签云。主要的棘手部分是提取“多词”关键字,例如“白宫”,而不是将它们识别为两个单独的词,而是一个短语。

一定有很多脚本可以用于此目的,只是似乎找不到!

感谢您的帮助!

最佳答案

这是我使用的一个小块 - 它解析一个逗号分隔的字符串,并相应地打印大小:

PHP

function cs_get_tag_cloud_data($data)
{
$data = str_replace(' ', '', $data);
$tagwords_arr = explode(",", $data);
$tags_arr = null;

for( $x=0; $x<sizeof($tagwords_arr); $x++)
{
$word_count = get_tag_count($tagwords_arr, $tagwords_arr[$x]);

if(in_tag_array($tags_arr, $tagwords_arr[$x]) == false)
{
$tags_arr[] = array("tag" => $tagwords_arr[$x], "count" => $word_count);
}
}

return $tags_arr;
}

# Get tag count
function get_tag_count($arr, $word)
{
$wordCount = 0;
for ( $i = 0; $i < sizeof($arr); $i++ )
{
if ( strtoupper($arr[$i]) == strtoupper($word) ) $wordCount++;
}
return $wordCount;
}

# check if word already exists
function in_tag_array($arr, $search)
{
$tag_exists = false;
if(sizeof($arr)>0)
{
for($b = 0; $b < sizeof($arr); $b++)
{
if (strtoupper($arr[$b]['tag']) == strtoupper($search))
{
$tag_exists = true;
break;
}
}
}
else
{
$tag_exists = false;
}
return $tag_exists;
}

HTML

<p id="tag-words">
<? $tag_data = cs_get_tag_cloud_data($cloud_data);
asort($tag_data);

for($x=0; $x<sizeof($tag_data); $x++)
{
$word = "";
$value = "";
$count = 0;
$font_size = 0;
$new_font_size = 0;

foreach($tag_data[$x] as $key => $value)
{
if($key == "tag") $word = $value;
if($key == "count") $count = $value;
if($count > 10) $count = 10;

if($count > 0)
{
$new_font_size = 0;
$font_size = 8;
$new_font_size = $font_size + ($count*3);

$word = preg_replace("/&#?[a-z0-9]+;/i","", $word);

echo '<a class="tag-link" style="font-size: ' . $new_font_size . 'px;" href="#">' . $word . '</a> ';
}
}
} ?>
</p>

这只是我用过的东西,但我想我会分享 - 也许它对你有帮助。

编辑:对于双词标签,您可以只做类似“White-House”的操作,然后在回显时删除破折号。只是另一个想法。

关于php - 从文本中提取关键字和多词关键字 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5050991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com