lib/NanoB2B/NER/Modelman.pm

filter: Statementstmt 31.0% 23 / 74 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 0.0% 0 / 2 totaltotal 26.7% 27 / 101 CC 12 SCAR 36.2
CC
12
Cov
25%
_init
72.0
make_model_file
20.0
new
6.0
CRAP 37.3
« lib/NanoB2B/NER/Wekaman.pm ↑ Summary lib/NanoB2B/NER/Metaman.pm »
1 #!/usr/bin/perl
2 # NanoB2B-NER::NER::Modelman
3 #
4 # Turns the ARFF Train files into models and loads models with ARFF Test files
5 # Version 1.0
6 #
7 # Program by Milk
8  
9 package NanoB2B::NER::Modelman;
10  
11 1 ▶use NanoB2B::UniversalRoutines;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Path qw(make_path); #makes sub directories
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #### GLOBAL VARIABLES ####
17  
18 #option variables
19 1 my $program_dir;
20 1 my $classifier = "weka.classifiers.bayes.NaiveBayes";
21 1 my $weka_size = "Xmx4G";
22 1 my @features;
23 1 my $buckets = 10;
24 1 my $debug = 0;
25  
26  
27 #hardcoded for now can be programmer later
28 1 my $C_val = 0.25;
29 1 my $M_val = 2;
30  
31  
32 #universal subroutines object
33 1 my %uniParams = ();
34 1 my $uniSub;
35  
36  
37 #### A CIVILLIAN IS SAVED ####
38  
39 # construction method to create a new Wekaman object
40 # input : $directory <-- the name of the directory for the files
41 # $features <-- the set of features to run on [e.g. omtpcs]
42 # \$type <-- the weka algorithm to run the set on [e.g. weka.classifiers.functions.SMO]
43 # \$weka_size <-- the size to for the memory allocation in the weka parameter [e.g. -Xmx6G]
44 # \$buckets <-- the number of buckets used for the k-fold cross validation
45 # \$debug <-- the set of features to run on [e.g. omtpcs]
46 # output : $self <-- an instance of the Wekaman object
47 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
48 #grab class and parameters
49 0 my $self = {};
50 0 my $class = shift;
51 0 ▶ return undef if(ref $class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
52 0 my $params = shift;
53  
54 #bless this object
55 0 bless $self, $class;
56 0 $self->_init($params);
57  
58 #retrieve parameters for universal-routines
59 0 $uniParams{'debug'} = $debug;
60 0 $uniSub = NanoB2B::UniversalRoutines->new(\%uniParams);
61  
62 #return the object
63 0 return $self;
64 }
65 # method to initialize the NanoB2B::NER::Wekaman object.
66 # input : $parameters <- reference to a hash
67 # output:
68 0 ▶sub _init {
Subroutine _init: not called CC=8 SCAR=42.8
69 0 my $self = shift;
70 0 my $params = shift;
71  
72 0 ▶ $params = {} if(!defined $params);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params 0 0 0
73  
74 # get some of the parameters
75 0 my $diroption = $params->{'directory'};
76 0 my $ftsoption = $params->{'features'};
77 0 my $bucketsNumoption = $params->{'buckets'};
78 0 my $typeoption = $params->{'type'};
79 0 my $sizeoption = $params->{'weka_size'};
80 0 my $debugoption = $params->{'debug'};
81  
82 #set the global variables
83 0 ▶ if(defined $debugoption){$debug = $debugoption;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debugoption) 0 0 0
84 0 ▶ if(defined $diroption){$program_dir = $diroption;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $diroption) 0 0 0
85 0 ▶ if(defined $bucketsNumoption){$buckets = $bucketsNumoption;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bucketsNumoption) 0 0 0
86 0 ▶ if(defined $ftsoption){@features = split(' ', $ftsoption); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ftsoption) 0 0 0
87 0 ▶ if(defined $typeoption){$classifier = $typeoption};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $typeoption) 0 0 0
88 0 ▶ if(defined $sizeoption){$weka_size = $sizeoption};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sizeoption) 0 0 0
89 }
90  
91  
92 ############### I'M AN EVERYDAY AVERAGE MODELMAN ################
93  
94 # runs the arff files through weka to export models
95 # input : $name <-- the name of the file to run through weka - model maker
96 # output: (model files)
97 0 ▶sub make_model_file{
Subroutine make_model_file: not called CC=4 SCAR=30.0
Pod undocumented
98 0 my $self = shift;
99 0 my $name = shift;
100  
101 0 $name = lc($name);
102  
103 #split them up by sets
104 0 my @sets = ();
105 0 my $item = "_";
106 0 foreach my $fs (@features){
107 0 my $abbrev = substr($fs, 0, 1); #add to abbreviations for the name
108 0 $item .= $abbrev;
109 0 push(@sets, $item);
110 }
111  
112 #get the ending part of the classifier for the weka dir name
113 0 my @b = split(/\./, $classifier);
114 0 my $weka_dir = $b[$#b];
115  
116 #run each set through weka and save the accuracy file
117 0 foreach my $set(@sets){
118 #set up the new folder
119 0 my $direct = "$program_dir/_MODELS/$weka_dir/$name" . "_MODEL_DATA/$set";
120 0 make_path($direct);
121  
122 #prep the output accuracy file and the test and train files
123 0 ▶ for(my $a = 1; $a <= $buckets; $a++){
Branch: 1 branch
BranchTrueFalseTotal
if ($a <= $buckets) 0 0 0
124 0 $| = 1;
125 0 $uniSub->printColorDebug("cyan", ("\r" . "$name - $set -- $a"));
126 0 my $TRAIN = "$program_dir/_ARFF/$name" . "_ARFF/$set/_train/$name" . "_train-$a.arff";
127 0 my $WEK = $direct . "/$name" . "_model_$a";
128  
129 #run weka-modelling and output
130 0 system "java $weka_size $classifier -C $C_val -t $TRAIN -d $direct";
131 }
132 0 $uniSub->printDebug("\n");
133 }
134 }
135  
136  
137  
138 1 1;
« lib/NanoB2B/NER/Wekaman.pm ↑ Summary lib/NanoB2B/NER/Metaman.pm »