-
Notifications
You must be signed in to change notification settings - Fork 102
/
printtopics.py
45 lines (40 loc) · 1.63 KB
/
printtopics.py
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
# printtopics.py: Prints the words that are most prominent in a set of
# topics.
#
# Copyright (C) 2010 Matthew D. Hoffman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, os, re, random, math, urllib2, time, cPickle
import numpy
import onlineldavb
def main():
"""
Displays topics fit by onlineldavb.py. The first column gives the
(expected) most prominent words in the topics, the second column
gives their (expected) relative prominence.
"""
vocab = str.split(file(sys.argv[1]).read())
testlambda = numpy.loadtxt(sys.argv[2])
for k in range(0, len(testlambda)):
lambdak = list(testlambda[k, :])
lambdak = lambdak / sum(lambdak)
temp = zip(lambdak, range(0, len(lambdak)))
temp = sorted(temp, key = lambda x: x[0], reverse=True)
print 'topic %d:' % (k)
# feel free to change the "53" here to whatever fits your screen nicely.
for i in range(0, 53):
print '%20s \t---\t %.4f' % (vocab[temp[i][1]], temp[i][0])
print
if __name__ == '__main__':
main()