in reply to Re^2: Is there a module to break a string into proper words?
in thread Is there a module to break a string into proper words?

What's a proper word? What's a word? Working with natural language is always a lot of fun.

Below is a very naive approach to the problem. It uses Grady Ward's Moby Word list. It does not provide alternate parsing of a string. It also just silently skips over a character or string of characters that is not a 'word'.

In the code...

Enjoy.

>perl -wMstrict -le "my @words; ;; my $fname = '../../../../moby/mwords/354984si.ngl'; open my $fh, '<', $fname or die qq{opening '$fname': $!}; while (<$fh>) { chomp; next if m{ [^[:alnum:]-] }xms; push @words, $_; } close $fh; ;; @words = reverse sort @words; my $ok_one_letter = qr{\A [ai] \z}xmsi; my $ok_two_letter = qr{\A (?: be | my | is | at | do) \z}xmsi; my $ignore = qr{\A ism | ismy \z}xmsi; my $ok_other = qr{\A (?! $ignore) .{3,} \z}xmsi; @words = grep { $_ =~ $ok_one_letter || $_ =~ $ok_two_letter || $_ =~ $ok_other } @words ; my $words = join '|', @words; $words = qr{ $words }xms; ;; print '---------------'; for my $string ('www.thisismydomain.com', @ARGV) { my @chunks = $string =~ m{ $words }xmsog; printf qq{'$string' ->}; printf qq{ '$_'} for @chunks; printf qq{\n}; } " www.knowthyself.net kXnowthyXself.net --------------- 'www.thisismydomain.com' -> 'this' 'is' 'my' 'domain' 'com' 'www.knowthyself.net' -> 'know' 'thyself' 'net' 'kXnowthyXself.net' -> 'nowt' 'self' 'net'

Note: 'nowt' is short for 'nothing' or maybe 'naught'. Update: Actually, dict.org says 'nowt' means "Neat cattle", whatever that is (or those are).

Replies are listed 'Best First'.
Re^4: Is there a module to break a string into proper words?
by Anonymous Monk on Dec 30, 2010 at 05:35 UTC
    This looks very promising, is it possible to expand it so that it retu +rns an array instead? e.g. thisismydomain -> this is my domain this is my do main his is my domain his is my do main
      is it possible ...

      absolutely :)