Page 1 of 2
Number of times that word apears in text
Posted: Fri Sep 22, 2006 11:41 am
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
Posted: Fri Sep 22, 2006 12:19 pm
by feyd
Posted: Fri Sep 22, 2006 1:01 pm
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?
Posted: Fri Sep 22, 2006 1:03 pm
by feyd
- Store the results for each word into an array
- Sort the array after all words are processed
Posted: Fri Sep 22, 2006 1:52 pm
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?
Posted: Fri Sep 22, 2006 2:05 pm
by feyd
I'm not sure what I can say to help. Posting your code may be useful.
Posted: Fri Sep 22, 2006 3:47 pm
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);
Posted: Fri Sep 22, 2006 4:12 pm
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.
Posted: Fri Sep 22, 2006 4:41 pm
by ddragas
How to sort array in way I've put it?
Posted: Fri Sep 22, 2006 4:52 pm
by feyd
hmm.. that's a tough one..
rsort() maybe?
Posted: Fri Sep 22, 2006 5:10 pm
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";
}
}
Posted: Fri Sep 22, 2006 5:35 pm
by feyd
You don't have any code I talked about from my previous to last post.
Posted: Fri Sep 22, 2006 5:40 pm
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
Posted: Fri Sep 22, 2006 5:44 pm
by feyd
I'm not interested in writing this code for you.
Posted: Fri Sep 22, 2006 6:15 pm
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;
}