は、「キーワード密度は、」単に言葉が与えられて発生する頻度である(総無い他のキーワードの。)単語の総数に対するパーセンテージとして表します。次の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コードは、このWebページ上の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";
}
?>
私はこのことができます願っています。
出典
2010-03-22 13:58:00
Tom
ありがとうTom!これはうまくいきました - strip_tagsを使って別のものに置き換えることができました(http://php.net/manual/en/function.strip-tags.phpのコメントを読んでください)。 – IEnumerator
これはすばらしいですが、2〜3語のフレーズにマッチさせるにはどうすればいいですか? – chovy