Lingua::EN::Inflect::Phrase - Inflect short English Phrases


Lingua-EN-Inflect-Phrase documentation Contained in the Lingua-EN-Inflect-Phrase distribution.

Index


Code Index:

NAME

Top

Lingua::EN::Inflect::Phrase - Inflect short English Phrases

SYNOPSIS

Top

  use Lingua::EN::Inflect::Phrase;
  use Test::More tests => 2;

  my $plural   = Lingua::EN::Inflect::Phrase::to_PL('green egg and ham');

  is $plural, 'green eggs and ham';

  my $singular = Lingua::EN::Inflect::Phrase::to_S('green eggs and ham');

  is $singular, 'green egg and ham';

DESCRIPTION

Top

Attempts to pluralize or singularize short English phrases.

If it doesn't work, please email or submit to RT the example you tried, and I'll try to fix it.

OPTIONAL EXPORTS

Top

to_PL, to_S

SUBROUTINES

Top

to_PL

Attempts to pluralizes a phrase unless already plural.

to_S

Attempts to singularize a phrase unless already singular.

BUGS

Top

Please report any bugs or feature requests to bug-lingua-en-inflect-phrase at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Lingua-EN-Inflect-Phrase. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

More information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-EN-Inflect-Phrase

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Lingua-EN-Inflect-Phrase

* CPAN Ratings

http://cpanratings.perl.org/d/Lingua-EN-Inflect-Phrase

* Search CPAN

http://search.cpan.org/dist/Lingua-EN-Inflect-Phrase/

REPOSITORY

Top

  git clone git://github.com/rkitover/lingua-en-inflect-phrase.git lingua-en-inflect-phrase

SEE ALSO

Top

Lingua::EN::Inflect, Lingua::EN::Inflect::Number, Lingua::EN::Tagger

AUTHOR

Top

Rafael Kitover <rkitover@cpan.org>

LICENSE AND COPYRIGHT

Top


Lingua-EN-Inflect-Phrase documentation Contained in the Lingua-EN-Inflect-Phrase distribution.
package Lingua::EN::Inflect::Phrase;

use strict;
use warnings;
use Exporter 'import';
use Lingua::EN::Inflect::Number;
use Lingua::EN::Tagger;

our $VERSION = '0.04';

our @EXPORT_OK = qw/to_PL to_S/;

my $NOUN = qr{(\S+)/(NN|CD)};

my $tagger;

sub _inflect_noun {
  my ($noun, $is_plural, $want_plural, $inflect_method) = @_;
  my $want_singular = not $want_plural;

  if (($want_plural && (not $is_plural)) || ($want_singular && $is_plural)) {
    return $noun->$inflect_method;
  }

  return undef;
}

sub _inflect {
  my ($phrase, $want_plural, $method) = @_;
  my $want_singular = not $want_plural;

  $tagger ||= Lingua::EN::Tagger->new;

  my $tagged = $tagger->get_readable($phrase);
  my $noun;

# last noun before a preposition/conjunction
# or last noun
  if ((($noun) = $tagged =~ m{$NOUN (?!.*/(?:NN|CD|JJ).*/(?:CC|IN)) .* /(?:CC|IN)}x) or
      (($noun) = $tagged =~ m{$NOUN (?!.*/(?:NN|CD|JJ))}x)) {
    my $is_plural = Lingua::EN::Inflect::Number::number($noun) ne 's';
    my $inflected_noun = _inflect_noun($noun, $is_plural, $want_plural, $method);

    substr($tagged, $-[1], ($+[1] - $-[1])) = $inflected_noun if $inflected_noun;

    ($phrase = $tagged) =~ s{/[A-Z]+}{}g;
  }
# fallback
  else {
    my $number = Lingua::EN::Inflect::Number::number($phrase);

    if (($want_plural && $number ne 'p') || ($want_singular && $number ne 's')) {
      $phrase = $phrase->$method;
    }
  }

  return $phrase;
}

sub to_PL {
  return _inflect(shift, 1, \&Lingua::EN::Inflect::Number::to_PL);
}

sub to_S {
  return _inflect(shift, 0, \&Lingua::EN::Inflect::Number::to_S);
}

1;
# vim:et sts=2 sw=2 tw=0: