Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: extract phrases of n-words length

by jandrew (Hermit)
on Jun 25, 2009 at 00:38 UTC ( #774555=note: print w/ replies, xml ) Need Help??


in reply to extract phrases of n-words length

At the risk of being redundant and borrowing heavily I submit a possible variation

use strict; use warnings; use Smart::Comments '###'; my $abstract = 'Perl is a high-level, general-purpose, interpreted, + dynamic programming language.'; my $MinLength = 2;#Must be at least 1; my $MaxLength = 4;#Must be >= $MinLength my $MinPosition = $MinLength - 1;#Counting from 0 my ( $ArrayRef, $AnswerRef ) = (); @$ArrayRef = split(/\s+/, $abstract); #Iterate through @$ArrayRef for my $position (0..($#$ArrayRef-$MinPosition)) { #handle the first word with no space my $phrase = $ArrayRef->[$position]; for my $x (1..$MaxLength) {#Build the phrase #For all but the first time $phrase .= ' ' . $ArrayRef->[$position] if $x != 1; #Load the output $AnswerRef->[$x]->{$phrase} = 1 if $x >= $MinLength; $position++; #Bump up against the end last if $position > $#$ArrayRef; } } #use Smart::Comment.pm ### The unique 3 word phrases are: $AnswerRef->[3]
Update adjusted the output to demonstrate surfing the answer ref and used .=


Comment on Re: extract phrases of n-words length
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://774555]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2015-07-08 04:26 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 (94 votes), past polls