Acme-Wabby
view release on metacpan or search on metacpan
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 19yy <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) 19yy name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
sub add {
my $self = shift;
die "Invalid object" unless (ref($self) eq __PACKAGE__);
# Make sure we actually got something to add
my $text = shift;
unless ($text) {
return undef;
}
# If we don't care about case, lowercase the whole thing to start with
unless ($self->{'conf'}{'case_sensitive'}) {
$text = lc($text);
}
# Split the text into component phrases, which we define as being delimited
# by the characters below. I left the comma out because it seems to lead
# to slightly more coherent results.
my @phrases = split /[.!?;]/, $text;
foreach my $phrase (@phrases) {
my $text = shift;
# If we don't have at least 10 * min_len words, we probably don't have a
# very good chance of making a sentence, so let's just return.
if (scalar(keys %{$self->{'data'}{'hash'}}) <
($self->{'conf'}{'min_len'} * 10)) {
return undef;
}
my $directed;
my $start;
# If they passed in an argument, take a look at it.
if ($text) {
$directed = 1;
# If we're case-insensitive, lowercase what they sent us.
unless ($self->{'conf'}{'case_sensitive'}) {
$text = lc($text);
}
# If there's not a word left to talk about, return.
if ($text !~ /([-a-zA-Z0-9']+)$/) {
return undef;
}
# If we don't know anything about this word, return.
if (!exists(${$self->{'data'}{'hash'}}{$1})) {
return undef;
}
# Seems like a good starting place, so let's mark it.
$start = ${$self->{'data'}{'hash'}}{$1};
}
# They didn't pass an argument, so we're on our own.
else {
$directed = 0;
# The 0th element in the list is 'special' in that no hash entry points
# to it, and it only contains pointers to words which are possible
# sentence starting points. Thus, let's grab a random entry out of the
# 0th element in the list and start there.
$start = ${${$self->{'data'}{'list'}}[0]{'num'}}[int rand scalar @{${$self->{'data'}{'list'}}[0]{'num'}}];
$text = ${$self->{'data'}{'list'}}[$start]{'word'};
}
# Since we're dealing with randomness, we can't always be sure that we'll
# be able to make a sentence of min_len, so we just keep retrying up to
# max_attempts times, relying on sheer dumb luck to help us out. On a
# reasonably-sized body of text, this works perfectly fine.
my $attempts = 0;
my $count = 0;
my $final = "";
my $next = $start;
while ($count < $self->{'conf'}{'min_len'} &&
$attempts < $self->{'conf'}{'max_attempts'}) {
# We start out with one word, and uppercase the first character in our
# starting text.
$count = 1;
$final = "\u$text";
$next = $start;
# Keep adding new words to this sentence until we hit an sentence end
# mark, or we hit max_len
while ($next != -1 && ($count < $self->{'conf'}{'max_len'})) {
# If the word we're on has no transitions, count this as a stopping
# point, since we can't go any further.
if (scalar(@{${$self->{'data'}{'list'}}[$next]{'num'}}) < 1) {
$next = -1;
}
# If we haven't yet passed our max number of attempts, try again.
if ($attempts < $self->{'conf'}{'max_attempts'}) {
$attempts++;
next;
}
# If we passed our max number of attempts, we can take one of two
# course of action.
else {
# If we're trying to talk about something in particular, we're
# always going to be stuck with the same starting point. Thus,
# there's not the best chance for continued success, so just
# give up and bail.
if ($directed) {
return undef;
}
# If we're talking about random things, we likely just got
# a bad starting point, so we'll pick a new random starting
# point, and do the whole thing over again.
else {
$attempts = 0;
$start = ${${$self->{'data'}{'list'}}[0]{'num'}}[int rand scalar @{${$self->{'data'}{'list'}}[0]{'num'}}];
$text = ${$self->{'data'}{'list'}}[$start]{'word'};
next;
}
}
}
}
# If we're not case sensitive, make sure any I's by themselves are
# capitalized, for aesthetic purposes. If we are, they probably want
# things to come out the way they are.
# FIXME - Need to be able to configure this so that persons with
( run in 0.228 second using v1.01-cache-2.11-cpan-0d8aa00de5b )