- Pull the repository to your local machine and then create a new branch
- In the new branch, enter the client folder and run
npm installoryarn installto install all the dependencies in the terminal. - Read the updated frontend code written by @Broliix and try to understand the algorithm code written by @Wanderer0074348
- Make changes to the frontend code and test it by running
npm run startoryarn startin the terminal.
Please make changes only to your branch, to merge to main, perform a pull request. Do not change the main branch. Thanks
This section explains the A* search algorithm for ranking songs based on a query. The algorithm is implemented in Python and uses heuristics to determine the similarity between the query and both the song title and artist.
def a_star_search(query):
pq = []
for idx, row in df.iterrows():
song_heuristic = fuzz.partial_ratio(query.lower(), row['title'].lower())
artist_heuristic = fuzz.partial_ratio(query.lower(), row['artist'].lower())
h = max(song_heuristic, artist_heuristic)
g = 100 - row['popularity']
f = g + (100 - h)
heapq.heappush(pq, (f, idx))
ranked_results = []
while pq:
f, idx = heapq.heappop(pq)
ranked_results.append(df.iloc[idx].to_dict())
return ranked_results- Defines the function
a_star_searchwhich takes a query (user's search term) as input.
- Initializes an empty list
pqthat will act as a priority queue for storing the songs and their computed scores.
- Iterates over each row in the dataset
dfwhich contains the songs. It returns both the index (idx) and row (song data).
- Calculates a heuristic that measures the similarity between the query and the song's title. The
fuzz.partial_ratiofunction (from thefuzzywuzzylibrary) returns a score between 0 and 100.
- Similarly, this calculates the heuristic for how similar the query is to the artist's name. The same fuzzy matching is applied.
- Chooses the maximum value between the song title heuristic and the artist heuristic. The final heuristic (
h) is the best match for either the title or the artist.
- Calculates a score
gbased on the song's popularity. Less popular songs have highergscores, meaning they are prioritized.
- The final score
fis the sum of the popularity score (g) and the heuristic penalty (100 - h). Songs that are less popular and have a higher match get a lowerfscore.
- Adds the song's score
(f, idx)to the priority queuepq. The heap structure ensures that songs with the lowestfscore can be retrieved first.
- Initializes an empty list
ranked_resultsto store the final sorted list of songs.
- This loop continues as long as there are items in the priority queue
pq.
- Pops (removes) the song with the lowest score
ffrom the priority queue. This gives the best-matching song first.
- Converts the song at index
idxto a dictionary and adds it to theranked_resultslist.
- Returns the list of ranked results based on the user's query.
-
Heuristic (
h):- The heuristic measures how similar the query is to both the song's title and artist name. The higher the score (closer to 100), the better the match.
-
Popularity Score (
g):- Less popular songs are given a higher
gscore. This helps prioritize songs that might be less well-known but still relevant to the query.
- Less popular songs are given a higher
-
Final Score (
f):- The total score
fis a combination of the popularity score and the heuristic penalty. Lowerfvalues indicate better matches.
- The total score
The A* search algorithm in this implementation ranks songs based on their similarity to the user's query and adjusts for popularity. It prioritizes songs that match well with the query while giving a slight edge to lesser-known songs.