Take the 2-minute tour ×

Suppose I have a corpus of documents and I run LSA algorithm on it. How can I use the final matrix obtained after applying SVD to semantically cluster all the words appearing in my corpus of documents? Wikipedia says LSA can be used to find relation between terms. Is there any library available in Python which can help me accomplish my task of semantically clustering words based on LSA?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Try gensim (http://radimrehurek.com/gensim/index.html), simply install it following these instruction: http://radimrehurek.com/gensim/install.html

then here a code sample:

from gensim import corpora, models, similarities

documents = ["Human machine interface for lab abc computer applications",
             "The EPS user interface management system",
             "System and human system engineering testing of EPS",
             "Relation of user perceived response time to error measurement",
             "The generation of random binary unordered trees",
             "The intersection graph of paths in trees",
             "Graph minors A survey"]

# remove common words and tokenize
texts = [[word for word in document.lower().split() if word not in stoplist]
         for document in documents]

# remove words that appear only once
all_tokens = sum(texts, [])
tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1)

texts = [[word for word in text if word not in tokens_once] for text in texts]

dictionary = corpora.Dictionary(texts)
corp = [dictionary.doc2bow(text) for text in texts]

# extract 400 LSI topics; use the default one-pass algorithm
lsi = gensim.models.lsimodel.LsiModel(corpus=corp, id2word=dictionary, num_topics=400)

# print the most contributing words (both positively and negatively) for each of the first ten topics
share|improve this answer

Your Answer


