function out = sheet08_solution

data % load data prepared by sheet08.py

% compute word counts, tf-idf, and term frequencies
word_counts = word_counts;
tf = tf(word_counts);
tfidf = tf_idf(word_counts, doc_counts);

% plot similarities (scalar products) for all three
% features
figure(1)
S = similarities(word_counts);
plot_sim(S, doc_class);
title('similarities using the raw word_counts');

figure(2)
S = similarities(tf);
plot_sim(S, doc_class);
title('similarities using the term-frequencies');

figure(3)
S = similarities(tfidf);
plot_sim(S, doc_class);
title('similarities using the tf-idfs');
caxis([0, 1])

% collect word counts to compare classes
wc1 = collect_word_counts(word_counts, doc_class == 1);
wc2 = collect_word_counts(word_counts, doc_class == -1);

% first, we take term-frequencies
wc1 = tf(wc1);
wc2 = tf(wc2);

fprintf('Top 20 words for positive and negative class using\n');
fprintf('term frequencies\n');

fprintf('Top 20 positive words:\n');
show_top_words(20, wc1, words)
fprintf('Top 20 negative words:\n');
show_top_words(20, wc2, words)

% now, we weight the term frequencies by the invers document
% frequency
tfidf1 = tf_idf(wc1, doc_counts);
tfidf2 = tf_idf(wc2, doc_counts);

fprintf('Top 20 words for positive and negative class using\n');
fprintf('tf-idf scores\n');

fprintf('Top 20 positive words:\n');
show_top_words(20, tfidf1, words)
fprintf('Top 20 negative words:\n');
show_top_words(20, tfidf2, words)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Your solution below!
%

% 1. compute term frequencies from word counts
function word_counts = tf(word_counts)
% ...

% 2. compute the TF-IDF statistic
function score = tf_idf(word_counts, doc_counts)
% ...

% 3. compute linear similarities (scalar products between
% all *rows* of matrix feats.
function S = similarities(feats)
% ...

% 4. plot similarities. Also plott boxes around the classes. You can
% assume that the class where doc_class == 1 come first.
function plot_sim(S, doc_class)
% ...

% 5. print the top n entries in each *row* of feats. Use the words
% cell-array to print the real names.
function show_top_words(n, feats, words)
% ...

% 6. from word_counts, sum rows in index I
function wc = collect_word_counts(word_counts, I)
% ...
