%% Relevance Versus Redundancy
% Create an array of input documents.

str = [
    "the quick brown fox jumped over the lazy dog"
    "the quick brown fox jumped over the lazy dog"
    "the fast fox jumped over the lazy dog"
    "the dog sat there and did nothing"
    "the other animals sat there watching"
    "the other animals sat there watching"];
documents = tokenizedDocument(str);

% Create a bag-of-words model from the input documents.

bag = bagOfWords(documents)

% Create an array of query documents.

str = [
    "a brown fox leaped over the lazy dog"
    "another fox leaped over the dog"];
queries = tokenizedDocument(str)

% Calculate the MMR scores. The output is a sparse matrix.

scores = mmrScores(bag,queries);

% Visualize the MMR scores in a heat map.

figure
heatmap(scores);
xlabel("Query Document")
ylabel("Input Document")
title("MMR Scores")

% Now calculate the scores again, and set the lambda value to 0.01. When the lambda value is close to 0, redundant documents yield lower scores and diverse (but less query-relevant) documents yield higher scores.

lambda = 0.01;
scores = mmrScores(bag,queries,lambda);

% Visualize the MMR scores in a heat map.

figure
heatmap(scores);
xlabel("Query Document")
ylabel("Input Document")
title("MMR Scores, lambda = " + lambda)

% Finally, calculate the scores again and set the lambda value to 1. When the lambda value is 1, the query-relevant documents yield higher scores despite other documents yielding high scores.

lambda = 1;
scores = mmrScores(bag,queries,lambda);

% Visualize the MMR scores in a heat map.

figure
heatmap(scores);
xlabel("Query Document")
ylabel("Input Document")
title("MMR Scores, lambda = " + lambda)

% Copyright 2019 The MathWorks, Inc.