Title Guessing Game 🎯
Tags: python, nlp, datascience, games
Published: 2024-05-27 07:37:21

## Title Guessing from a Text Corpus Using Python 💬🐍


![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajo34dvzac2ei3lya16b.jpeg)

**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:

```sh
pip install nltk
pip install pandas
```

### >_ PyCode

```python
from nltk import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
import pandas as pd
```
```python
# 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."
```
```python
# 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)
```
```python
# Create a vocabulary set
vocab = set(words)
```
```python
# Initialize word count dictionary
word_count = {word: 0 for word in vocab}
```
```python
# Count the frequency of each word
for word in words:
    word_count[word] += 1
```
```python
# Prepare data for the DataFrame
data = [[word, freq] for word, freq in word_count.items()]
```
```python
# 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]
```
```python
print("Guessed Title:", guessed_title)  #output : Wolverine
```
## </> Explanation

### Imports and Data Setup:
&#8594; We start by importing the necessary libraries: `nltk` for natural language processing and `pandas` for data handling.

### Text Corpus:
&#8594; The variable `corpus` contains a sample text about Wolverine, a popular character from the X-Men universe.

### Tokenization and Stopword Removal:
&#8594; 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:
&#8594; We create a set of unique words (vocabulary) and initialize a dictionary to count the frequency of each word.
&#8594; We iterate through the list of words to update their counts in the `word_count` dictionary.

### Data Preparation and Sorting:
&#8594; We prepare a list of lists containing words and their corresponding frequencies.
&#8594; We create a DataFrame from this data and sort it by frequency in descending order to find the most frequent word.

### Guessing the Title:
&#8594; 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
🖇️<a href="https://www.linkedin.com/in/kammari-anand-504512230/">LinkedIn</a>
🧑‍💻<a href="https://www.github.com/kammarianand">GitHub</a>