Reply
Keyword Density Analysis Functions
Old 06-28-2004, 10:24 PM Keyword Density Analysis Functions
Novice Talker

Posts: 9
I am trying to get this Keyword Density Analysis script to work and it won't. I have tried a few things, but I am really not that great at Php, so can some please help me or tell me why this won't work and how to fix it?
<?php

function kda($url)
{

if(!stristr($url, 'http://'))
{
$url = 'http://'.$url;
}

if($html = @file_get_contents($url))
{
$html = html_entity_decode(file_get_contents($url));
//preg_match('/(?<=<title>).*?(?=<\\/title>)/is', $html, $matches);
//$title = array_shift($matches);

$meta_tags = get_meta_tags($url);
$html = kda_strip_tag_script($html);
$no_html = strip_tags($html);

$tag_info = $meta_tags['description']." ".$meta_tags['keywords'];
$text .= $tag_info." ".$no_html;
$total = count(explode(' ', $text));

$text = kda_clean(kda_stopWords($text));
$words = explode(' ', $text);
$total = count($words);
for($x=0; $x<$total; $x++)
{
$words[$x] = trim($words[$x]);
if($words[$x]!='')
{
$ws[$words[$x]]++;
if(trim($words[$x+1])!='')
{
$phrase2 = $words[$x]." ".trim($words[$x+1]);
$ws[$phrase2]++;
if(trim($words[$x+2])!='')
{
$phrase3 = $words[$x]." ".trim($words[$x+1])." ".trim($words[$x+2]);
$ws[$phrase3]++;
}
}
}

}
foreach($ws as $word=>$count)
{
if( ($count>1) and (strlen($word)>2) )
{
$phrase_size = count(explode(' ', $word));
$occurances[$phrase_size] = $occurances[$phrase_size] + $count;
}
}

foreach($ws as $word=>$count)
{
if( ($count>1) and (strlen($word)>2) )
{
$phrase_size = count(explode(' ', $word));
$density = round( ($count/$occurances[$phrase_size])*100, 2);
$dens[$phrase_size][$word] = $density;
}
}

arsort($dens[1]);
if($dens[2])
{
arsort($dens[2]);
}
if($dens[3])
{
arsort($dens[3]);
}


$return = array('dens'=>$dens, 'occurances'=>$occurances);
/*
echo "<pre>";
print_r($occurances);
print_r($dens);
*/
return $return ;
}else {
return false;
}
}
function kda_strip_tag_script($html) {
$pos1 = false;
$pos2 = false;
do {
if ($pos1 !== false && $pos2 !== false) {
$first = NULL;
$second = NULL;
if ($pos1 > 0)
$first = substr($html, 0, $pos1);
if ($pos2 < strlen($html) - 1)
$second = substr($html, $pos2);
$html = $first . $second;
}
preg_match("/<script[^>]*>/i", $html, $matches);
$str1 =& $matches[0];
preg_match("/<\/script>/i", $html, $matches);
$str2 =& $matches[0];
$pos1 = strpos($html, $str1);
$pos2 = strpos($html, $str2);
if ($pos2 !== false)
$pos2 += strlen($str2);
} while ($pos1 !== false && $pos2 !== false);
return $html;
}
function kda_clean($text)
{
global $stopwords_file;
$text = str_replace('.', '', $text);
$text = str_replace(',', '', $text);
$text = str_replace('(', '', $text);
$text = str_replace(')', '', $text);
$text = str_replace('_', '', $text);
$text = str_replace('*', '', $text);
$text = str_replace('"', '', $text);
$text = str_replace('-', '', $text);
$text = str_replace("!", '', $text);
$text = str_replace("?", '', $text);
$text = str_replace("\n", '', $text);
$text = str_replace('/', '', $text);
$text = str_replace('’', "'", $text);

return trim(strtolower($text));
}
function kda_stopWords($term)
{
global $sw_count;
//load list of common words
$common = file('kdawords.txt');
$total = count($common);
for ($x=0; $x<= $total; $x++)
{
$common[$x] = trim(strtolower($common[$x]));
}

//make array of search terms
$_terms = explode(" ", $term);

foreach ($_terms as $line)
{
if (in_array(strtolower(trim($line)), $common))
{
$removeKey = array_search($line, $_terms);
$sw_count++;
unset($_terms[$removeKey]);
}
else
{
$clean_term .= " ".$line;
}
}
return $clean_term;
}

