Skip to content
Browse files

fix for ordinal number phrases (RT#82242)

Fix 'first release' to not singularize to 'first releases'. This was
happening because Lingua::EN::FindNumber finds ordinal numbers as well
as cardinal.

Release a new module, Lingua::EN::Number::IsOrdinal to help with this
situation and use it here.
  • Loading branch information...
1 parent e91e030 commit 5b875438df3872938cd6588729fc2a13c88ab3cc @rkitover committed Dec 25, 2012
Showing with 28 additions and 114 deletions.
  1. +3 −0 .gitignore
  2. +3 −0 Changes
  3. +0 −103 README
  4. +14 −11 lib/Lingua/EN/Inflect/Phrase.pm
  5. +8 −0 t/numbers.t
View
3 .gitignore
@@ -0,0 +1,3 @@
+.*.sw[nop]
+.build/
+Lingua-EN-Inflect-Phrase-*
View
3 Changes
@@ -2,6 +2,9 @@ Revision history for Lingua-EN-Inflect-Phrase
{{$NEXT}}
+ - fix for ordinal number phrases, 'first release' was singularizing to
+ 'first releases' (RT#82242)
+
0.16 2012-11-05 02:43:34
- bump up Lingua::EN::Tagger dep again due to unescaped braces in
regexes
View
103 README
@@ -1,103 +0,0 @@
-NAME
- Lingua::EN::Inflect::Phrase - Inflect short English Phrases
-
-SYNOPSIS
- 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
- Attempts to pluralize or singularize short English phrases.
-
- Does not throw exceptions at present, if you attempt to pluralize an
- already pluralized phrase, it will leave it unchanged (and vice versa.)
-
- The behavior of this module is subject to change as I tweak the
- heuristics, as some things get fixed others might regress. The
- processing of natural language is a messy business.
-
- If it doesn't work, please email or submit to RT the example you tried,
- and I'll try to fix it.
-
-OPTIONS
- By default, this module prefers to treat words as nouns (sometimes words
- can be interpreted as a verb or a noun without context.) This is better
- for things such as database table/column names, which is what this
- module is primarily for.
-
- This behavior can be switched with the variable $prefer_nouns. The
- default is 1.
-
- For example:
-
- {
- local $Lingua::EN::Inflect::Phrase::prefer_nouns = 0;
- is Lingua::EN::Inflect::Phrase::to_S('sources split'), 'source splits';
- }
- {
- local $Lingua::EN::Inflect::Phrase::prefer_nouns = 1;
- is Lingua::EN::Inflect::Phrase::to_S('source splits'), 'source split';
- }
-
-OPTIONAL EXPORTS
- "to_PL", "to_S"
-
-SUBROUTINES
- to_PL
- Attempts to pluralizes a phrase unless already plural.
-
- to_S
- Attempts to singularize a phrase unless already singular.
-
-BUGS
- 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
- 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
- git clone git://github.com/rkitover/lingua-en-inflect-phrase.git lingua-en-inflect-phrase
-
-SEE ALSO
- Lingua::EN::Inflect, Lingua::EN::Inflect::Number, Lingua::EN::Tagger
-
-AUTHOR
- Rafael Kitover <rkitover@cpan.org>
-
-LICENSE AND COPYRIGHT
- Copyright (c) 2010 Rafael Kitover (rkitover@cpan.org).
-
- This program is free software; you can redistribute it and/or modify it
- under the terms of either: the GNU General Public License as published
- by the Free Software Foundation; or the Artistic License.
-
- See http://dev.perl.org/licenses/ for more information.
-
View
25 lib/Lingua/EN/Inflect/Phrase.pm
@@ -7,6 +7,7 @@ use Lingua::EN::Inflect;
use Lingua::EN::Inflect::Number;
use Lingua::EN::Tagger;
use Lingua::EN::FindNumber '$number_re';
+use Lingua::EN::Number::IsOrdinal 'is_ordinal';
=head1 NAME
@@ -194,20 +195,22 @@ sub _inflect {
$number = (sort { length $a <=> length $b } map $_||'', ($1, $2, $3, $4, $5))[-1];
- my $tagged_number_re;
+ if (not is_ordinal($number)) {
+ my $tagged_number_re;
- foreach my $num_elem (split /\s+/, $number) {
- $tagged_number_re .= "\Q$num_elem\E/[A-Z]+\\s*";
- }
+ foreach my $num_elem (split /\s+/, $number) {
+ $tagged_number_re .= "\Q$num_elem\E/[A-Z]+\\s*";
+ }
- my $tagged_number;
- ($tagged_number, $pad, $rest) = $tagged =~ m/($tagged_number_re)(\s*)(.*)/;
- my @tagged_number_pos = ($-[1], $+[1]);
+ my $tagged_number;
+ ($tagged_number, $pad, $rest) = $tagged =~ m/($tagged_number_re)(\s*)(.*)/;
+ my @tagged_number_pos = ($-[1], $+[1]);
- if (length $rest) {
- substr($tagged, $tagged_number_pos[0], ($tagged_number_pos[1] - $tagged_number_pos[0])) = $number;
- $want_plural = 1;
- $want_singular = 0;
+ if (length $rest) {
+ substr($tagged, $tagged_number_pos[0], ($tagged_number_pos[1] - $tagged_number_pos[0])) = $number;
+ $want_plural = 1;
+ $want_singular = 0;
+ }
}
}
View
8 t/numbers.t
@@ -79,6 +79,14 @@ test_phrase 'the two', 'the twos';
test_phrase 'one', 'ones';
test_phrase 'two', 'twos';
+# ordinal numbers
+
+test_phrase '1st release', '1st releases';
+test_phrase 'first release', 'first releases';
+test_phrase 'second trip', 'second trips';
+test_phrase 'twenty third egg', 'twenty third eggs';
+test_phrase '2nd car', '2nd cars';
+
Test::NoWarnings::had_no_warnings;
done_testing;

0 comments on commit 5b87543

Please sign in to comment.
Something went wrong with that request. Please try again.