Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Get Vowels from sentence

by graff (Chancellor)
on Jul 22, 2010 at 05:31 UTC ( [id://850769]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Get Vowels from sentence
in thread Get Vowels from sentence

The thread wasn't really about handling "a" vs. "an" correctly, but that's okay. As for your code snippet... apart from forgetting the "$" on the fourth mention of "something", there are some problems...

Since your three "exceptional case" conditions are not anchored to be string-initial, they'll misfire on words like "aunt", "aura", "man-hour", "dishonest", etc. Also, after you fix the anchoring, don't forget that adjectives can occur between the indefinite article and the noun, and many adjectives can begin with "un" as a negative prefix -- e.g. if your code snippet is preceded by:

$something = "untimely death";
you will get Bad English ("a" instead of "an"). In fact, it's pretty hard to handle word-initial "u" -- consider "unanswered" vs. "unanimous", "uninformed" vs. "uniform", etc. (And folks may disagree about cases like "Ugandan", "Ugaritic" and even "Uruguayan".) And then there's all those words starting with "eur"... Unless you take the time to plug in a pronouncing dictionary, you'll just have to put up with some mistakes.

Still, here's a stab that tries to handle most cases in a reasonable way -- note how it sets the initial "u" problem apart as a separate condition, allowing it to be more complicated all by itself. (This code can be run with target words as command-line args, but the subroutine is self-contained and easy to modularize.)

#!/usr/bin/perl use strict; sub indef_article { local $_ = shift; my $article; if ( /^(e)?u(\w+)/i ) { my $e = $1; local $_ = $2; $article = ( $e or /^(?:nanim|ni(?!n)|[gr][aeu])/i ) ? "a" : " +an"; # assumes y-glide pronunciations for Uruguay, Uganda, etc. } else { $article = ( /^(?:[aeio]|ho(?:ur|nest))/i ) ? "an" : "a"; } return "$article $_"; } print indef_article( $_ ).$/ for ( @ARGV );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://850769]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-25 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found