Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 =))



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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2018-06-20 06:53 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (116 votes). Check out past polls.