#!/usr/bin/perl

use strict;

if (@ARGV < 4) { # print a warning if we didn't get the right number of features
	print "Correct syntax is:\n\tanalyze_model.pl model_name feature_ids author_ids top_feature_count\n";
	exit;
}

my $model_file = $ARGV[0]; # fetch name of model file
my $feature_id_file = $ARGV[1]; # fetch name of feature id file
my $author_id_file = $ARGV[2]; # fetch name of author id file
my $feature_count = $ARGV[3]; # fetch number of highest-weighted-features to display for each model

open (FEATURES, $feature_id_file); # open the feature id file
my @feature_list = <FEATURES>; # slurp the feature file into memory
close (FEATURES); # we're done w/ the feature file, so close it

my %features; # defined here so it doesn't yell later

foreach my $feature (@feature_list) {
	# work on each entry in the feature set
	my @feature_name = split(/\s+/, $feature); # split feature id from feature number
	$features{$feature_name[0]} = $feature_name[1]; # load the feature name/id into a hash
}

@feature_list = (); # delete the contents of the list, since we're done with it

open (AUTHORS, $author_id_file); # open the author id file
my @author_list = <AUTHORS>; # slurp the author file into memory
close (AUTHORS); # we're done w/ the author file, so close it

my %authors; # defined here so it doesn't yell later

foreach my $author (@author_list) {
	# work on each entry in the author set
	my @author_name = split(/\W+/, $author); # split feature id from feature number
	$authors{$author_name[0]} = $author_name[1]; # load the feature name/id into a hash
}

@author_list = (); # delete the contents of the list, since we're done with it

open (MODEL, $model_file); # open the model file
my @model = <MODEL>; # slurp the model file into memory
close (MODEL); # we're done w/ the model file, so close it

my @beta_values = grep(/^betaClassSparse/, @model); # find the lines with feature values

@model = (); # clean out the model file variable, since we're done with it

foreach my $class (@beta_values) {
	# work on each class in the model
	my @feature_pairs = split(/ /, $class); # break the class model into feature pairs
	shift(@feature_pairs); # the first entry in the list is always 'betaClassSparse', so toss it
	my $class_number = shift(@feature_pairs); # item two is the class number. this is handy, so retain it
	
	my %weights_by_feature = (); # clean out the weight hash
	
	foreach my $feature_pair (@feature_pairs) {
		# let's work on the feature pairs
		my @pair = split (/:/, $feature_pair);
		$weights_by_feature{$pair[0]} = $pair[1];
	}
	
	my %features_by_weight = reverse %weights_by_feature; # we want to work by weight, not feature id
	
	my @sorted_weights = sort {$b <=> $a} keys %features_by_weight; # get the feature weights sorted from highest to lowest
	
	print "Author $class_number, $authors{$class_number}, top $feature_count features:\n\n"; # print the first part of the report
	print "Weight\tFeature\n";
	print "------\t-------\n";
	
	for(my $i = 0; $i < $feature_count; $i++) {
		# work on the first $feature_count weights in the list
		printf("%6u\t%s\n", $sorted_weights[$i] * 1000, $features{$features_by_weight{$sorted_weights[$i]}});
		#print "$sorted_weights[$i]\t$features{$features_by_weight{$sorted_weights[$i]}}\n";
	}
	
	print "\n\n";
}