Number of times that word apears in text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Number of times that word apears in text

Post by ddragas »

How to count number of times that single word is repeated in block of text

I need to make function, but dont know where to start from.


$x=some_number
$text=block_of_text_to_search_words_in_it


function givetop($x, $text)
{

}

If somebody could point me in right direction I would aprecheate it

Regards

ddragas
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

thank you for quick reply

These two functions are for counting No of times word apears.

How can I list these words in desc order of counted number words apears?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. Store the results for each word into an array
  2. Sort the array after all words are processed
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Thank you for reply, but I'm having hard time with thise function (especially with preg_match_all() - that I've never used before)

Please help needed.

How to start?

What should I do first?


Is there array function that tims top $x words from rest of the text?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not sure what I can say to help. Posting your code may be useful.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

How to sort array to get top X words desc?

Here is some code until now

Code: Select all

$some_text = "You’ve worked really hard, your product is at last finished, so why on earth would you give it away? Well because it works – here’s why.

The internet grew up on the premise of giving stuff away. No one has ever expected to pay to view information on an internet web site. This has resulted in most internet businesses having to devise complicated business models to make a living. Here’s the most popular one. “Give and you shall receive.”

Surfers on the web love getting something for nothing. They won’t buy immediately from you, so don’t disappoint them and give them what they want. Still don’t see the point?";

$No = 5;


function gettop_x_words($x, $text)
	{
		$words = explode(" ", $text);

		foreach ($words as $key => $value) 
		{
			if(strlen($value) > 3)
				{
					$count_words = substr_count ($text, $value);
					$array_counted_words[] = array(array('word' => $value, 'count'=> $count_words));
				}
		}
		$get_words = print_r($array_counted_words);		
		return $get_words;
	}

echo gettop_x_words($No, $some_text);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's something different that what I was talking about and what I thought you were asking about.

Roughly, I would strip out all non-alphanumerics (after the explosion) and then run array_count_values() then sort it.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

How to sort array in way I've put it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hmm.. that's a tough one..



rsort() maybe?
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

I can not see result.

Why?

Code: Select all

function gettop_x_words($x, $text)
	{
		$words = explode(" ", $text);

		foreach ($words as $key => $value) 
		{
			if(strlen($value) > 3)
				{
					$count_words = substr_count ($text, $value);
					$array_counted_words[] = array(array('word' => $value, 'count'=> $count_words));
				}
		}

	 rsort($array_counted_words); 


     foreach ($array_counted_words as $word)
		{
         	echo "$word[0] $word[1]<br>\n";
	    }
   
	}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't have any code I talked about from my previous to last post.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

yes it is true

I do not have streinght any more

son't know how to finish it.

Can you give me a couple lines of code ?

PLEASE
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not interested in writing this code for you.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

I'm guessing here because your description is not too good.

Code: Select all

function get_keywords($word_string, $num_words_to_return = 10, $noise_words = array(), $min_word_length = 3)
{
	preg_match_all('/\w{'.$min_word_length.',}/', strtolower($word_string), $matches);
	if(empty($matches[0])) return array();
	foreach($matches[0] as $word) in_array($word, $noise_words) or @$occurrences[$word]++;
	arsort($occurrences);
	$keys = array_keys($occurrences);
	$values = array_values($occurrences);
	for($i = 0, $j = count($keys); $i < $j and $i < $num_words_to_return; $i++)
	{
		$return[] = array('word' => $keys[$i], 'count' => $values[$i]);
	}
	return $return;
}
Post Reply