What is Keyword Density and how to create a script in PHP?

送分小仙女□ 提交于 2019-12-17 18:21:33

问题


I am working on a project where I have to find out the keyword density of thepage on the basis of URL of that page. I googled a lot but no help and scripts were found, I found a paid tool http://www.selfseo.com/store/_catalog/php_scripts/_keyword_density_checker_php_script

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.

Thanks


回答1:


"Keyword density" is simply the frequency that the word occurs given as a percentage of the total number of words. The following PHP code will output the density of each word in a string, $str. It demonstrates that keyword density is not a complex calculation, it can be done in a few lines of 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";
}
?>

Example output:

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

To fetch the content of a webpage you can use file_get_contents (or cURL). As an example, the following PHP code lists all keywords above 1% density on this webpage:

<?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";
}
?>

I hope this helps.




回答2:


Or you can try this: http://code.eyecatch-up.de/?p=155
Update: Relocated the class to 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()); 
?>

above code returns:

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

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

works with local and remote files.




回答3:


keyword density is roughly:

(no. of times keyword appeared on the page)/(total no. of other keywords)




回答4:


Keyword density just means the percentage that the keywords appear in the content versus rest of the text. In general, it's also a fairly useless metric for SEO. I wouldn't bother building a script for it as you'd be better off concentrating on other metrics. You might find this reference useful.




回答5:


If the given keyword is "elephant walks", the keyword density would be how often the term "elephant walks" appears on any given web page in relation to other text. As VirtuosiMedia said, this is (broadly) useless information.

To measure it, you must strip all mark up from the text, count the words while keeping track of how often the keyword(s) appear.

At that point, you will know, xx.xx % of all words in this text are keywords. xx.xx % of the time , the key word(s) are used next to each other, therefore my keyword density for "elephant walks" is xx

Again, the only reason this is useful is to demonstrate pattern matching and string functions in php.



来源:https://stackoverflow.com/questions/819166/what-is-keyword-density-and-how-to-create-a-script-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!