2009-05-04 21 views
6

我正在开发一个项目,我必须根据该页面的URL找出页面的关键字密度。我GOOGLE了很多,但没有帮助和脚本被发现,我发现了一个付费工具http://www.selfseo.com/store/_catalog/php_scripts/_keyword_density_checker_php_script什么是关键字密度以及如何在PHP中创建脚本?

但我不知道实际上什么“关键字密度页”实际上是什么意思?还请告诉我如何创建一个PHP脚本来获取网页的关键字密度。

感谢

回答

23

“关键字密度”是简单地认为单词发生给定的频率(总没有其他关键字。)占单词总数的百分比。以下PHP代码将输出字符串中每个单词的密度,$str。这表明关键字密度是不是一个复杂的计算,它可以在PHP中的几行完成:

<?php 
$str = "I am working on a project where I have to find out the keyword density of the page on the basis of URL of that page. But I am not aware actually what \"keyword Density of a page\" actually means? and also please tell me how can we create a PHP script which will fetch the keyword density of a web page."; 

// str_word_count($str,1) - returns an array containing all the words found inside the string 
$words = str_word_count(strtolower($str),1); 
$numWords = count($words); 

// array_count_values() returns an array using the values of the input array as keys and their frequency in input as values. 
$word_count = (array_count_values($words)); 
arsort($word_count); 

foreach ($word_count as $key=>$val) { 
    echo "$key = $val. Density: ".number_format(($val/$numWords)*100)."%<br/>\n"; 
} 
?> 

输出示例:

of = 5. Density: 8% 
a = 4. Density: 7% 
density = 3. Density: 5% 
page = 3. Density: 5% 
... 

要提取网页的内容,你可以使用file_get_contents (或cURL)。例如,以下PHP代码列出了此网页上所有密度高于1%的关键字:

<?php 
$str = strip_tags(file_get_contents("http://stackoverflow.com/questions/819166")); 

$words  = str_word_count(strtolower($str),1); 
$word_count = array_count_values($words); 

foreach ($word_count as $key=>$val) { 
    $density = ($val/count($words))*100; 
    if ($density > 1) 
     echo "$key - COUNT: $val, DENSITY: ".number_format($density,2)."%<br/>\n"; 
} 
?> 

我希望这有助于。

+0

谢谢汤姆!这工作很好 - 使用strip_tags可以替换为别的东西(阅读http://php.net/manual/en/function.strip-tags.php的评论),但这个工程! – IEnumerator 2010-09-30 19:41:25

+0

这很好,但我如何使它匹配2个和3个单词短语? – chovy 2012-01-20 01:51:11

1

关键字密度只是意味着该关键字出现在内容与文本的其余部分的比例。一般来说,这也是一个相当无用的指标。我不打算为它创建一个脚本,因为你最好专注于其他指标。你可能会发现这个reference有用。

0

如果给定的关键字是“大象散步”,则关键字密度将是任何给定网页上出现与其他文本相关的术语“大象散步”的频率。正如VirtuosiMedia所说,这是(大体上)无用的信息。

要测量它,您必须从文本中去除所有标记,在记录关键字出现频率的同时对这些词进行计数。

在这一点上,你会知道,本文中所有单词的xx.xx%是关键字。 xx.xx%的时间,关键词是紧挨着使用的,因此我的“大象行走”的关键字密度是xx

此外,这个有用的唯一原因是演示模式匹配和在PHP中的字符串函数。

1

关键字密度大致是:

(无次关键字出现在页面上。)/

5

或者你可以试试这个: http://code.eyecatch-up.de/?p=155
更新:拆迁类http://code.google.com/p/php-class-keyword-density-check/

<?php 
include 'class/class.keywordDensity.php';    // Include class 

$obj = new KD();          // New instance 
$obj->domain = 'http://code.eyecatch-up.de';   // Define Domain 
print_r ($obj->result()); 
?> 

上面的代码返回:

Array 
(
    [0] => Array 
     (
      [total words] => 231 
     ) 

    [1] => Array 
     (
      [keyword] => display 
      [count] => 14 
      [percent] => 6.06 
     ) 
and so on... 

作品与本地和远程文件。

相关问题