Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found