function sentimentScore = calculateSentiment(filename)

%% Import SEC filing data 10-K
str = extractFileText(filename);

%% Clean data
% Split up into different lines
cleanTextData = splitlines(str);
cleanTextData = eraseURLs(cleanTextData);
cleanTextData = lower(cleanTextData);

%% Identify a portion to dto run sentiment analysis on - Risk factor section
kstart = strfind(cleanTextData,lower("Item 1A. Risk Factors"));
kend = strfind(cleanTextData,lower("Item 1B. Unresolved Staff Comments"));

startidx = find(~cellfun('isempty',kstart));
endidx = find(~cellfun('isempty',kend));

cleanTextData = cleanTextData(startidx(2)+1:endidx(2)-1);

%% Continue cleaning data
cleanTextData = erasePunctuation(cleanTextData);

%% Visualize text data
% wordcloud(cleanTextData)
% T = wordCloudCounts(cleanTextData);

%% Tokenize
cleanDocuments = tokenizedDocument(cleanTextData);

%% More cleaning
cleanDocuments = removeWords(cleanDocuments, stopWords);
% cleanDocuments(1:10)
cleanDocuments = removeShortWords(cleanDocuments, 2);
cleanDocuments = removeLongWords(cleanDocuments,15);
% cleanDocuments = normalizeWords(cleanDocuments);
% cleanDocuments(1:10)
cleanDocuments = removeEmptyDocuments(cleanDocuments);

%% Import dictionary of words (+ve and -ve)
importDictionary
dictionary.Word = lower(dictionary.Word);
%% Compare each document/sentence to dictionary of words

[~,ib] = ismember(cleanDocuments.Vocabulary,dictionary.Word);
% Remove zeros from subscripts ib
ib(ib==0) = [];

%% Sentiment Scoring
subsetwords = dictionary(ib',{'Negative','Positive','Litigious'});

% Assign weights to -ve (-1), +ve(+1), litigious (-0.5)
subsetwords.Negative(subsetwords.Negative > 0) = -1;
subsetwords.Positive(subsetwords.Positive > 0) = 1;
subsetwords.Litigious(subsetwords.Litigious > 0) = -0.5;

% Normalize by number of words in the document? 

% Total sentiment
sentimentScore = sum(sum(table2array(subsetwords)));

end

