| 1 | package Bot::Training; | ||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:AVAR'; | |||||||||||||||||||
| 3 | 2 | $Bot::Training::VERSION = '0.06'; | |||||||||||||||||||
| 4 | 2 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use autodie qw(open close); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Class::Load; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | use Module::Pluggable ( | ||||||||||||||||||||
| 9 | 2 | search_path => [ 'Bot::Training' ], | |||||||||||||||||||
| 10 | except => [ 'Bot::Training::Plugin' ], | ||||||||||||||||||||
| 11 | 2 | ▶ | ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 2 | ▶ | use List::Util qw< first >; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | ▶ | use namespace::clean -except => [ qw< meta plugins > ]; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | with 'MooseX::Getopt::Dashes'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | has help => ( | |||||||||||||||||||
| 18 | traits => [ qw/ Getopt / ], | ||||||||||||||||||||
| 19 | cmd_aliases => 'h', | ||||||||||||||||||||
| 20 | cmd_flag => 'help', | ||||||||||||||||||||
| 21 | isa => 'Bool', | ||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||
| 23 | default => 0, | ||||||||||||||||||||
| 24 | documentation => 'This help message', | ||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 2 | has _go_version => ( | |||||||||||||||||||
| 28 | traits => [ qw/ Getopt / ], | ||||||||||||||||||||
| 29 | cmd_aliases => 'v', | ||||||||||||||||||||
| 30 | cmd_flag => 'version', | ||||||||||||||||||||
| 31 | documentation => 'Print version and exit', | ||||||||||||||||||||
| 32 | isa => 'Bool', | ||||||||||||||||||||
| 33 | is => 'ro', | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 2 | has _go_list => ( | |||||||||||||||||||
| 37 | traits => [ qw/ Getopt / ], | ||||||||||||||||||||
| 38 | cmd_aliases => 'l', | ||||||||||||||||||||
| 39 | cmd_flag => 'list', | ||||||||||||||||||||
| 40 | documentation => 'List the known Bot::Training files. Install Task::Bot::Training to get them all', | ||||||||||||||||||||
| 41 | isa => 'Bool', | ||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 2 | has _go_file => ( | |||||||||||||||||||
| 46 | traits => [ qw/ Getopt / ], | ||||||||||||||||||||
| 47 | cmd_aliases => 'f', | ||||||||||||||||||||
| 48 | cmd_flag => 'file', | ||||||||||||||||||||
| 49 | documentation => 'The file to retrieve. Matched case-insensitively against Bot::Training plugins', | ||||||||||||||||||||
| 50 | isa => 'Str', | ||||||||||||||||||||
| 51 | is => 'ro', | ||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | sub _new_class { | ||||||||||||||||||||
| 55 | 1 | ▶ | my ($self, $class) = @_; | ||||||||||||||||||
|
Subroutine
_new_class: called CC=3 SCAR=12.9
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 1 | my $pkg; | |||||||||||||||||||
| 58 | 1 | ▶ | if ($class =~ m[^\+(?<custom_plugin>.+)$]) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | $pkg = $+{custom_plugin}; | |||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||
| 61 | # Be fuzzy about includes, e.g. Training::Test, Test or test is OK | ||||||||||||||||||||
| 62 | 0 | ▶ | $pkg = first { / : $class /ix } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 63 | 1 | sort { length $a <=> length $b } | |||||||||||||||||||
| 64 | $self->plugins; | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 1 | ▶ | unless ($pkg) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 1 | local $" = ', '; | |||||||||||||||||||
| 68 | 1 | my @plugins = $self->plugins; | |||||||||||||||||||
| 69 | 1 | die "Couldn't find a class name matching '$class' in plugins '@plugins'"; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | Class::Load::load_class($pkg); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | return $pkg->new; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub file { | ||||||||||||||||||||
| 79 | 1 | ▶ | my ($self, $fuzzy) = @_; | ||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 1 | return $self->_new_class($fuzzy); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | sub run { | ||||||||||||||||||||
| 86 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
run: not called CC=8 SCAR=40.1
Pod
undocumented
| |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | ▶ | if ($self->_go_version) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | # Munging strictness because we don't have a version from a | ||||||||||||||||||||
| 90 | # Git checkout. Dist::Zilla provides it. | ||||||||||||||||||||
| 91 | 2 | ▶ | no strict 'vars'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 92 | 0 | ▶ | my $version = $VERSION // 'dev-git'; | ||||||||||||||||||
|
Condition0%0/2
$VERSION // "dev-git"
MC/DC0%0/1
$VERSION // "dev-git"
$VERSION
Truth table0%0/2
A
A= $VERSION
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | say "bot-training $version"; | |||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | ▶ | if ($self->_go_list) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | my @plugins = $self->plugins; | |||||||||||||||||||
| 100 | 0 | ▶ | if (@plugins) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 0 | say for @plugins; | |||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||
| 103 | 0 | say "No plugins loaded. Install Task::Bot::Training"; | |||||||||||||||||||
| 104 | 0 | return 1; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | ▶ | if ($self->_go_file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 0 | my $trn = $self->file( $self->_go_file );; | |||||||||||||||||||
| 110 | 0 | open my $fh, "<", $trn->file; | |||||||||||||||||||
| 111 | 0 | ▶ | print while <$fh>; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | close $fh; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | # --i--do-not-exist | ||||||||||||||||||||
| 118 | 0 | ▶ | sub _getopt_spec_exception { goto &_getopt_full_usage } | ||||||||||||||||||
|
Subroutine
_getopt_spec_exception: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # --help | ||||||||||||||||||||
| 121 | sub print_usage_text { | ||||||||||||||||||||
| 122 | 0 | ▶ | my ($self, $usage, $plain_str) = @_; | ||||||||||||||||||
|
Subroutine
print_usage_text: not called CC=5 SCAR=31.9
Pod
undocumented
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # If called from _getopt_spec_exception we get "Unknown option: foo" | ||||||||||||||||||||
| 125 | 0 | ▶ | my $warning = ref $usage eq 'ARRAY' ? $usage->[0] : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | my ($use, $options) = do { | |||||||||||||||||||
| 128 | # $plain_str under _getopt_spec_exception | ||||||||||||||||||||
| 129 | 0 | ▶ | my $out = $plain_str // $usage->text; | ||||||||||||||||||
|
Condition0%0/3
$plain_str // $usage->text
MC/DC0%0/2
$plain_str // $usage->text
$plain_str
$usage->text
Truth table0%0/3
A // B
A= $plain_strB= $usage->text
| |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | # The default getopt order sucks, use reverse sort order | ||||||||||||||||||||
| 132 | 0 | my @out = split /^/, $out; | |||||||||||||||||||
| 133 | 0 | chomp @out; | |||||||||||||||||||
| 134 | 0 | my $opt = join "\n", sort { $b cmp $a } @out[1 .. $#out]; | |||||||||||||||||||
| 135 | 0 | ($out[0], $opt); | |||||||||||||||||||
| 136 | }; | ||||||||||||||||||||
| 137 | 0 | my $synopsis = do { | |||||||||||||||||||
| 138 | 0 | require Pod::Usage; | |||||||||||||||||||
| 139 | 0 | my $out; | |||||||||||||||||||
| 140 | 0 | open my $fh, '>', \$out; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 2 | ▶ | no warnings 'once'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | my $hailo = File::Spec->catfile($Hailo::Command::HERE_MOMMY, 'hailo'); | |||||||||||||||||||
| 145 | # Try not to fail on Win32 or other odd systems which might have hailo.pl not hailo | ||||||||||||||||||||
| 146 | 0 | ▶ | $hailo = ((glob("$hailo*"))[0]) unless -f $hailo; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 0 | Pod::Usage::pod2usage( | |||||||||||||||||||
| 148 | -input => $hailo, | ||||||||||||||||||||
| 149 | -sections => 'SYNOPSIS', | ||||||||||||||||||||
| 150 | -output => $fh, | ||||||||||||||||||||
| 151 | -exitval => 'noexit', | ||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||
| 153 | 0 | close $fh; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 0 | $out =~ s/\n+$//s; | |||||||||||||||||||
| 156 | 0 | $out =~ s/^Usage:/examples:/; | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 0 | $out; | |||||||||||||||||||
| 159 | }; | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | # Unknown option provided | ||||||||||||||||||||
| 162 | 0 | ▶ | print $warning if $warning; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | print <<"USAGE"; | |||||||||||||||||||
| 165 | $use | ||||||||||||||||||||
| 166 | $options | ||||||||||||||||||||
| 167 | USAGE | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 0 | say "\n", $synopsis; | |||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | exit 1; | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =encoding utf8 | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 NAME | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Bot::Training - Plain text training material for bots like L<Hailo> and L<AI::MegaHAL> | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =head1 SYNOPSIS | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | use Bot::Training; | ||||||||||||||||||||
| 185 | use File::Slurp qw< slurp >; | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | my $bt = Bot::Training->new; | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | # Plugins I know about. Install Task::Bot::Training for more: | ||||||||||||||||||||
| 190 | my @plugins = $bt->plugins | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | # Get the plugin object of a .trn file (which is just a plain text | ||||||||||||||||||||
| 193 | # file). These all work just as well: | ||||||||||||||||||||
| 194 | my $hal = $bt->file('megahal'); | ||||||||||||||||||||
| 195 | my $hal = $bt->file('MegaHAL'); | ||||||||||||||||||||
| 196 | my $hal = $bt->file('Bot::Training::MegaHAL'); | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | # Get all lines in the file with File::Slurp: | ||||||||||||||||||||
| 199 | my @test = split /\n/, slurp($hal->file); | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =head1 DESCRIPTION | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | Markov bots like L<Hailo> and L<AI::MegaHAL> are fun. But to get them | ||||||||||||||||||||
| 204 | working you either need to train them on existing training material or | ||||||||||||||||||||
| 205 | make your own. | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | This module provides a pluggable way to install already existing | ||||||||||||||||||||
| 208 | training files via the CPAN. It also comes with a command-line | ||||||||||||||||||||
| 209 | interface called C<bot-training>. | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org> | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org> | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | This program is free software, you can redistribute it and/or modify | ||||||||||||||||||||
| 220 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||