Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Efficient matching with accompanying data

by LanX (Canon)
on Jul 11, 2013 at 01:43 UTC ( #1043609=note: print w/ replies, xml ) Need Help??


in reply to Re: Efficient matching with accompanying data
in thread Efficient matching with accompanying data

proof of concept

DB<137> %lexicon=("day"=>1,"night"=>2,"knight"=>3) => ("day", 1, "night", 2, "knight", 3) DB<138> $pattern = join "|",sort {length($b)<=>length( $a) } keys %l +exicon => "knight|night|day" DB<139> $tweet= "today I will knight a guy I met last night" => "today I will knight a guy I met last night" DB<140> @matches = ( $tweet =~ /($pattern)/g ) => ("day", "knight", "night") DB<141> @lexicon{@matches} => (1, 3, 2)

if you need word boundaries try map { "\\b$_\\b" } between join and sort

DB<146> $pattern = join "|", map { "\\b$_\\b" } sort {length($b)<=>l +ength( $a) } keys %lexicon => "\\bknight\\b|\\bnight\\b|\\bday\\b" DB<147> @matches = ( $tweet =~ /($pattern)/g ) => ("knight", "night")

Cheers Rolf

( addicted to the Perl Programming Language)


Comment on Re^2: Efficient matching with accompanying data
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2015-07-07 08:37 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 (87 votes), past polls