2009-05-04 50 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... 

作品與本地和遠程文件。

相關問題