#!/usr/bin/perl

use Cwd 'abs_path';
use strict;

# this script automates construction and testing of test sets for the BMR software.
# we assume that we will be creating a trio of folders: one for the wordPOS feature set, one for POS only, one for word only

# figure out where our scripts and software live
print "Enter the path to the software repository [.]: ";
my $software_path = <STDIN>;
chomp ($software_path);
if (!$software_path) { $software_path = "."; }; # do a check for defaults
$software_path = abs_path($software_path);
print "\tUsing software repository location $software_path\n";

# start by figuring out what to use as our base folder name
print "Enter the folder basename: ";
my $folder_name = <STDIN>;
chomp ($folder_name);

# get parameters for the set builder
print "Enter the corpus name [Compass]: ";
my $corpus_name = <STDIN>;
chomp ($corpus_name);
if (!$corpus_name) { $corpus_name = "Compass"; }; # do a check for defaults
print "Enter the training ratio [.75]: ";
my $training_ratio = <STDIN>;
chomp ($training_ratio);
if (!$training_ratio) { $training_ratio = .75; }; # do a check for defaults
print "Enter the list of input files (space delimited): ";
my $input_files = <STDIN>;
chomp ($input_files);

# get prefix for classifer
print "Enter prefix for classifier output: ";
my $output_prefix = <STDIN>;
chomp ($output_prefix);

# get highest weight values
print "Enter number of highest-weighted features to return [20]: ";
my $top_feature_count = <STDIN>;
chomp ($top_feature_count);
if (!$top_feature_count) { $top_feature_count = 20; }; # do a check for defaults

print "\nSwitching to automated run mode. No further input required.\n";

mkdir $folder_name; # create our base folder
mkdir $folder_name . "_tagonly"; # create a folder for the POS only feature set
mkdir $folder_name . "_wordsonly"; # create a folder for the word only feature set
chdir $folder_name; # change to the base folder

# run the set builder
print "\nBuilding training/test sets, directing output to tt_split\n";
system("$software_path/build_sets.pl $corpus_name $training_ratio $input_files > tt_split");

print "\tTraining/test split assembled. Copying to other directories\n";

# copy the files we just produced into the other feature set directories
system("cp * ../$folder_name" . "_tagonly");
system("cp * ../$folder_name" . "_wordsonly");

# run the feature extractor on the wordPOS dataset
print "\nRunning feature extractor for wordPOS feature set\n";
system("$software_path/featex13.pl $corpus_name.training $corpus_name.testing");

# run the trainer
print "\tRunning trainer - directing output to bmrt_output\n";
system("$software_path/BMRtrain $corpus_name.training.bmr $corpus_name.model > bmrt_output");

# run the classifier
print "\tRunning classifer\n";
system("$software_path/BMRclassify $corpus_name.testing.bmr $corpus_name.model > $output_prefix" ."_classification");

# analyze the model
print "\tAnalyzing model\n";
system("$software_path/analyze_model.pl $corpus_name.model features.ids authors.ids $top_feature_count > $output_prefix"."_model_data");

# switch to the tags only directory
chdir "../$folder_name" . "_tagonly";

# run the feature extractor on the POS only dataset
print "\nRunning feature extractor for POS only feature set\n";
system("$software_path/featex13.pl -tokenize LENTagsOnly $corpus_name.training $corpus_name.testing");

# run the trainer
print "\tRunning trainer - directing output to bmrt_output\n";
system("$software_path/BMRtrain $corpus_name.training.bmr $corpus_name.model > bmrt_output");

# run the classifier
print "\tRunning classifer\n";
system("$software_path/BMRclassify $corpus_name.testing.bmr $corpus_name.model > $output_prefix" ."_classification.to");

# analyze the model
print "\tAnalyzing model\n";
system("$software_path/analyze_model.pl $corpus_name.model features.ids authors.ids $top_feature_count > $output_prefix"."_model_data.to");

# switch to the words only directory
chdir "../$folder_name" . "_wordsonly";

# run the feature extractor on the words only dataset
print "\nRunning feature extractor for words only feature set\n";
system("$software_path/featex13.pl -tokenize LENTagWordsOnly $corpus_name.training $corpus_name.testing");

# run the trainer
print "\tRunning trainer - directing output to bmrt_output\n";
system("$software_path/BMRtrain $corpus_name.training.bmr $corpus_name.model > bmrt_output");

# run the classifier
print "\tRunning classifer\n";
system("$software_path/BMRclassify $corpus_name.testing.bmr $corpus_name.model > $output_prefix" ."_classification.wo");

# analyze the model
print "\tAnalyzing model\n";
system("$software_path/analyze_model.pl $corpus_name.model features.ids authors.ids $top_feature_count > $output_prefix"."_model_data.wo");