-
Notifications
You must be signed in to change notification settings - Fork 3
/
feedfilter.m
executable file
·85 lines (77 loc) · 2.92 KB
/
feedfilter.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function feedfilter(classifier,url)
% Accept 'classifier' object and Google Blog Search URL or equivalent and
% display parsed text for interactive training.
if nargin <2
url='python_search.xml';
end
% Parse XML into a cell array 'items'
% This doesn't work with all RSS feeds.
f=xmlread(url);
allitems=f.getElementsByTagName('item');
entries=cell(allitems.getLength,3);
for i=0:size(entries,1)-1
thisitem=allitems.item(i);
childNode=thisitem.getFirstChild;
while ~isempty(childNode)
if childNode.getNodeType==childNode.ELEMENT_NODE
childText=char(childNode.getFirstChild.getData);
switch char(childNode.getTagName)
case 'title';
entries{i+1,1}=childText;
case 'dc:publisher';
entries{i+1,2}=childText;
case 'description';
entries{i+1,3}=childText;
end
end
childNode=childNode.getNextSibling;
end
end
% Start the interactive session
disp(' ')
disp(sprintf('Start interactive training: %d entries', size(entries,1)))
for i=1:size(entries,1)
disp(' ')
disp('-----')
% Print the content of the entry
disp(sprintf('Entry # %d',i))
disp(sprintf('Title: %s', entries{i,1}))
disp(sprintf('Publisher: %s', entries{i,2}))
disp(' ')
disp(entries{i,3})
if strcmp(char(classifier.getfeatures),'entryfeatures')
fulltext=entries(i,:);
else
% Combine all the text to create one item for the classifier
fulltext=sprintf('%s\n%s\n%s', entries{i,1},entries{i,2},entries{i,3});
end
% Print the best guess at the current category
guess=classifier.classify(fulltext);
disp(['Guess: ' guess]);
% Ask the user to specify the correct category
if ~isempty(guess)
% Ask to confirm the quess or enter new category
prompt=sprintf('Confirm category? [%s] or enter new category: ',guess);
else
% Ask to enter new category
prompt='Etner category: ';
end
user_entry = input(prompt,'s');
% User data exits
if ~isempty(user_entry)
% If it is not 'end' use the user data for training
if ~strcmp(user_entry,'end')
classifier.train(fulltext,user_entry);
else % If it is 'end', end the interactive session
break;
end
else % If user data doesn't exit, user accepted the guess
if ~isempty(guess)
classifier.train(fulltext,guess);
end
end
end
disp(' ')
disp('End of interactive training')
disp('Thank you for your help.')
disp(' ')