Hello you PHP Community! I am looking to make an automatic meta keyword generator based on an array of phrases. It is more my site that is under construction, PHPMine. Go to the "Q and A" section and you'll see a series of questions there, five in total. right now the system simply grabs all the words there, filters though common words, and then spits out the words as keywords. I would like some advice on how to set it up in this way:

  • puts all the words in an array and counts their frequency
  • creates an array of two words phrases and counts their frequency
  • does the same for 3 word phrases
  • spits out the top 25 keywords from the array generated

maybe someone has already cooked up this nasty script. thanks in advance guys!

Recommended Answers

All 4 Replies

This is how I would generate those arrays

//$keywordsarray is the array that contains said keywords

$twowordphrases = array();
//generate two word phrases
for($i = 0; $i < count($keywordsarray); $i += 2)
{
	if(isset($keywordsarray[$i + 1]))
	{
		$twowordphrases[] = $keywordsarray[$i] . " " . $keywordsarray[$i + 1];
	}
}

$threewordphrases = array();
//generate three word phrases
for($i = 0; $i < count($keywordsarray); $i += 3)
{
	if(isset($keywordsarray[$i + 2]))
	{
		$threewordphrases[] = $keywordsarray[$i] . " " . $keywordsarray[$i + 1] . " " . $keywordsarray[$i + 2];
	}
}

ah i see and now i cycle through the array and see how many times that phrase comes up right? AWESOME! I think i was trying to do this all through one pass but I'll have to create 4 loops, one that create the phrases ( the snippet you created) and then I'll have to go through each and count the number of times the phrase is found. right?

Right, you just cycle through the two and three word arrays just like you do the one word array.

This is how I would generate those arrays

//$keywordsarray is the array that contains said keywords

$twowordphrases = array();
//generate two word phrases
for($i = 0; $i < count($keywordsarray); $i += 2)
{
	if(isset($keywordsarray[$i + 1]))
	{
		$twowordphrases[] = $keywordsarray[$i] . " " . $keywordsarray[$i + 1];
	}
}

$threewordphrases = array();
//generate three word phrases
for($i = 0; $i < count($keywordsarray); $i += 3)
{
	if(isset($keywordsarray[$i + 2]))
	{
		$threewordphrases[] = $keywordsarray[$i] . " " . $keywordsarray[$i + 1] . " " . $keywordsarray[$i + 2];
	}
}

I wouldn't jump 2, and 3 words though. Just have one loop, and then take 1,2,3 words from the current word and put them into lists. That way you get each possible phrase. The current misses those that don't start with multiples of 2 and 3.

Probably good to also discard any keyword phrase that includes common words, or words less then or equal to 2 maybe 3 chars.

2 and 3 word phrases won't work so well unless you have a large body of text to compare to so you'd probably want to take your phrases to your database and do the comparison in mysql on an index for your whole site. I know its intensive, but it is worth it if you cache well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.