0% found this document useful (0 votes)
6 views9 pages

Smart Lyrics Finder in Python

The document outlines a Python program called Smart Lyrics Finder and Analyzer that utilizes various data structures like stacks, linked lists, arrays, queues, and trees to search for songs based on partial lyrics and analyze their themes. It includes classes for managing songs, linked lists for song storage, a stack for search history, a queue for songs to analyze, and a tree structure for themes. An example usage demonstrates adding a song, searching for lyrics, analyzing themes, and displaying the last search query.

Uploaded by

tekbwoy1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Smart Lyrics Finder in Python

The document outlines a Python program called Smart Lyrics Finder and Analyzer that utilizes various data structures like stacks, linked lists, arrays, queues, and trees to search for songs based on partial lyrics and analyze their themes. It includes classes for managing songs, linked lists for song storage, a stack for search history, a queue for songs to analyze, and a tree structure for themes. An example usage demonstrates adding a song, searching for lyrics, analyzing themes, and displaying the last search query.

Uploaded by

tekbwoy1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sure!

Let’s break down a simple, understandable Python code for a


**Smart Lyrics Finder and Analyzer** that uses **Stacks, Linked Lists,
Arrays, Queues, and Trees**. The goal is to find songs based on partial
lyrics and analyze their meaning. We’ll use one song as an example.

### Code Walkthrough:

The main goal of the program is:

1. To search for songs based on partial lyrics.

2. To analyze the themes or meaning in the lyrics using basic string


matching.

3. Using different data structures to store and manage the songs and their
analysis.

### Here’s the code:

```python

# Song class to represent a song with title and lyrics

Class Song:

Def __init__(self, title, lyrics):

[Link] = title # Store song title

[Link] = lyrics # Store song lyrics

# Linked List Node to store each song in the linked list

Class LinkedListNode:

Def __init__(self, song):

[Link] = song # The song object

[Link] = None # Link to the next node (song)

# Linked List class to manage a collection of songs

Class LinkedList:
Def __init__(self):

[Link] = None # Start with an empty list

# Method to add a song to the linked list

Def add_song(self, song):

New_node = LinkedListNode(song) # Create a new node for the


song

If not [Link]: # If list is empty, make this the first node

[Link] = new_node

Else:

Current = [Link] # Start from the head

While [Link]: # Traverse until the last node

Current = [Link]

[Link] = new_node # Add the new song at the end

# Method to find a song by partial lyrics

Def find_song_by_lyrics(self, partial_lyrics):

Current = [Link] # Start from the head of the list

While current: # Traverse through each song

If partial_lyrics.lower() in [Link](): # Case-


insensitive match

Return [Link] # If found, return the song

Current = [Link] # Move to the next song in the list

Return None # Return None if no match is found

# Stack class to store search history (last searched lyrics)

Class Stack:

Def __init__(self):

[Link] = [] # Initialize an empty list to simulate stack behavior


# Push an item (search query) onto the stack

Def push(self, item):

[Link](item) # Add the item to the top of the stack

# Pop the top item from the stack

Def pop(self):

If [Link]: # If the stack is not empty

Return [Link]() # Remove and return the top item

Return None # Return None if the stack is empty

# Peek at the top item without removing it

Def peek(self):

If [Link]: # If the stack is not empty

Return [Link][-1] # Return the top item

Return None # Return None if the stack is empty

# Queue class to store songs that need to be analyzed (FIFO structure)

Class Queue:

Def __init__(self):

[Link] = [] # Initialize an empty list to simulate queue behavior

# Enqueue a song to the end of the queue

Def enqueue(self, item):

[Link](item) # Add the item to the end of the queue

# Dequeue a song from the front of the queue

Def dequeue(self):

If [Link]: # If the queue is not empty

Return [Link](0) # Remove and return the first song


Return None # Return None if the queue is empty

# Check if the queue is empty

Def is_empty(self):

Return len([Link]) == 0 # Return True if empty, else False

# TreeNode class to represent themes in the lyrics (tree structure)

Class TreeNode:

Def __init__(self, theme):

[Link] = theme # The theme (e.g., “love”, “hope”)

[Link] = [] # A list to store sub-themes (children)

# Add a child theme (sub-theme) to the tree

Def add_child(self, child_node):

[Link](child_node) # Append the child node to the


children list

# SmartLyricsAnalyzer class to manage the lyrics analysis process

Class SmartLyricsAnalyzer:

Def __init__(self):

Self.songs_list = LinkedList() # Linked list to store songs

Self.songs_queue = Queue() # Queue for songs to be analyzed

Self.previous_search_stack = Stack() # Stack to store previous


search queries

Self.lyrics_themes_tree = TreeNode(“Root”) # Root node for themes

# Add a song to the song collection (linked list)

Def add_song(self, title, lyrics):

Song = Song(title, lyrics) # Create a song object

Self.songs_list.add_song(song) # Add the song to the linked list


# Find a song based on partial lyrics and store the search history

Def find_song(self, partial_lyrics):

Song = self.songs_list.find_song_by_lyrics(partial_lyrics) # Search for


the song

If song:

Print(f”Song found: {[Link]}”) # Print song title if found

Self.previous_search_stack.push(partial_lyrics) # Store the search


query in the stack

Self.songs_queue.enqueue(song) # Add song to the queue for


analysis

Else:

Print(“No song found with those lyrics.”) # Print if no song is


found

# Analyze the song’s lyrics to identify themes

Def analyze_song(self):

If not self.songs_queue.is_empty(): # If there are songs to analyze

Song = self.songs_queue.dequeue() # Get the song from the front


of the queue

Print(f”Analyzing song: {[Link]}”) # Print the song title being


analyzed

Self.analyze_lyrics_themes([Link]) # Analyze the lyrics for


themes

Else:

Print(“No songs in the queue for analysis.”) # Print if no songs are


in the queue

# Analyze the lyrics to identify themes like love, hope, etc.

Def analyze_lyrics_themes(self, lyrics):

Print(“Analyzing themes from lyrics...”)

Themes = [“love”, “life”, “hope”, “pain”] # Predefined themes


For theme in themes: # Check if any of the themes appear in the
lyrics

If [Link]() in [Link](): # Case-insensitive match

Self.add_theme_to_tree(theme) # Add detected theme to the


tree

Print(f”Theme detected: {theme}”) # Print the detected theme

# Add a detected theme to the themes tree

Def add_theme_to_tree(self, theme):

Theme_node = TreeNode(theme) # Create a new node for the


theme

Self.lyrics_themes_tree.add_child(theme_node) # Add theme to the


root node’s children

# Display the last searched lyrics from the stack

Def show_previous_search(self):

Last_search = self.previous_search_stack.peek() # Get the last


searched lyrics

If last_search:

Print(f”Last searched lyrics: {last_search}”) # Print the last search

Else:

Print(“No previous searches.”) # Print if no previous search exists

# Example usage:

Analyzer = SmartLyricsAnalyzer()

# Add one example song to the analyzer

Song_title = “Example Song”

Song_lyrics = “This is a song about love, life, and hope. It brings pain too,
sometimes.”

Analyzer.add_song(song_title, song_lyrics)
# Search for a song based on partial lyrics

Analyzer.find_song(“love”)

# Analyze the song’s lyrics for themes

Analyzer.analyze_song()

# Show the last search query

Analyzer.show_previous_search()

```

