Skip to content
Browse files

Initial version

  • Loading branch information...
1 parent 8485a53 commit aa987860c4f1ce27d3ab01eb1b2d1f85ffe9a42f @doherty committed
Showing with 418 additions and 15 deletions.
  1. +1 −1 .gitignore
  2. +2 −2 CHANGES
  3. +59 −1 MANIFEST.SKIP
  4. +91 −0 README
  5. +1 −1 dist.ini
  6. +138 −0 lib/Lingua/Boolean.pm
  7. +0 −9 lib/Text/Boolean.pm
  8. +9 −0 t/00-init.t
  9. +34 −0 t/01-fail.t
  10. +41 −0 t/lang/en.t
  11. +41 −0 t/lang/fr.t
  12. +1 −1 weaver.ini
View
2 .gitignore
@@ -1,2 +1,2 @@
-Text-Boolean*
+Lingua-Boolean*
.build
View
4 CHANGES
@@ -1,4 +1,4 @@
-Revision history for Perl module Text-Boolean
+Revision history for Perl module Lingua::Boolean
{{$NEXT}}
- *
+ * Initial version
View
60 MANIFEST.SKIP
@@ -1,4 +1,62 @@
-#!include_default
+
+#!start included /usr/local/share/perl/5.10.1/ExtUtils/MANIFEST.SKIP
+# Avoid version control files.
+\bRCS\b
+\bCVS\b
+\bSCCS\b
+,v$
+\B\.svn\b
+\B\.git\b
+\B\.gitignore\b
+\b_darcs\b
+\B\.cvsignore$
+
+# Avoid VMS specific MakeMaker generated files
+\bDescrip.MMS$
+\bDESCRIP.MMS$
+\bdescrip.mms$
+
+# Avoid Makemaker generated and utility files.
+\bMANIFEST\.bak
+\bMakefile$
+\bblib/
+\bMakeMaker-\d
+\bpm_to_blib\.ts$
+\bpm_to_blib$
+\bblibdirs\.ts$ # 6.18 through 6.25 generated this
+
+# Avoid Module::Build generated and utility files.
+\bBuild$
+\b_build/
+\bBuild.bat$
+\bBuild.COM$
+\bBUILD.COM$
+\bbuild.com$
+
+# Avoid temp and backup files.
+~$
+\.old$
+\#$
+\b\.#
+\.bak$
+\.tmp$
+\.#
+\.rej$
+
+# Avoid OS-specific files/dirs
+# Mac OSX metadata
+\B\.DS_Store
+# Mac OSX SMB mount metadata files
+\B\._
+
+# Avoid Devel::Cover and Devel::CoverX::Covered files.
+\bcover_db\b
+\bcovered\b
+
+# Avoid MYMETA files
+^MYMETA\.
+#!end included /usr/local/share/perl/5.10.1/ExtUtils/MANIFEST.SKIP
+
# Specific to this project
^dist\.ini$
View
91 README
@@ -0,0 +1,91 @@
+NAME
+ Lingua::Boolean - comprehensively parse boolean response strings
+
+VERSION
+ version 0.001
+
+SYNOPSIS
+ use Lingua::Boolean;
+ print "Do it? ";
+ chomp(my $response = <>);
+ if ( boolean $response ) { # YES, y, OK, 1...
+ print "OK, doing it.\n";
+ }
+ else { # no, N, 0...
+ print "OK, not doing it.\n";
+ }
+
+ # Once more, with feeling
+ print "Fait-le? ";
+ chomp(my $response = <>);
+ if ( boolean $response, 'fr' ) { # OUI
+ print "OK, on le fait.\n";
+ }
+ else { # non
+ print "OK, on ne le fait pas.\n";
+ }
+
+DESCRIPTION
+ Does that string look like they said "true" or "false"? To know, you
+ have to check a lot of things. Lingua::Boolean attempts to do that in a
+ single module, and do so for multiple languages.
+
+FUNCTIONS
+ import
+ Calling "import()" will, obviously, import subs into your namespace. By
+ default, Lingua::Boolean imports the sub "boolean()". You can request to
+ have "looks_true()", "looks_false()", and "languages()" imported.
+
+ languages
+ "languages()" returns the list of languages Lingua::Boolean knows about.
+
+ looks_true
+ "looks_true()" tells you whether Lingua::Boolean thinks the thing looks
+ true. By default, it uses *en*; pass a language code as the second
+ parameter to check another language. Croaks if the language is unknown
+ to Lingua::Boolean.
+
+ looks_false
+ "looks_false()" tells you whether Lingua::Boolean thinks the thing looks
+ false. By default, it uses *en*; pass a language code as the second
+ parameter to check another language. Croaks if the language is unknown
+ to Lingua::Boolean.
+
+ boolean
+ "boolean()" tries to determine if the string "looks_true()" or
+ "looks_false()", and returns true or false accordingly. If both tests
+ fail, dies. By default, uses *en*; pass a language code as the second
+ parameter to check another language. Croaks if the language is unknown
+ to Lingua::Boolean.
+
+EXPORTS
+ By default, Lingua::Boolean exports "boolean()".
+
+AVAILABILITY
+ The latest version of this module is available from the Comprehensive
+ Perl Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a
+ CPAN site near you, or see
+ <http://search.cpan.org/dist/Lingua-Boolean/>.
+
+ The development version lives at
+ <http://github.com/doherty/Lingua-Boolean> and may be cloned from
+ <git://github.com/doherty/Lingua-Boolean>. Instead of sending patches,
+ please fork this project using the standard git and github
+ infrastructure.
+
+BUGS AND LIMITATIONS
+ No bugs have been reported.
+
+ Please report any bugs or feature requests through the web interface at
+ <http://github.com/doherty/Lingua-Boolean/issues>.
+
+AUTHOR
+ Mike Doherty <doherty@cs.dal.ca>
+
+COPYRIGHT AND LICENSE
+ This software is Copyright (c) 2010 by Mike Doherty.
+
+ This is free software, licensed under:
+
+ The GNU General Public License, Version 3, June 2007
+
View
2 dist.ini
@@ -1,4 +1,4 @@
-name = Text-Boolean
+name = Lingua-Boolean
author = Mike Doherty <doherty@cs.dal.ca>
license = GPL_3
copyright_holder = Mike Doherty
View
138 lib/Lingua/Boolean.pm
@@ -0,0 +1,138 @@
+use strict;
+use warnings;
+use 5.0100;
+
+package Lingua::Boolean;
+# ABSTRACT: comprehensively parse boolean response strings
+
+use Carp;
+use boolean 0.21 qw(true false);
+
+=head1 SYNOPSIS
+
+ use Lingua::Boolean;
+ print "Do it? ";
+ chomp(my $response = <>);
+ if ( boolean $response ) { # YES, y, OK, 1...
+ print "OK, doing it.\n";
+ }
+ else { # no, N, 0...
+ print "OK, not doing it.\n";
+ }
+
+ # Once more, with feeling
+ print "Fait-le? ";
+ chomp(my $response = <>);
+ if ( boolean $response, 'fr' ) { # OUI
+ print "OK, on le fait.\n";
+ }
+ else { # non
+ print "OK, on ne le fait pas.\n";
+ }
+
+=head1 DESCRIPTION
+
+Does that string look like they said "true" or "false"? To know, you
+have to check a lot of things. L<Lingua::Boolean> attempts to do that
+in a single module, and do so for multiple languages.
+
+=head1 FUNCTIONS
+
+=head2 import
+
+Calling C<import()> will, obviously, import subs into your namespace.
+By default, L<Lingua::Boolean> imports the sub C<boolean()>. You can
+request to have C<looks_true()>, C<looks_false()>, and C<languages()>
+imported.
+
+=cut
+
+use Exporter qw(import);
+our @EXPORT = qw(boolean); # EXPORTS
+our @EXPORT_OK = qw(looks_true looks_false languages);
+
+my $langs; # PATCHES WELCOME
+$langs->{en}->{yes} = [qr{^y(?:es)?$}i, qr{^on$}i, qr{^ok$}i, qr{^[1-9]$}];
+$langs->{en}->{no} = [qr{^no?$}i, qr{^off$}i, qr{not ?ok$}i, qr{^0$}];
+
+$langs->{fr}->{yes} = [qr{^oui$}i, qr{^ok$}i, qr{^[1-9]$}];
+$langs->{fr}->{no} = [qr{^n(?:on?)?$}i, qr{^0$}];
+
+=head2 languages
+
+C<languages()> returns the list of languages L<Lingua::Boolean> knows about.
+
+=cut
+
+sub languages {
+ return keys %$langs;
+}
+
+=head2 looks_true
+
+C<looks_true()> tells you whether L<Lingua::Boolean> thinks the thing looks
+true. By default, it uses I<en>; pass a language code as the second parameter
+to check another language. Croaks if the language is unknown to L<Lingua::Boolean>.
+
+=cut
+
+sub looks_true {
+ my $to_test = shift;
+ my $lang = shift || 'en';
+
+ croak "I don't know anything about the language '$lang'" unless exists $langs->{$lang};
+ return true if ($to_test ~~ $langs->{$lang}->{yes});
+ return false;
+}
+
+=head2 looks_false
+
+C<looks_false()> tells you whether L<Lingua::Boolean> thinks the thing looks
+false. By default, it uses I<en>; pass a language code as the second parameter
+to check another language. Croaks if the language is unknown to L<Lingua::Boolean>.
+
+=cut
+
+sub looks_false {
+ my $to_test = shift;
+ my $lang = shift || 'en';
+
+ croak "I don't know anything about the language '$lang'" unless exists $langs->{$lang};
+ return true if ($to_test ~~ $langs->{$lang}->{no});
+ return false;
+}
+
+=head2 B<boolean>
+
+B<C<boolean()>> tries to determine if the string C<looks_true()> or C<looks_false()>, and
+returns true or false accordingly. If both tests fail, dies. By default, uses I<en>; pass
+a language code as the second parameter to check another language. Croaks if the language
+is unknown to L<Lingua::Boolean>.
+
+=cut
+
+sub boolean {
+ my $to_test = shift;
+ my $lang = shift || 'en';
+
+ if (looks_true($to_test, $lang)) {
+ return true;
+ }
+ elsif (looks_false($to_test, $lang)) {
+ return false;
+ }
+ else {
+ #omg
+ die "'$to_test' isn't recognizable as either true or false";
+ }
+}
+
+=head1 EXPORTS
+
+By default, L<Lingua::Boolean> exports C<boolean()>.
+
+=cut
+
+1;
+
+__END__
View
9 lib/Text/Boolean.pm
@@ -1,9 +0,0 @@
-use strict;
-use warnings;
-
-package Text::Boolean;
-# ABSTRACT:
-
-1;
-
-__END__
View
9 t/00-init.t
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+use Test::More 0.75 tests => 2;
+
+BEGIN {
+ use_ok('Lingua::Boolean', qw(boolean looks_true looks_false languages));
+}
+
+can_ok('Lingua::Boolean', qw(boolean looks_true looks_false languages));
View
34 t/01-fail.t
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+use Lingua::Boolean;
+
+my $lang = 'nonexistent';
+
+my @languages = Lingua::Boolean::languages();
+ok(!($lang ~~ @languages), "$lang isn't available");
+
+subtest 'yes' => sub { #YES
+ my @yes = ('y', 'yes', 'ok', 'on', 'Y', 'YES', 'OK', 'ON', 1, 2);
+ plan tests => scalar @yes;
+
+ foreach my $word (@yes) {
+ eval {
+ boolean($word, $lang);
+ };
+ like($@, qr{^I don't know anything about the language '$lang'}o, 'failed OK');
+ }
+};
+
+subtest 'no' => sub { # NO
+ my @no = ('n', 'no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 0);
+ plan tests => scalar @no;
+
+ foreach my $word (@no) {
+ eval {
+ boolean($word, $lang);
+ };
+ like($@, qr{^I don't know anything about the language '$lang'}o, 'failed OK');
+ }
+};
View
41 t/lang/en.t
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use 5.0100;
+use Test::More tests => 4;
+
+use Lingua::Boolean;
+
+my $lang = 'en';
+
+my @languages = Lingua::Boolean::languages();
+ok($lang ~~ @languages, "$lang is available");
+
+subtest 'yes' => sub { #YES
+ my @yes = ('y', 'yes', 'ok', 'on', 'Y', 'YES', 'OK', 'ON', 1, 2);
+ plan tests => scalar @yes;
+
+ foreach my $word (@yes) {
+ ok(boolean($word, $lang), "$word is true");
+ }
+};
+
+subtest 'no' => sub { # NO
+ my @no = ('n', 'no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 0);
+ plan tests => scalar @no;
+
+ foreach my $word (@no) {
+ ok(!boolean($word, $lang), "$word is false");
+ }
+};
+
+subtest 'fail' => sub { # nonsense
+ my @nonsense = qw(one two three);
+ plan tests => scalar @nonsense;
+
+ foreach my $word (@nonsense) {
+ eval {
+ boolean($word, $lang);
+ };
+ like($@, qr{^'$word' isn't recognizable as either true or false}, "$word is nonsense");
+ }
+};
View
41 t/lang/fr.t
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use 5.0100;
+use Test::More tests => 4;
+
+use Lingua::Boolean;
+
+my $lang = 'fr';
+
+my @languages = Lingua::Boolean::languages();
+ok($lang ~~ @languages, "$lang is available");
+
+subtest 'yes' => sub { #YES
+ my @yes = ('oui', 'ok', 1);
+ plan tests => scalar @yes;
+
+ foreach my $word (@yes) {
+ ok(boolean($word, $lang), "$word is true");
+ }
+};
+
+subtest 'no' => sub { # NO
+ my @no = ('n', 'no', 'non', 0);
+ plan tests => scalar @no;
+
+ foreach my $word (@no) {
+ ok(!boolean($word, $lang), "$word is false");
+ }
+};
+
+subtest 'fail' => sub { # nonsense
+ my @nonsense = qw(one two three);
+ plan tests => scalar @nonsense;
+
+ foreach my $word (@nonsense) {
+ eval {
+ boolean($word, $lang);
+ };
+ like($@, qr{^'$word' isn't recognizable as either true or false}, "$word is nonsense");
+ }
+};
View
2 weaver.ini
@@ -4,7 +4,7 @@
[Version]
[Generic / SYNOPSIS]
[Generic / DESCRIPTION]
-[Generic / METHODS]
+[Generic / FUNCTIONS]
[Leftovers]

0 comments on commit aa98786

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