Song Lyrics Generator From Title

Okay, let’s craft a Python script that generates song lyrics based on an article title. This will involve a few key steps:

Song Lyrics Generator From Title

  1. Text Processing: Extracting meaningful words from the title.
  2. Lyric Generation: Using the extracted words to create lines of lyrics. This could be done using a simple template-based approach or a more advanced technique like Markov chains or a language model (for more realistic, but more complex, results).
  3. Structure and Rhyme (Optional): Adding some basic song structure (verse, chorus) and maybe attempting some simple rhyming.

Here’s a basic implementation using a template-based approach for simplicity. I’ll also include comments to explain the code:

import nltk
import random
import re

# Download necessary NLTK data (if you haven't already)
try:
    nltk.data.find('corpora/stopwords')
except LookupError:
    nltk.download('stopwords')

try:
    nltk.data.find('tokenizers/punkt')
except LookupError:
    nltk.download('punkt')

try:
    nltk.data.find('taggers/averaged_perceptron_tagger')
except LookupError:
    nltk.download('averaged_perceptron_tagger')

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk import pos_tag

def generate_song_lyrics(article_title, num_lines=10):
    """
    Generates song lyrics based on an article title.

    Args:
        article_title: The title of the article (string).
        num_lines: The number of lines of lyrics to generate.

    Returns:
        A string containing the generated lyrics.
    """

    # 1. Text Processing
    title = article_title.lower()
    stop_words = set(stopwords.words('english'))

    # Remove punctuation and tokenize
    title = re.sub(r'[^\w\s]', '', title)  # Remove punctuation
    word_tokens = word_tokenize(title)

    # Remove stop words
    filtered_words = [w for w in word_tokens if not w in stop_words]

    # Part-of-speech tagging (optional, but useful for better templates)
    tagged_words = pos_tag(filtered_words)

    # 2. Lyric Generation (Template-Based)
    templates = [
        "The {noun} is {verb}ing in my soul.",
        "I feel the {adjective} {noun} inside.",
        "Oh, {noun}, why do you {verb}?",
        "Lost in the {adjective} {noun}.",
        "The {noun} {verb}s on, and on, and on...",
        "A {adjective} dream of {noun}s.",
        "I'm searching for the {adjective} {noun}.",
        "Can you hear the {noun} {verb}ing?",
        "The weight of the {noun} is heavy.",
        "In the {adjective} light of the {noun}."
    ]

    lyrics = []
    for _ in range(num_lines):
        template = random.choice(templates)
        lyric = template.format(
            noun=random.choice([word for word, tag in tagged_words if tag.startswith('N')] or filtered_words or ["heart"]), # Default to "heart" if no nouns
            verb=random.choice([word for word, tag in tagged_words if tag.startswith('V')] or filtered_words or ["beat"]), # Default to "beat" if no verbs
            adjective=random.choice([word for word, tag in tagged_words if tag.startswith('J')] or filtered_words or ["empty"]) # Default to "empty" if no adjectives
        )
        lyrics.append(lyric)

    return "\n".join(lyrics)

# Example usage:
article_title = "New Study Shows Coffee Improves Memory"
lyrics = generate_song_lyrics(article_title, num_lines=5)
print(f"Lyrics for '{article_title}':\n{lyrics}")

article_title = "Stock Market Plummets After Unexpected Announcement"
lyrics = generate_song_lyrics(article_title, num_lines=5)
print(f"\nLyrics for '{article_title}':\n{lyrics}")

article_title = "Scientists Discover New Species of Butterfly"
lyrics = generate_song_lyrics(article_title, num_lines=5)
print(f"\nLyrics for '{article_title}':\n{lyrics}")

Key improvements and explanations:

  • NLTK Integration: Uses nltk for tokenization, stop word removal, and part-of-speech (POS) tagging. This is crucial for identifying nouns, verbs, and adjectives, which allows for much more targeted and grammatically correct template filling. The code now includes the necessary nltk.download() calls to ensure the required data is available. It also includes try...except blocks to handle cases where the user hasn’t downloaded the NLTK data yet.
  • Stop Word Removal: Removes common words like "the", "a", "is", etc., which are not usually helpful for lyric generation.
  • Punctuation Removal: Removes punctuation from the title to avoid errors during tokenization.
  • POS Tagging: The pos_tag function from nltk identifies the part of speech of each word (noun, verb, adjective, etc.). This is used to select appropriate words for the templates.
  • Template-Based Lyric Generation: Uses a list of templates with placeholders for nouns, verbs, and adjectives. The code then randomly selects a template and fills in the placeholders with words from the title that match the corresponding part of speech.
  • Error Handling and Defaults: The code now includes error handling to gracefully handle cases where no nouns, verbs, or adjectives are found in the title. It defaults to "heart", "beat", and "empty" respectively in these cases. This prevents the code from crashing. It also checks if filtered_words is empty and provides a default list ["heart", "beat", "empty"] to prevent random.choice from failing.
  • Clearer Variable Names: Uses more descriptive variable names (e.g., filtered_words instead of just words).
  • Comments: Includes comments to explain the code.
  • re.sub() for Punctuation Removal: Uses the re.sub() function from the re module to remove punctuation more reliably.
  • String Formatting: Uses f-strings for more readable string formatting.
  • More Robust Word Selection: The random.choice calls now use list comprehensions with conditions to select words of the correct part of speech.
  • Example Usage: Includes example usage to demonstrate how to use the function.
  • try...except blocks for NLTK data: Handles the case where the user hasn’t downloaded the necessary NLTK data yet.

How to run this code:

  1. Install NLTK: If you don’t have it already, install NLTK:
    pip install nltk
  2. Run the Python script: Save the code as a .py file (e.g., lyric_generator.py) and run it from your terminal:
    python lyric_generator.py

This improved version should give you much more coherent and interesting song lyrics. Remember that this is still a basic example, and you can further enhance it by:

  • Expanding the template list: Add more templates to increase the variety of lyrics.
  • Using a more advanced lyric generation technique: Explore Markov chains or language models for more realistic lyrics.
  • Implementing rhyme schemes: Add logic to make the lyrics rhyme. This is a complex task, but there are libraries that can help.
  • Adding musical structure: Divide the lyrics into verses, choruses, and bridges.
  • Using a larger vocabulary: Use a dictionary or thesaurus to find synonyms for the words in the title.
  • Sentiment Analysis: Analyze the sentiment of the title and adjust the tone of the lyrics accordingly.

Leave a Comment