?>

You can see a like Keyword Density Analysis script at Link Exchange it
mikeysamer is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 06-29-2004, 12:03 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
mikey,

When you say the script "doesn't work," could you be more specific? Are you getting an error, bad results, causing your machine to crash and emit radiation? Also, could you briefly explain the algorithm you are trying to implement -- (i.e., what do you want the code to do?).
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 06-29-2004, 12:05 AM
Christopher's Avatar
Iced Cap

Latest Blog Post:
PHP and Unicode with UTF-8
Posts: 3,111
Location: Toronto, Ontario
And please paste your code within the [php ][/php ] tags (without spaces). This will preserve indentation and make it easier to read with syntax highlighting.
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 06-29-2004, 02:48 PM
Novice Talker

Posts: 9
Sorry, I am new to all of this. I don't get no warnings, I just get a blank page.

This code will allow other webmasters to put their (or someone elses site) URL in a little box, then when they hit submit it will show the keyword Density of that page they put in the box for SEO.

PHP Code:
<?php 

function kda($url


if(!
stristr($url'http://')) 

$url 'http://'.$url


if(
$html = @file_get_contents($url)) 

$html html_entity_decode(file_get_contents($url)); 
//preg_match('/(?<=<title>).*?(?=<\\/title>)/is', $html, $matches); 
//$title = array_shift($matches); 

$meta_tags get_meta_tags($url); 
$html kda_strip_tag_script($html); 
$no_html strip_tags($html); 

$tag_info $meta_tags['description']." ".$meta_tags['keywords']; 
$text .= $tag_info." ".$no_html
$total count(explode(' '$text)); 

$text kda_clean(kda_stopWords($text)); 
$words explode(' '$text); 
$total count($words); 
for(
$x=0$x<$total$x++) 

$words[$x] = trim($words[$x]); 
if(
$words[$x]!=''

$ws[$words[$x]]++; 
if(
trim($words[$x+1])!=''

$phrase2 $words[$x]." ".trim($words[$x+1]); 
$ws[$phrase2]++; 
if(
trim($words[$x+2])!=''

$phrase3 $words[$x]." ".trim($words[$x+1])." ".trim($words[$x+2]); 
$ws[$phrase3]++; 





foreach(
$ws as $word=>$count

if( (
$count>1) and (strlen($word)>2) ) 

$phrase_size count(explode(' '$word)); 
$occurances[$phrase_size] = $occurances[$phrase_size] + $count



foreach(
$ws as $word=>$count

if( (
$count>1) and (strlen($word)>2) ) 

$phrase_size count(explode(' '$word)); 
$density round( ($count/$occurances[$phrase_size])*1002); 
$dens[$phrase_size][$word] = $density



arsort($dens[1]); 
if(
$dens[2]) 

arsort($dens[2]); 

if(
$dens[3]) 

arsort($dens[3]); 



$return = array('dens'=>$dens'occurances'=>$occurances); 
/* 
echo "<pre>"; 
print_r($occurances); 
print_r($dens); 
*/ 
return $return 
}else { 
return 
false


function 
kda_strip_tag_script($html) { 
$pos1 false
$pos2 false
do { 
if (
$pos1 !== false && $pos2 !== false) { 
$first NULL
$second NULL
if (
$pos1 0
$first substr($html0$pos1); 
if (
$pos2 strlen($html) - 1
$second substr($html$pos2); 
$html $first $second

preg_match("/<script[^>]*>/i"$html$matches); 
$str1 =& $matches[0]; 
preg_match("/<\/script>/i"$html$matches); 
$str2 =& $matches[0]; 
$pos1 strpos($html$str1); 
$pos2 strpos($html$str2); 
if (
$pos2 !== false
$pos2 += strlen($str2); 
} while (
$pos1 !== false && $pos2 !== false); 
return 
$html

function 
kda_clean($text

global 
$stopwords_file
$text str_replace('.'''$text); 
$text str_replace(','''$text); 
$text str_replace('('''$text); 
$text str_replace(')'''$text); 
$text str_replace('_'''$text); 
$text str_replace('*'''$text); 
$text str_replace('"'''$text); 
$text str_replace('-'''$text); 
$text str_replace("!"''$text); 
$text str_replace("?"''$text); 
$text str_replace("\n"''$text); 
$text str_replace('/'''$text); 
$text str_replace('’'"'"$text); 

return 
trim(strtolower($text)); 

function 
kda_stopWords($term

global 
$sw_count
//load list of common words 
$common file('kdawords.txt'); 
$total count($common); 
for (
$x=0$x<= $total$x++) 

$common[$x] = trim(strtolower($common[$x])); 


//make array of search terms 
$_terms explode(" "$term); 

foreach (
$_terms as $line

if (
in_array(strtolower(trim($line)), $common)) 

$removeKey array_search($line$_terms); 
$sw_count++; 
unset(
$_terms[$removeKey]); 

else 

$clean_term .= " ".$line


return 
$clean_term


?>
mikeysamer is offline
Reply With Quote
View Public Profile
 
Old 07-04-2004, 11:01 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
mikey,

I looked through the code and didn't find any instance where something was printed to the screen. I didn't evaluate the functionality of the code there, but are you sure it is not working?
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 07-04-2004, 11:21 AM
celticbrue's Avatar
Extreme Talker

Posts: 175
Location: Wiltshire, England
Ditto Kyrnt's response,

The only code that I can see would print anything to the webpage has been commented out: -

PHP Code:
/* 
echo "<pre>"; 
print_r($occurances); 
print_r($dens); 
*/ 
What happens if you un comment this?

Ian.
celticbrue is offline
Reply With Quote
View Public Profile
 
Old 07-05-2004, 07:46 PM
Novice Talker

Posts: 9
It doesn't do anything when I on comment it and it has not worked since I got, but try to mess with it more.

Thanks foe the help guys!
mikeysamer is offline
Reply With Quote
View Public Profile
 
Old 07-05-2004, 10:14 PM
cmonkey's Avatar
Ultra Talker

Posts: 268
Do you really need your own keyword density script? I mean there are hundreds of sites out there that already offer free keyword density analyzers to visitors. Maybe it would be easier to just use one of those for now. As you said, you're "really not that great at Php", I commend you for attempting such a project, but perhaps this isn't the easiest thing for a newcomer to PHP to undertake.
cmonkey is offline
Reply With Quote
View Public Profile
 
Old 07-06-2004, 08:49 AM
Republikin's Avatar
Super Moderator

Posts: 3,191
Quote:
Originally Posted by cmonkey
Do you really need your own keyword density script? I mean there are hundreds of sites out there that already offer free keyword density analyzers to visitors. Maybe it would be easier to just use one of those for now. As you said, you're "really not that great at Php", I commend you for attempting such a project, but perhaps this isn't the easiest thing for a newcomer to PHP to undertake.
How else do you we learn more advanced things? I commend you for trying and say keep up the good work.
Republikin is offline
Reply With Quote
View Public Profile
 
Old 07-06-2004, 09:15 AM
Kennyfr's Avatar
Novice Talker

Posts: 7
Location: France
Hi Mikey

What bits did you "mess with"? Did you save the original code? There is nothing to display in the sample code above.

I'm not trying to teach you how to suck eggs, but, when you're gonna mess with code and you're not sure, back up the original!

Best wishes ~ Kenny
__________________
Some of the best vacation properties in France.
www.francedirect.net
Kennyfr is offline
Reply With Quote
View Public Profile Visit Kennyfr's homepage!
 
Old 07-07-2004, 05:27 AM
Novice Talker

Posts: 9
It's the original one, I think the problem with it is that it's not calling the Functions, I tried a few things...

1) $url = $_GET["url"];.
2) print_r ($words); and tried a few another prints such as $meta_tags.
3) Taking the comments out.
4) changing the form to php and used both post and get in the form.

There are a few another things I used, but these are the biggest.

Also I won't be using this for my self it will be for my visitors, so this will add more and better content to my site. They say content is king once you get the visitors to your site, but it will also help make a better theme for my site when search engines spider my site.

Quote:
How else do you we learn more advanced things? I commend you for trying and say keep up the good work
Thanks, I should learn it fast, I hope anyway!
mikeysamer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Keyword Density Analysis Functions
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.19338 seconds with 12 queries