### Explanation of Each Line:

1. **Song Class**:

- `class Song:`: Defines a class to represent a song.

- `def __init__(self, title, lyrics):`: Constructor to initialize the song with a


title and lyrics.

2. **LinkedListNode Class**:

- `class LinkedListNode:`: Defines a node to hold each song and link to


the next song.

- `def __init__(self, song):`: Constructor to initialize the node with a song.

3. **LinkedList Class**:

- `class LinkedList:`: Defines a linked list to manage multiple songs.

- `def add_song(self, song):`: Method to add a song to the linked list.

- `def find_song_by_lyrics(self, partial_lyrics):`: Method to search for a


song based on partial lyrics.

4. **Stack Class**:
- `class Stack:`: Defines a stack data structure to store search history.

- `def push(self, item):`: Method to push an item onto the stack.

- `def pop(self):`: Method to pop an item off the stack.

- `def peek(self):`: Method to peek at the top item of the stack.

5. **Queue Class**:

- `class Queue:`: Defines a queue to manage songs for analysis.

- `def enqueue(self, item):`: Method to add a song to the queue.

- `def dequeue(self):`: Method to remove a song from the front of the


queue.

6. **TreeNode Class**:

- `class TreeNode:`: Defines a tree structure for holding themes (like


“love” or “hope”).

- `def add_child(self, child_node):`: Method to add a sub-theme to the


node.

7. **SmartLyricsAnalyzer Class**:

- `class SmartLyricsAnalyzer:`: The main class for managing songs,


searches, and analysis.

- `def add_song(self, title, lyrics):`: Adds a song to the linked list.

- `def find_song(self, partial_lyrics):`: Searches for a song based on


partial lyrics and adds it to the queue for analysis.

- `def analyze_song(self):`: Analyzes the song’s lyrics for themes like


“love” or “hope”.

- `def analyze_lyrics_themes(self, lyrics):`: Identifies themes within the


lyrics and adds them to the theme tree.

- `def show_previous_search(self):`: Displays the last search query.

### Example Usage:


1. **Adding a song**: We add one song titled “Example Song” with lyrics
about **love, life, hope**, and **pain**.

2. **Searching for songs**: The system searches for a song based on the
word “love”.

3. **Analyzing themes**: It identifies themes like **love** and **hope** in


the lyrics.

4. **Showing previous search**: The system shows the last search term
(“love”).

This code is simple, uses basic data structures, and demonstrates how we
can organize and analyze song lyrics based on partial text searches and
theme detection.

You might also like