Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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:

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|&lt;|<|g; $text =~ s|&gt;|>|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

In reply to Trying to make a string generated like google by ultranerds

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (9)
    As of 2015-07-07 11:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (88 votes), past polls