| 1 | package analogize; | ||
| 2 | # ABSTRACT: classify data with AM from the command line | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '3.13'; | ||
| 6 | use 5.010; | ||
| 7 | use Carp; | ||
| 8 | use Algorithm::AM::Batch; | ||
| 9 | # Log only warnings by default, since the user chooses the desired reports using | ||
| 10 | # the script parameters | ||
| 11 | use Log::Any::Adapter ('Stdout', log_level => 'warn'); | ||
| 12 | use Path::Tiny; | ||
| 13 | # 2.13 needed for aliases | ||
| 14 | use Getopt::Long 2.13 qw(GetOptionsFromArray); | ||
| 15 | use Pod::Usage; | ||
| 16 | |||
| 17 | _run(@ARGV) unless caller; | ||
| 18 | |||
| 19 | sub _run { | ||
| 20 | my %args = ( | ||
| 21 | # defaults here... | ||
| 22 | ); | ||
| 23 | GetOptionsFromArray(\@_, \%args, | ||
| 24 | 'format=s', | ||
| 25 | 'exemplars|train|data:s', | ||
| 26 | 'project:s', | ||
| 27 | 'test:s', | ||
| 28 | 'print:s', | ||
| 29 | 'include_given', | ||
| 30 | 'include_nulls', | ||
| 31 | 'linear', | ||
| 32 | 'help|?', | ||
| 33 | ) or pod2usage(2); | ||
| 34 | _validate_args(%args); | ||
| 35 | |||
| 36 | my @print_methods; | ||
| 37 | if($args{print}){ | ||
| 38 | @print_methods = split ',', $args{print}; | ||
| 39 | } | ||
| 40 | |||
| 41 | my ($train, $test); | ||
| 42 | if($args{exemplars}){ | ||
| 43 | $train = dataset_from_file( | ||
| 44 | path => $args{exemplars}, | ||
| 45 | format => $args{format}); | ||
| 46 | } | ||
| 47 | if($args{test}){ | ||
| 48 | $test = dataset_from_file( | ||
| 49 | path => $args{test}, | ||
| 50 | format => $args{format}); | ||
| 51 | } | ||
| 52 | if($args{project}){ | ||
| 53 | $train = dataset_from_file( | ||
| 54 | path => path($args{project})->child('data'), | ||
| 55 | format => $args{format}); | ||
| 56 | if(path($args{project})->child('test')->exists){ | ||
| 57 | $test = dataset_from_file( | ||
| 58 | path => path($args{project})->child('test'), | ||
| 59 | format => $args{format}); | ||
| 60 | }else{ | ||
| 61 | $test = $train; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | # default to leave-one-out if no test set specified | ||
| 65 | $test ||= $train; | ||
| 66 | |||
| 67 | my $count = 0; | ||
| 68 | my $batch = Algorithm::AM::Batch->new( | ||
| 69 | linear => $args{linear}, | ||
| 70 | exclude_given => !$args{include_given}, | ||
| 71 | exclude_nulls => !$args{include_nulls}, | ||
| 72 | |||
| 73 | training_set => $train, | ||
| 74 | # print the result of each classification at the time it is provided | ||
| 75 | end_test_hook => sub { | ||
| 76 | my ($batch, $test_item, $result) = @_; | ||
| 77 | ++$count if $result->result eq 'correct'; | ||
| 78 | say $test_item->comment . ":\t" . $result->result . "\n"; | ||
| 79 | for (@print_methods) { | ||
| 80 | if($_ eq 'gang_detailed'){ | ||
| 81 | say ${ $result->gang_summary(1) }; | ||
| 82 | }else{ | ||
| 83 | say ${ $result->$_ }; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | ); | ||
| 88 | $batch->classify_all($test); | ||
| 89 | |||
| 90 | say "$count out of " . $test->size . " correct"; | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _validate_args { | ||
| 95 | my %args = @_; | ||
| 96 | if($args{help}){ | ||
| 97 | pod2usage(1); | ||
| 98 | } | ||
| 99 | my $errors = ''; | ||
| 100 | if(!$args{exemplars} and !$args{project}){ | ||
| 101 | $errors .= "Error: need either --exemplars or --project parameters\n"; | ||
| 102 | }elsif(($args{exemplars} or $args{test}) and $args{project}){ | ||
| 103 | $errors .= "Error: --project parameter cannot be used with --exempalrs or --test\n"; | ||
| 104 | } | ||
| 105 | if(!defined $args{format}){ | ||
| 106 | $errors .= "Error: missing --format parameter\n"; | ||
| 107 | }elsif($args{format} !~ m/^(?:no)?commas$/){ | ||
| 108 | $errors .= | ||
| 109 | "Error: --format parameter must be either 'commas' or 'nocommas'\n"; | ||
| 110 | } | ||
| 111 | if($args{print}){ | ||
| 112 | my %allowed = | ||
| 113 | map {$_ => 1} qw( | ||
| 114 | config_info | ||
| 115 | statistical_summary | ||
| 116 | analogical_set_summary | ||
| 117 | gang_summary | ||
| 118 | gang_detailed | ||
| 119 | ); | ||
| 120 | for my $param (split ',', $args{print}){ | ||
| 121 | if(!exists $allowed{$param}){ | ||
| 122 | $errors .= "Error: unknown print parameter '$param'\n"; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | if($errors){ | ||
| 127 | $errors .= 'use "analogize --help" for detailed usage information'; | ||
| 128 | chomp $errors; | ||
| 129 | pod2usage($errors); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | __END__ |