Skip to content

Instantly share code, notes, and snippets.

@y-tag
Created March 23, 2013 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-tag/5229576 to your computer and use it in GitHub Desktop.
Save y-tag/5229576 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/perl
use strict;
use warnings;
use 5.008;
main();
sub main {
if (@ARGV < 1) {
print STDERR "Usage: $0 out_dir\n";
exit 1;
}
my $out_dir = shift @ARGV;
my @C_array = map {2 ** $_} (-4..4);
my @phi_array = qw(0.000000 0.125661 0.253347 0.385320 0.524401 0.674490 0.841621 1.036433 1.281552 1.644854);
my $param_and_method = "";
system("mkdir -p $out_dir") unless -d $out_dir;
# perceptron
$param_and_method = ' "method" : "perceptron"';
generate_conf("$out_dir/perceptron.json", $param_and_method);
# PA
$param_and_method = ' "method" : "PA"';
generate_conf("$out_dir/pa.json", $param_and_method);
my $i;
# PA1
$i = 0;
foreach my $C (@C_array) {
$param_and_method = ' "parameter" : {' . "\n";
$param_and_method .= ' "regularization_weight" : ' . $C . "\n";
$param_and_method .= ' },' . "\n";
$param_and_method .= ' "method" : "PA1"';
my $n = sprintf("%02d", $i);
generate_conf("$out_dir/pa1_$n.json", $param_and_method);
$i += 1;
}
# PA2
$i = 0;
foreach my $C (@C_array) {
$param_and_method = ' "parameter" : {' . "\n";
$param_and_method .= ' "regularization_weight" : ' . $C . "\n";
$param_and_method .= ' },' . "\n";
$param_and_method .= ' "method" : "PA2"';
my $n = sprintf("%02d", $i);
generate_conf("$out_dir/pa2_$n.json", $param_and_method);
$i += 1;
}
# CW
$i = 0;
foreach my $phi (@phi_array) {
$param_and_method = ' "parameter" : {' . "\n";
$param_and_method .= ' "regularization_weight" : ' . $phi . "\n";
$param_and_method .= ' },' . "\n";
$param_and_method .= ' "method" : "CW"';
my $n = sprintf("%02d", $i);
generate_conf("$out_dir/cw_$n.json", $param_and_method);
$i += 1;
}
# AROW
$i = 0;
foreach my $C (@C_array) {
$param_and_method = ' "parameter" : {' . "\n";
$param_and_method .= ' "regularization_weight" : ' . $C . "\n";
$param_and_method .= ' },' . "\n";
$param_and_method .= ' "method" : "AROW"';
my $n = sprintf("%02d", $i);
generate_conf("$out_dir/arow_$n.json", $param_and_method);
$i += 1;
}
# NHERD
$i = 0;
foreach my $C (@C_array) {
$param_and_method = ' "parameter" : {' . "\n";
$param_and_method .= ' "regularization_weight" : ' . $C . "\n";
$param_and_method .= ' },' . "\n";
$param_and_method .= ' "method" : "NHERD"';
my $n = sprintf("%02d", $i);
generate_conf("$out_dir/nherd_$n.json", $param_and_method);
$i += 1;
}
# SCW1
$i = 0;
foreach my $phi (@phi_array) {
foreach my $C (@C_array) {
$param_and_method = ' "parameter" : {' . "\n";
$param_and_method .= ' "regularization_weight" : ' . $phi . ",\n";
$param_and_method .= ' "penalty_weight" : ' . $C . "\n";
$param_and_method .= ' },' . "\n";
$param_and_method .= ' "method" : "SCW1"';
my $n = sprintf("%02d", $i);
generate_conf("$out_dir/scw1_$n.json", $param_and_method);
$i += 1;
}
}
# SCW2
$i = 0;
foreach my $phi (@phi_array) {
foreach my $C (@C_array) {
$param_and_method = ' "parameter" : {' . "\n";
$param_and_method .= ' "regularization_weight" : ' . $phi . ",\n";
$param_and_method .= ' "penalty_weight" : ' . $C . "\n";
$param_and_method .= ' },' . "\n";
$param_and_method .= ' "method" : "SCW2"';
my $n = sprintf("%02d", $i);
generate_conf("$out_dir/scw2_$n.json", $param_and_method);
$i += 1;
}
}
}
sub generate_conf {
my ($out_f, $param_and_method) = @_;
my $doc = <<"EOD";
{
"converter" : {
"string_filter_types" : {},
"string_filter_rules" : [],
"num_filter_types" : {},
"num_filter_rules" : [],
"string_types" : {},
"string_rules" : [
{ "key" : "*", "type" : "str", "sample_weight" : "bin", "global_weight" : "bin" }
],
"num_types" : {},
"num_rules" : [
{ "key" : "*", "type" : "num" }
]
},
$param_and_method
}
EOD
open(my $ofh, '>', $out_f) or die $!;
print {$ofh} $doc;
close($ofh);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment