-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_topic_tsne.m
executable file
·184 lines (158 loc) · 5.08 KB
/
sub_topic_tsne.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
%% running standard nmf of subset
function [mappedX_sub, cl_idx_sub, Wtopk_idx_sub] = sub_topic_tsne(idx)
addpath('./library/nmf');
addpath('./library/ramkis');
addpath('./library/peripheral');
addpath('./library/discnmf');
addpath('./library/tSNE_matlab');
%% importing term-doc matrix
<<<<<<< HEAD
% in_txt_dir = 'text_files';
% out_fname = 'out.txt';
%
% is_stemming = 1;
%
% % merge multiple text files in a particular directory to a single file
% %[raw_txt,fnames] = merge_to_single_text(in_txt_dir,out_fname);
%
% if ~exist('tmp1','dir')
% mkdir('tmp1');
% end
%
% delete(['tmp1' filesep '*.*']);
%
% % run python function to generate a term-document matrix
% load('vis_paper_ver201508.mat');
% fid = fopen('out.txt', 'w');
% for i=1:size(PaperTitle,1)
% fprintf(fid, '%s %s\n', PaperTitle{i}, Abstract{i});
% end
% fclose(fid);
% dos(sprintf('python txt2mtx_fast.py %s',out_fname));
%
% % importing dictionary and term-document matrix
% dict = import_dictionary(['tmp1' filesep 'vocabulary.txt']);
% tdm = import_tdm(['tmp1' filesep 'tmp.mtx']);
% A = sparse(tdm(:,1),tdm(:,2),tdm(:,3),max(tdm(:,1)),max(tdm(:,2)));
% clear tdm;
%
% if is_stemming
% tmp = cell(size(dict));
% tmp{i}=cell(size(dict));
% for i=1:length(dict)
% tmp{i} = porterStemmer(dict{i});
% end
% [dict_stemmed,ia,ic] = unique(tmp);
% A_stemmed = sparse([],[],[],length(dict_stemmed),size(A,2));
% A_stemmed = A_stemmed';
% A_transpose = A';
% for i=1:length(dict_stemmed)
% A_stemmed(:,i) = sum(A_transpose(:,ic==i),2);
% end
% A_stemmed = A_stemmed';
%
% A = A_stemmed;
% dict = dict_stemmed;
% clear A_transpose A_stemmed dict_stemmed;
% end
%
% % [A w] = tfidf2(A);
%
% %% additional stopwords
% addl_stopwords = {'visualization','visual','information','analysis','analysi','data','approach','process','based','technique','techniques','paper'};
%
% idxs =[];
%
% for i=1:length(addl_stopwords)
% idxs = [idxs find(strcmp(dict,addl_stopwords{i}))];
% end
%
% idxs = setdiff(1:size(A,1),idxs);
% A = A(idxs,:);
% dict = dict(idxs);
%% sub_A set
load tdm;
=======
%in_txt_dir = 'text_files';
out_fname = 'out.txt';
is_stemming = 1;
% merge multiple text files in a particular directory to a single file
[raw_txt,fnames] = merge_to_single_text(in_txt_dir,out_fname);
if ~exist('tmp1','dir')
mkdir('tmp1');
end
delete(['tmp1' filesep '*.*']);
% run python function to generate a term-document matrix
load('vis_paper_ver201508.mat');
fid = fopen('out.txt', 'w');
for i=1:size(PaperTitle,1)
fprintf(fid, '%s %s\n', PaperTitle{i}, Abstract{i});
end
fclose(fid);
dos(sprintf('python txt2mtx_fast.py %s',out_fname));
% importing dictionary and term-document matrix
dict = import_dictionary(['tmp1' filesep 'vocabulary.txt']);
tdm = import_tdm(['tmp1' filesep 'tmp.mtx']);
A = sparse(tdm(:,1),tdm(:,2),tdm(:,3),max(tdm(:,1)),max(tdm(:,2)));
clear tdm;
if is_stemming
tmp = cell(size(dict));
for i=1:length(dict)
tmp{i} = porterStemmer(dict{i});
end
[dict_stemmed,ia,ic] = unique(tmp);
A_stemmed = sparse([],[],[],length(dict_stemmed),size(A,2));
A_stemmed = A_stemmed';
A_transpose = A';
for i=1:length(dict_stemmed)
A_stemmed(:,i) = sum(A_transpose(:,ic==i),2);
end
A_stemmed = A_stemmed';
A = A_stemmed;
dict = dict_stemmed;
clear A_transpose A_stemmed dict_stemmed;
end
% [A w] = tfidf2(A);
%% additional stopwords
addl_stopwords = {'visualization','visual','information','analysis','analysi','data','approach','process','based','technique','techniques','paper'};
idxs =[];
for i=1:length(addl_stopwords)
idxs = [idxs find(strcmp(dict,addl_stopwords{i}))];
end
idxs = setdiff(1:size(A,1),idxs);
A = A(idxs,:);
dict = dict(idxs);
>>>>>>> 6ccfe69e276e8719933d5ff429b6b035337a0458
idx;
% idx = cell2mat(idx);
A_sub = A(:,idx);
% no of topics
k_sub = min([floor(length(idx)/10) 10]);
% no of top keywords
topk_sub = 5;
% normalization
A_norm_sub = bsxfun(@rdivide,A_sub,sqrt(sum(A_sub.^2)));
% choosing one among different preprocessings
target_A_sub = A_norm_sub; % replaced by below code (9/10) <- original
% target_A = A_norm;
%%
% target_A = A_idf;
size(target_A_sub)
[tree_sub, splits_sub, is_leaf_sub, clusters_sub, timings_sub, Ws_sub, priorities_sub, W_sub, H_sub] = hier8_neat(target_A_sub, k_sub);
%%
% displaying top keywords for each topic
[Wtopk_sub,Htopk_sub,DocTopk_sub,Wtopk_idx_sub] = parsenmf(W_sub,H_sub,dict,topk_sub);
Wtopk_idx_sub = Wtopk_idx_sub';
[~,cl_idx_sub] = max(H_sub);
Wlen=size(Wtopk_sub,2);
%% t-sne visualization
% Run t-SNE
no_dims=2;
initial_dims_sub=min([30, size(A_sub,2)]);
shrink_factor = .3;
perplexity = 30;
mappedX_sub = tsne_sup(target_A_sub', cl_idx_sub, shrink_factor, no_dims, initial_dims_sub, perplexity);
% Run t?SNE
% mappedX = tsne(target_A', cl_idx, no_dims, initial_dims, perplexity);
%mappedX_sub = tsne(target_A_sub', cl_idx_sub, no_dims, initial_dims, perplexity);
end