-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
111 lines (85 loc) · 2.24 KB
/
utils.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
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
import sys, csv
from nltk.tokenize import wordpunct_tokenize
from scipy.special import psi
import numpy as n
import matplotlib.pyplot as plt
LISTOFDOCS = "alldocs.txt"
filenames = []
def get_filenames(filename):
print "getting filenames"
if filename == None:
filename = LISTOFDOCS
with open(filename, 'r') as f:
docs = f.readlines()
for doc in docs:
# print str(doc).split("\n")
filenames.append(str(doc).split("\n")[0])
return filenames
def getfiles(filename):
# print "getting file " + filename
f = open(filename, 'r')
doc = f.read()
# print doc
return doc
def getalldocs(filename = None):
files = get_filenames(filename)
docs = []
for file in files:
doc = getfiles(file)
# print doc
docs.append(doc)
# print docs
return docs
def dirichlet_expectation(alpha):
'''see onlineldavb.py by Blei et al'''
if (len(alpha.shape) == 1):
return (psi(alpha) - psi(n.sum(alpha)))
return (psi(alpha) - psi(n.sum(alpha, 1))[:, n.newaxis])
def beta_expectation(a, b, k):
mysum = psi(a + b)
Elog_a = psi(a) - mysum
Elog_b = psi(b) - mysum
Elog_beta = n.zeros(k)
Elog_beta[0] = Elog_a[0]
# print Elog_beta
for i in range(1, k):
Elog_beta[i] = Elog_a[i] + n.sum(Elog_b[0:i])
# print Elog_beta
# print Elog_beta
return Elog_beta
def parseDocument(doc, vocab):
wordslist = list()
countslist = list()
doc = doc.lower()
tokens = wordpunct_tokenize(doc)
dictionary = dict()
for word in tokens:
if word in vocab:
wordtk = vocab[word]
if wordtk not in dictionary:
dictionary[wordtk] = 1
else:
dictionary[wordtk] += 1
wordslist.append(dictionary.keys())
countslist.append(dictionary.values())
return (wordslist[0], countslist[0])
def getVocab(file):
'''getting vocab dictionary from a csv file (nostopwords)'''
vocab = dict()
with open(file, 'r') as infile:
reader = csv.reader(infile)
for index, row in enumerate(reader):
vocab[row[0]] = index
return vocab
def plottrace(x, Y, K, n, perp):
for i in range(K):
plt.plot(x, Y[i], label = "Topic %i" %(i+1))
plt.xlabel("Number of Iterations")
plt.ylabel("Probability of Each topic")
plt.legend()
plt.title("Trace plot for topic probabilities")
plt.savefig("temp/plot_%i_%i_%f.png" %(K, n, perp))
#
#
# def calcPerplexity(test_docs):
# getalldocs()