Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Perl pattern matching question

by graff (Chancellor)
on May 07, 2015 at 03:08 UTC ( [id://1125943]=note: print w/replies, xml ) Need Help??


in reply to Perl pattern matching question

As with a surprising number of similar issues relating to human language, there's already a module on CPAN for this: Lingua::EN::Inflect -- behold:
#!/usr/bin/env perl use strict; use warnings; use Lingua::EN::Inflect 'A'; while (<DATA>) { while ( /\b (an? \s (\w+))/gx ) { my ( $phrase, $word ) = ( $1, $2 ); if ( $phrase eq A( $word )) { print "RIGHT: $phrase\n"; } else { print "WRONG: $phrase\n"; } } } __DATA__ I am a howling maniac in an hospital. It takes a hour to win an honorable mention. Calling him a umpire is a euphemism. This is a united front against a evil empire.
That just shows how to use the module to check for errors. It should be easy to tweak the example a bit so as to fix them.

(updated to fix a typo in the cpan link.)

One more update: a bit more logic is needed to handle various kinds and amounts of punctuation that might occur between the article and the word that follows:

while (<DATA>) { while ( /\b ((an?) \s\W* (\w+))/gx ) { my ( $phrase, $article, $word ) = ( $1, $2, $3 ); my $bigram = "$article $word"; if ( $bigram eq A( $word )) { print "RIGHT: $phrase\n"; } else { print "WRONG: $phrase\n"; } } } __DATA__ Suitable for a (older) child or an 'early adult'.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-19 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found