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)
-
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.