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


In reply to Re^3: Is there a module to break a string into proper words? by AnomalousMonk
in thread Is there a module to break a string into proper words? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.