I do not understand how $s =~ /^$re2$/; can match from the beginning to the end for my $s = 'couldsomeonerecommendaworkingperlmoduletosplitconcatenatedwords'; and ## $re2 = (recommend|someone|working|module|words|split|comme|could|...
If that's what $re2 looks like, then you are not running the code I posted!
It should look like:
(concatenated|concatenate|catenated|recommend|catenate|commend|someone +|working|catena|module|could|enate|split|words|perl|cate|king|mend|so +me|word|work|ate|cat|con|daw|end|eon|ere|kin|let|lit|men|mod|one|per| +rec|som|ted|ten|at|aw|ed|en|er|et|in|it|ki|li|me|mm|mo|na|ne|od|om|on +|or|os|pe|re|so|to|wo|a)? ... repeat 11 (or more) times.
The way the code:
#! perl -slw use strict; my @w = do{ local @ARGV = 'words.txt'; <> }; chomp @w; my $s = 'couldsomeonerecommendaworkingperlmoduletosplitconcatenatedwor +ds'; my @subset = grep{ $s =~ /$_/ } 'a', 'perl', @w; my $re1 = join '|', sort{ length( $b ) <=> length( $a ) }@subset; my $re2 = "($re1)?" x 11; print for grep defined(), $s =~ /^$re2$/;
Works is:
This is the subset of all words in the dictionary that appear somewhere in the input string.
So $re1 looks like this: longestword|longword|shorter|short
(longestword|longword|shorter|short)?(longestword|longword|shorter|sho +rt)?(longestword|longword|shorter|short)?...
The effect is that (provided every word in your input is spelt correctly, and appears in the dictionary), is that it will match each longest word in turn.
Because the repeated regex is conditional ($re1)?, if the number of repeated elements is longer than the number of words in the string, it will just stop matching when it reaches the end of the string, and the redundant captures will return the null string.
Hence the grep defined().
The results will rarely be perfect, but it depending upon the source of your strings, it might form the basis for further, perhaps statistical, analysis.
I'm curious as hell about the source of the data and the purpose of the exercise?
In reply to Re^3: Splitting compound (concatenated) words )
by BrowserUk
in thread Splitting compound (concatenated) words )
by vit
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |