ultranerds has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I'm trying to make a string show up like:
Some test bla bla .... and now we carry on, with more keywords...
Basically, the string would look something like:
Some test bla bla. We wanna add some more content in here, just as an example - and now we carry on, with more keywords etc
Here is my current code:
What this does, is generate a hashref, like so:
Now, this is exactly what I want.
After this, I need to then grab the "phrases" which has the highest value for "count" .. in this case, it would be 1 and 0.
Can anyone suggest to me how I could accomplish this?
I know I could enter the values into a mySQL table, and then do an ORDER BY to get the 2 highest values - but this is going to be run on about 20 strings per page, so I would imagine that would be a little slow (especially seeing as we are doing about 2 million+ page views a week, and about 200,000 searchs =))
TIA!
Andy
I'm trying to make a string show up like:
Some test bla bla .... and now we carry on, with more keywords...
Basically, the string would look something like:
Some test bla bla. We wanna add some more content in here, just as an example - and now we carry on, with more keywords etc
Here is my current code:
my $keywords = q|fait une petite St-Laurent|; my @keywords = split / /, $keywords; $text =~ s|<!--start_tag-->\s*.*?<!--end_tag-->||g; #remove all th +e citations $text =~ s|<|<|g; $text =~ s|>|>|g; $text =~ s|<\s*.*?>||g; #remove any html $text =~ s|\[\s*.*?\]||g; #remove any bbcode tags my $strings; use Data::Dumper; my @split = split / /, $text; my $number = 0; my $hash_count = 0; foreach (@split) { $back .= "$_ "; if (length($back) >= 250) { # print qq|Adding to: \$string->{$number} \n|; $strings->{$number}->{value} = $back; $strings->{$number}->{count} = get_word_count_match($back) +; $back = undef; $number++; } } print Dumper($strings); sub get_word_count_match { my $text = $_[0]; my $total_words_found; foreach (@keywords) { my $count =0; $count++ while $text =~ /\Q$_/g; $total_words_found = $total_words_found + $count; } return $total_words_found || 0 ; }
What this does, is generate a hashref, like so:
user@serverdev admin $ perl test.cgi $VAR1 = { '1' => { 'count' => 3, 'value' => 'large. La température de l\'eau durant +l\'été ne dépasse pas 3 ou 4°c et lorsque le vent se met de la partie + vaut mieux être bien habillé... Cet été, j\'ai fait une petite excursion en kayak de mer dans la régio +n des Bergeronnes (à l\'Est de Tadoussac). ' }, '0' => { 'count' => 2, 'value' => ' Salut Memphre, Je ne connais pas les dates de fin des excursions pour aller voir les +baleines dans le St-Laurent. Mais je ne crois pas qu\'une ballade en +bateau au large en octobre soit très agréable. Déjà, durant l\'été, i +l peut faire très froid au ' }, '2' => { 'count' => 0, 'value' => 'Nous avons vu plusieurs baleines: rorqu +als communs, marsouins et bélugas. C\'est vraiment impressionnant de +voir les baleines de si près! Entre l\'excursion en bateau ou celle e +n kayak, je te conseille fortement celle en kayak. L\'expérience est +beaucoup ' } };
Now, this is exactly what I want.
After this, I need to then grab the "phrases" which has the highest value for "count" .. in this case, it would be 1 and 0.
Can anyone suggest to me how I could accomplish this?
I know I could enter the values into a mySQL table, and then do an ORDER BY to get the 2 highest values - but this is going to be run on about 20 strings per page, so I would imagine that would be a little slow (especially seeing as we are doing about 2 million+ page views a week, and about 200,000 searchs =))
TIA!
Andy
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Trying to make a string generated like google
by davorg (Chancellor) on Jul 20, 2009 at 10:39 UTC | |
by ultranerds (Hermit) on Jul 20, 2009 at 10:43 UTC | |
by ultranerds (Hermit) on Aug 10, 2009 at 14:01 UTC | |
by davorg (Chancellor) on Jun 30, 2010 at 07:50 UTC | |
Re: Trying to make a string generated like google
by moritz (Cardinal) on Jul 20, 2009 at 10:50 UTC | |
by ultranerds (Hermit) on Jul 20, 2009 at 11:13 UTC |
Back to
Seekers of Perl Wisdom