Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
LDA/LDA.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
52 lines (39 sloc)
1.4 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
@Author: Arunav Mishra, Supratim Das | |
""" | |
import os | |
import gensim | |
from gensim import similarities | |
class LDA(object): | |
# Initialize | |
def __init__(self): | |
self.id2wordFile = '/Users/Supra/PycharmProjects/LDA/Input/OP/words.dict' | |
self.id2word = gensim.corpora.Dictionary.load(self.id2wordFile) | |
# Load the corpus files generated in the last step | |
self.corpusFile = '/Users/Supra/PycharmProjects/LDA/Input/OP/corpus.mm' | |
self.mm = gensim.corpora.MmCorpus(self.corpusFile) | |
# Run LDA | |
self.lda = gensim.models.ldamodel.LdaModel(corpus=self.mm, id2word=self.id2word, num_topics=100, update_every=0, | |
passes=20) | |
self.ldaFile = '/Users/Supra/PycharmProjects/LDA/Input/OP/lda' | |
self.dir_lda = os.path.dirname(self.ldaFile) | |
# Run LDA | |
def run(self): | |
try: | |
os.stat(self.dir_lda) | |
except: | |
os.mkdir(self.dir_lda) | |
self.lda.save(self.ldaFile) | |
index = similarities.MatrixSimilarity(self.lda[self.mm]) | |
dir = os.path.dirname(self.ldaFile) | |
try: | |
os.stat(dir) | |
except: | |
os.mkdir(dir) | |
self.lda.save(self.ldaFile) | |
ldaIndexFile = '/Users/Supra/PycharmProjects/LDA/Input/OP/lda.index' | |
index.save(ldaIndexFile) | |
def main(self): | |
self.run() | |
if __name__ == '__main__': | |
LDA().main() |