-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (77 loc) · 3.35 KB
/
main.py
File metadata and controls
90 lines (77 loc) · 3.35 KB
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
import os
import glob
import sys
from operator import itemgetter # for sort
import cPickle
import plsa
STOP_WORDS_SET = set()
def print_topic_word_distribution(corpus, number_of_topics, topk, filepath):
"""
Print topic-word distribution to file and list @topk most probable words for each topic
"""
print "Writing topic-word distribution to file: " + filepath
V = len(corpus.vocabulary) # size of vocabulary
assert(topk < V)
f = open(filepath, "w")
for k in range(number_of_topics):
word_prob = corpus.topic_word_prob[k, :]
word_index_prob = []
for i in range(V):
word_index_prob.append([i, word_prob[i]])
word_index_prob = sorted(word_index_prob, key=itemgetter(1), reverse=True) # sort by word count
f.write("Topic #" + str(k) + ":\n")
for i in range(topk):
index = word_index_prob[i][0]
f.write(corpus.vocabulary[index] + " ")
f.write("\n")
f.close()
def print_document_topic_distribution(corpus, number_of_topics, topk, filepath):
"""
Print document-topic distribution to file and list @topk most probable topics for each document
"""
print "Writing document-topic distribution to file: " + filepath
assert(topk <= number_of_topics)
f = open(filepath, "w")
D = len(corpus.documents) # number of documents
for d in range(D):
topic_prob = corpus.document_topic_prob[d, :]
topic_index_prob = []
for i in range(number_of_topics):
topic_index_prob.append([i, topic_prob[i]])
topic_index_prob = sorted(topic_index_prob, key=itemgetter(1), reverse=True)
f.write("Document #" + str(d) + ":\n")
for i in range(topk):
index = topic_index_prob[i][0]
f.write("topic" + str(index) + " ")
f.write("\n")
f.close()
def main(argv):
print "Usage: python ./main.py <number_of_topics> <maxiteration>"
# load stop words list from file
stopwordsfile = open("stopwords.txt", "r")
for word in stopwordsfile: # a stop word in each line
word = word.replace("\n", '')
word = word.replace("\r\n", '')
STOP_WORDS_SET.add(word)
corpus = plsa.Corpus() # instantiate corpus
# iterate over the files in the directory.
document_paths = ['./texts/grimm_fairy_tales', './texts/tech_blog_posts', './texts/nyt']
#document_paths = ['./test/']
for document_path in document_paths:
for document_file in glob.glob(os.path.join(document_path, '*.txt')):
document = plsa.Document(document_file) # instantiate document
document.split(STOP_WORDS_SET) # tokenize
corpus.add_document(document) # push onto corpus documents list
corpus.build_vocabulary()
print "Vocabulary size:" + str(len(corpus.vocabulary))
print "Number of documents:" + str(len(corpus.documents))
number_of_topics = int(argv[1])
max_iterations = int(argv[2])
corpus.plsa(number_of_topics, max_iterations)
#print corpus.document_topic_prob
#print corpus.topic_word_prob
#cPickle.dump(corpus, open('./models/corpus.pickle', 'w'))
print_topic_word_distribution(corpus, number_of_topics, 20, "./topic-word.txt")
print_document_topic_distribution(corpus, number_of_topics, 10, "./document-topic.txt")
if __name__ == "__main__":
main(sys.argv)