% addpath('liblinear-1.7/matlab');  % add LIBLINEAR to the path
addpath('liblinear-1.8'); % My path
[sparseTestMatrix, tokenlist, testCategory] = readMatrix('MATRIX.TEST');

numTestDocs = size(sparseTestMatrix, 1);
numTokens = size(sparseTestMatrix, 2);

% Assume liblinear_train.m has just been executed, and the model trained
% by your classifier is in memory through that execution. You can also assume 
% that the columns in the test set are arranged in exactly the same way as for the
% training set (i.e., the j-th column represents the same token in the test data 
% matrix as in the original training data matrix).

% Write code below to classify each document in the test set (ie, each row
% in the current document word matrix) as 1 for SPAM and 0 for NON-SPAM.

% Note that the predict function for LIBLINEAR uses the sparse matrix 
% representation of the document word  matrix, which is stored in sparseTestMatrix.
% Additionally, it expects the labels to be dimension (numTestDocs x 1).

% Construct the (numTestDocs x 1) vector 'output' such that the i-th entry 
% of this vector is the predicted class (1/0) for the i-th  email (i-th row 
% in testMatrix) in the test set.
% output = zeros(numTestDocs, 1);

%--------------------------------------------------
% Tony Hyun Kim
% CS 229, PS#2, Problem 3(d)
%   We're not doing much... just using the provided library
%--------------------------------------------------
testCategory = 2*full(testCategory)-1; % Change labels appropriately
output = predict(testCategory',sparseTestMatrix,model);

% Compute the error on the test set
error=0;
for i=1:numTestDocs
  if (testCategory(i) ~= output(i))
    error=error+1;
  end
end

%Print out the classification error on the test set
fprintf('The classification error rate is %.2f%%\n',error/numTestDocs*100);
