1package Lingua::EN::Inflect::Number;
2use 5.006;
3use strict;
4use warnings;
5
6require Exporter;
7our @ISA = qw(Exporter);
8our $VERSION = '1.1';
9our @EXPORT_OK = qw(to_PL to_S number);
10use Lingua::EN::Inflect qw(PL PL_N_eq);
11
12sub import {
13    my ($self, @syms) = @_;
14    # Grep out the ones we provide:
15    my $provide = join "|", map quotemeta, @EXPORT_OK;
16    my @new_syms;
17    for my $sym (@syms) {
18        if ($sym =~ /^\&?($provide)$/) {
19            $self->export_to_level(1, $self, $sym);
20        } else {
21            push @new_syms, $sym;
22        }
23    }
24    return unless @new_syms;
25
26    # Pretend we don't exist
27    @_ = ("Lingua::EN::Inflect", @new_syms);
28    goto &Exporter::import;
29}
30
31sub to_PL {
32    my $word = shift;
33    my $num = number($word);
34    return $word if $num eq "ambig" or $num eq "p";
35    return PL($word);
36}
37
38sub to_S {
39    my $word = shift;
40    my $num = number($word);
41    return $word if $num eq "ambig" or $num eq "s";
42    return PL($word); # I don't know why this works, but it seems to.
43}
44
45sub number {
46    my $word = shift;
47    my $test = PL_N_eq($word, PL($word));
48    $test =~ s/:.*//;
49    $test = "ambig" if $test eq "eq";
50    return $test;
51}
52
531;
54__END__
55# Below is stub documentation for your module. You better edit it!
56
57=head1 NAME
58
59Lingua::EN::Inflect::Number - Force number of words to singular or plural
60
61=head1 SYNOPSIS
62
63  use Lingua::EN::Inflect::Number qw(
64    number to_S to_PL # Or anything you want from Lingua::EN::Inflect
65  );
66
67  print number("goat");  # "s" - there's only one goat
68  print number("goats"); # "p" - there's several goats
69  print number("sheep"); # "ambig" - there could be one or many sheep
70
71  print to_S("goats");   # "goat"
72  print to_PL("goats");  # "goats" - it already is
73  print to_S("goat");    # "goat" - it already is
74  print to_S("sheep");   # "sheep"
75
76=head1 DESCRIPTION
77
78This module extends the functionality of Lingua::EN::Inflect with three
79new functions available for export:
80
81=head2 number
82
83This takes a word, and determines its number. It returns C<s> for singular,
84C<p> for plural, and C<ambig> for words that can be either singular or plural.
85
86Based on that:
87
88=head2 to_S / to_PL
89
90These take a word and convert it forcefully either to singular or to
91plural. C<Lingua::EN::Inflect> does funny things if you try to pluralise
92an already-plural word, but this module does the right thing.
93
94=head1 DISCLAIMER
95
96The whole concept is based on several undocumented features and
97idiosyncracies in the way Lingua::EN::Inflect works. Because of this,
98the module only works reliably on nouns. It's also possible that these
99idiosyncracies will be fixed at some point in the future and this module
100will need to be rethought. But it works at the moment. Additionally,
101any disclaimers on Lingua::EN::Inflect apply double here.
102
103=head1 AUTHOR
104
105Simon Cozens, C<simon@cpan.org>
106
107=head1 SEE ALSO
108
109L<Lingua::EN::Inflect>.
110
111=cut
112