Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 ### 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?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2015-10-10 13:15 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (257 votes), past polls