Title Guessing Game ๐ŸŽฏ

Anand - May 27 - - Dev Community

Title Guessing from a Text Corpus Using Python ๐Ÿ’ฌ๐Ÿ

Image

Title guessing, or title generation, is a fascinating area in natural language processing (NLP) where we attempt to generate a relevant title for a given text corpus. In this post, I'll walk through a Python script that performs title guessing using some basic NLP techniques. We'll be using libraries such as nltk and pandas for our analysis.

Prerequisites

Before we dive into the code, make sure you have the necessary libraries installed. You can install them using pip:

pip install nltk
pip install pandas
Enter fullscreen mode Exit fullscreen mode

>_ PyCode

from nltk import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
import pandas as pd
Enter fullscreen mode Exit fullscreen mode
# Sample text corpus
corpus = "Wolverine, one of the most iconic characters in the X-Men universe, is known for his extraordinary abilities and complex personality. Wolverine possesses enhanced senses, superhuman strength, and a rapid healing factor that allows him to recover from almost any injury. His adamantium-coated skeleton and retractable claws make Wolverine a formidable fighter."
Enter fullscreen mode Exit fullscreen mode
# Tokenize the corpus into words and remove stopwords
words = []
for word in word_tokenize(corpus):
    if word.lower() not in stopwords.words('english') and len(word) >= 2:
        words.append(word)
Enter fullscreen mode Exit fullscreen mode
# Create a vocabulary set
vocab = set(words)
Enter fullscreen mode Exit fullscreen mode
# Initialize word count dictionary
word_count = {word: 0 for word in vocab}
Enter fullscreen mode Exit fullscreen mode
# Count the frequency of each word
for word in words:
    word_count[word] += 1
Enter fullscreen mode Exit fullscreen mode
# Prepare data for the DataFrame
data = [[word, freq] for word, freq in word_count.items()]
Enter fullscreen mode Exit fullscreen mode
# Create a DataFrame and sort by frequency
df = pd.DataFrame(data, columns=['word', 'freq'])
guessed_title = df.sort_values(by='freq', ascending=False).head().values[0][0]
Enter fullscreen mode Exit fullscreen mode
print("Guessed Title:", guessed_title)  #output : Wolverine
Enter fullscreen mode Exit fullscreen mode

</> Explanation

Imports and Data Setup:

โ†’ We start by importing the necessary libraries: nltk for natural language processing and pandas for data handling.

Text Corpus:

โ†’ The variable corpus contains a sample text about Wolverine, a popular character from the X-Men universe.

Tokenization and Stopword Removal:

โ†’ We tokenize the corpus into individual words using word_tokenize and remove common stopwords using NLTK's stopwords list. Additionally, we filter out words with fewer than two characters.

Vocabulary and Word Counting:

โ†’ We create a set of unique words (vocabulary) and initialize a dictionary to count the frequency of each word.
โ†’ We iterate through the list of words to update their counts in the word_count dictionary.

Data Preparation and Sorting:

โ†’ We prepare a list of lists containing words and their corresponding frequencies.
โ†’ We create a DataFrame from this data and sort it by frequency in descending order to find the most frequent word.

Guessing the Title:

โ†’ The guessed title is the word with the highest frequency, which we obtain by selecting the first row of the sorted DataFrame.

Conclusion

In this project, I explored a simple approach to title guessing from a text corpus using Python. By tokenizing the text, removing stopwords, counting word frequencies, and sorting the results, we can identify the most frequent word as a potential title. This method provides a basic yet effective way to generate titles for text documents.

Feel free to experiment with different corpus and tweak the code to improve title accuracy. Happy coding!


About Me

๐Ÿ–‡๏ธLinkedIn
๐Ÿง‘โ€๐Ÿ’ปGitHub

. . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player