-
Notifications
You must be signed in to change notification settings - Fork 0
/
getWordBags.m
37 lines (35 loc) · 1.36 KB
/
getWordBags.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function t = getWordBags(data)
bodies = data.articleBody;
headlines = data.Headline;
bsize = size(bodies);
bodyCounts = cell(bsize(1), 1);
headWords = cell(bsize(1), 1);
totalWords = cell(bsize(1), 1);
bodyBags = cell(bsize(1), 1);
headBags = cell(bsize(1), 1);
for i = 1:bsize(1)
[wordCounts, numWords, bodyBag] = getWordBag(bodies{i});
[importantHeadline, headBag] = getImportantHeadline(headlines{i});
bodyBags{i} = bodyBag;
bodyCounts{i} = wordCounts;
totalWords{i} = numWords;
headWords{i} = importantHeadline;
headBags{i} = headBag;
end
stance = data.Stance;
t = table(bodyCounts, headWords, totalWords, stance, bodyBags, headBags);
end
function [wordCounts, numWords, bodyBag] = getWordBag(articleBody)
doc = tokenizedDocument(articleBody);
bodyBag = bagOfWords(doc);
bodyBag = removeWords(bodyBag, [stopWords,".","?","!",",",";",":"]);
fullCounts = full(bodyBag.Counts);
wordCounts = containers.Map(convertStringsToChars(bodyBag.Vocabulary), num2cell(fullCounts));
numWords = bodyBag.NumWords;
end
function [importantHeadline, headBag] = getImportantHeadline(origHead)
doc = tokenizedDocument(origHead);
headBag = bagOfWords(doc);
vocab = removeWords(doc, [stopWords,".","?","!",",",";",":"]);
importantHeadline = vocab.Vocabulary;
end