In the previous post we created python script to get posts from WordPress (WP) blog through WP API. This script was saving retrieved posts into csv file. In this post we will create script for topic extraction from the posts saved in this csv file. We will use the following 2 techniques (LSI and LDA) for topic modeling:
1. Latent semantic indexing (LSI) is an indexing and retrieval method that uses a mathematical technique called singular value decomposition (SVD) to identify patterns in the relationships between the terms and concepts contained in an unstructured collection of text. LSI is based on the principle that words that are used in the same contexts tend to have similar meanings. A key feature of LSI is its ability to extract the conceptual content of a body of text by establishing associations between those terms that occur in similar contexts.
2. Latent Dirichlet allocation (LDA) is a generative statistical model that allows sets of observations to be explained by unobserved groups that explain why some parts of the data are similar. For example, if observations are words collected into documents, it posits that each document is a mixture of a small number of topics and that each word’s creation is attributable to one of the document’s topics. LDA is an example of a topic model. 
In one of the previous posts we looked how to use LDA with python.  So now we just applying this script to data in csv file with blog posts. Additionally we will use LSI method as alternative for topic modeling.
The script for LDA/LSA consists of the following parts:
1. As the first step the script is opens csv data file and load data to the memory. During this the script also performs some text preprocessing. As result of this we have the set of posts (documents).
2. The script iterates through set of posts and converts documents into tokens and saves all documents into texts. After iteration is completed the script builds dictionary and corpus.
3. In this step the script uses LSI model and LDA model.
4. Finally in the end for LDA method the script prints some information about topics, including document – topic information.
Comparing results of LSI and LDA methods it seems that LDA gives more understable topics.
Also LDA coefficients are all in range 0 – 1 as they indicate probabilities. This makes easy to explain results.
In our script we used LDA and LSI from gensim library, but there are another packages that allows to do LDA:
MALLET for example allows also to model a corpus of texts 
LDA – another python package for Latent Dirichlet Allocation 
There are also other techniques to do approximate topic modeling in Python. For example there is a technique called non-negative matrix factorization (NMF) that strongly resembles Latent Dirichlet Allocation  Also there is a probabilistic latent semantic analysis (PLSA), also known as probabilistic latent semantic indexing (PLSI) technique that evolved from LSA. 
In the future posts some of the above methods will be considered.
There is an interesting discussion on quora site how to run LDA and here you can find also some insights on how to prepare data and how to evaluate results of LDA. 
Here is the source code of script.
# -*- coding: utf-8 -*- import csv from stop_words import get_stop_words from nltk.stem.porter import PorterStemmer from gensim import corpora import gensim import re from nltk.tokenize import RegexpTokenizer M="LDA" def remove_html_tags(text): """Remove html tags from a string""" clean = re.compile('<.*?>') return re.sub(clean, '', text) tokenizer = RegexpTokenizer(r'\w+') # use English stop words list en_stop = get_stop_words('en') # use p_stemmer of class PorterStemmer p_stemmer = PorterStemmer() fn="posts.csv" doc_set =  with open(fn, encoding="utf8" ) as f: csv_f = csv.reader(f) for i, row in enumerate(csv_f): if i > 1 and len(row) > 1 : temp=remove_html_tags(row) temp = re.sub("[^a-zA-Z ]","", temp) doc_set.append(temp) texts =  for i in doc_set: print (i) # clean and tokenize document string raw = i.lower() raw=' '.join(word for word in raw.split() if len(word)>2) raw=raw.replace("nbsp", "") tokens = tokenizer.tokenize(raw) stopped_tokens = [i for i in tokens if not i in en_stop] stemmed_tokens = [p_stemmer.stem(i) for i in stopped_tokens] texts.append(stemmed_tokens) # turn our tokenized documents into a id <-> term dictionary dictionary = corpora.Dictionary(texts) # convert tokenized documents into a document-term matrix corpus = [dictionary.doc2bow(text) for text in texts] lsi = gensim.models.lsimodel.LsiModel(corpus, id2word=dictionary, num_topics=5 ) print (lsi.print_topics(num_topics=3, num_words=3)) for i in lsi.show_topics(num_words=4): print (i, i) if M=="LDA": ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics=5, id2word = dictionary, passes=20) print (ldamodel) print(ldamodel.print_topics(num_topics=3, num_words=3)) for i in ldamodel.show_topics(num_words=4): print (i, i) # Get Per-topic word probability matrix: K = ldamodel.num_topics topicWordProbMat = ldamodel.print_topics(K) print (topicWordProbMat) for t in texts: vec = dictionary.doc2bow(t) print (ldamodel[vec])
3. Topic modeling in Python
4. Topic modeling with MALLET
5. Getting started with Latent Dirichlet Allocation in Python
6. What are good ways of evaluating the topics generated by running LDA on a corpus?
7. Working with text
8. Latent Dirichlet Allocation (LDA) with Python Script
9. Probabilistic latent semantic analysis