Topic Modeling

Topic Modeling

Topic Modeling

A step-by-step guide to discovering underlying topics in a collection of documents using Latent Dirichlet Allocation (LDA).

Libraries Used

This pipeline uses a few key Python libraries. Here is a quick overview of their roles:

  • gensim: A library for topic modeling and vector space modeling. It provides the implementation of LDA.
  • nltk: For text preprocessing tasks like stop word removal and lemmatization.
  • re: For regular expression-based text cleaning.

1. Data Loading and Preprocessing

  • What the code does: It takes a collection of documents, cleans the text, removes stop words and punctuation, and then lemmatizes the words (reduces them to their root form).
  • Behind the scenes: Preprocessing is critical for topic modeling. By cleaning the text and lemmatizing, we ensure that words like "running," "ran," and "runs" are all treated as the same word, "run," which helps the model find more coherent topics.
  • Abstraction: `nltk.corpus.stopwords` and `nltk.stem.WordNetLemmatizer` abstract the complex and labor-intensive process of cleaning text and normalizing words.
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from gensim.corpora import Dictionary
from gensim.models import LdaModel
import warnings
warnings.filterwarnings('ignore') # Suppress warnings for cleaner output

# Download necessary NLTK data
try:
    nltk.data.find('corpora/stopwords')
except nltk.downloader.DownloadError:
    nltk.download('stopwords')
try:
    nltk.data.find('corpora/wordnet')
except nltk.downloader.DownloadError:
    nltk.download('wordnet')

# Sample documents
documents = [
    "The new car is a marvel of engineering. It has a powerful engine and great fuel efficiency. The design is sleek and modern.",
    "Stock prices have been volatile this week. Investors are concerned about the economy. The market reacted negatively to the news.",
    "Scientists discovered a new planet in a distant galaxy. The telescope captured incredible images of the star system. Space exploration is expanding.",
    "A new movie was released starring a famous actor. The plot was captivating and the cinematography was beautiful. The film is a must-watch.",
    "The latest financial report shows strong growth. The company is expecting record profits this quarter. Shareholders are optimistic about the future.",
    "The rocket launched successfully into space, carrying a satellite. The mission is a huge step forward for space technology. NASA is leading the project."
]

# Preprocessing function
lemmatizer = WordNetLemmatizer()
stop_words = set(stopwords.words('english'))

def preprocess_text(doc):
    # Remove special characters and numbers
    doc = re.sub(r'[^a-zA-Z\s]', '', doc, re.I|re.A)
    doc = doc.lower().strip()
    words = doc.split()
    # Remove stop words and lemmatize
    words = [lemmatizer.lemmatize(word) for word in words if word not in stop_words]
    return words

processed_docs = [preprocess_text(doc) for doc in documents]
print("Processed document example:", processed_docs[0])

2. Building the LDA Model

  • What the code does: It creates a `Dictionary` (a mapping of words to their IDs) and a `Corpus` (a bag-of-words representation of the documents) which are the required inputs for the `LdaModel`.
  • Behind the scenes: LDA is a probabilistic model that treats each document as a mixture of various topics, and each topic as a mixture of various words. The dictionary and corpus provide the model with the necessary data structure to infer these topics.
  • Abstraction: `gensim.corpora.Dictionary` and `gensim.corpora.MmCorpus.serialize` abstract the process of creating a numerical representation of our text data that is ready for the LDA algorithm.
# Create a dictionary from the processed documents
dictionary = Dictionary(processed_docs)
# Create a bag-of-words corpus
corpus = [dictionary.doc2bow(doc) for doc in processed_docs]

# Build the LDA model
num_topics = 3 # We expect 3 topics in our sample data
lda_model = LdaModel(corpus=corpus,
                     id2word=dictionary,
                     num_topics=num_topics,
                     random_state=100,
                     update_every=1,
                     chunksize=100,
                     passes=10,
                     alpha='auto',
                     per_word_topics=True)

3. Interpreting the Topics

  • What the code does: It prints the discovered topics, with each topic being represented by its most prominent keywords.
  • Behind the scenes: The LDA model doesn't label the topics for you; it simply gives you a list of words that are most likely to appear together in a document. By looking at these words, you can infer what the topic is.
  • Abstraction: The `lda_model.print_topics` method provides a clean, human-readable summary of the topics discovered by the algorithm, abstracting the raw probabilistic output of the model.
# Print the topics and their keywords
print("Topics discovered by the LDA model:")
for idx, topic in lda_model.print_topics(-1):
    print(f"Topic #{idx}: {topic}")

# You can also see the topics in each document
print("\nTopic distribution for each document:")
for i, doc in enumerate(corpus):
    print(f"Document {i}: {lda_model.get_document_topics(doc)}")

Comments

Popular posts from this blog

Named Entity Recognition (NER)

Speech-to-Text with a Simple Model

Keyword Extractor