Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Efficient matching with accompanying data

by LanX (Chancellor)
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)

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 taking refuge in the Monastery: (4)
As of 2016-06-28 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (364 votes). Check out past polls.