CPSC312-2021/song recommender

From UBC Wiki

Authors: Adrian Pikor, Victor Cheng, Janice Wu

What is the problem?

With the amount of songs being produced to date, it may be difficult and overwhelming to find new music that users would like to listen to. To aid in this problem, our Prolog recommendation system will be able to produce a personalized playlist with the user's input being their preference in music.

The user will be able to select from a genre, year, or an artist for Top-Track / Top-Artist/ Top-Album features and the program will be able to return a playlist containing the top 5 hits from the selected option.

What is the something extra?

We will retrieve the songs using the user's input and Last.fm's API and then parse through the data returned and output a list of songs with their respective artists/musicians and a link to said songs.

Retrieve user's input:

  • Our program retrieve the input from user and guard against unexpected value
  • Process the input so our program can generate a URL to communicate with API

Communicate with the API (https://www.last.fm/api):

  • We use Last.fm's API for our songs/ albums/ artists searching.
  • We implemented three main feature from the API:
  1. tag.getTop Albums:
    • Get the top albums tagged by the tag, ordered by tag count.
    • Example URLs: JSON: /2.0/?method=tag.gettopalbums&tag=disco&api_key=YOUR_API_KEY&format=json
    • Params:
      • tag (Required) : The tag name
      • limit (Optional) : The number of results to fetch per page. Defaults to 50.
      • page (Optional) : The page number to fetch. Defaults to first page.
      • api_key (Required) : A Last.fm API key.
  2. tag.getTopArtists
    • Get the top artists tagged by the tag, ordered by tag count.
    • Example URLs: JSON: JSON: /2.0/?method=tag.gettoptracks&tag=disco&api_key=YOUR_API_KEY&format=json
    • Params:
      • tag (Required) : The tag name
      • limit (Optional) : The number of results to fetch per page. Defaults to 50.
      • page (Optional) : The page number to fetch. Defaults to first page.
      • api_key (Required) : A Last.fm API key.
  3. tag.getTopTracks
    • Get the top tracks tagged by this tag, ordered by tag count.
    • Example URLs: JSON: JSON: /2.0/?method=tag.gettoptracks&tag=disco&api_key=YOUR_API_KEY&format=json
      • tag (Required) : The tag name
      • limit (Optional) : The number of results to fetch per page. Defaults to 50.
      • page (Optional) : The page number to fetch. Defaults to first page.
      • api_key (Required) : A Last.fm API key.
  • We provide three options for each feature, user can select either genre, artist or year (search for specific year) .


Create a URL for the API:

  • Different API methods require different URL format, so we have three different functions to create the URL for three main features: Top-Tracks, Top-Artists, Top-Albums.
  • After we process the user's input, we will create a tag which is part of the URL, you can think of the tag behaves like a search engine.
  • We will make the HTTP request using the unique URL that we created here.


HTTP GET Request:

  • Our program make the HTTP "GET" request to LastFM's Server, which returns JSON that contains all the data we need.


JSON Example:File:JSON File example.jpg:

  • LastFM's Server returns a JSON file which contains all the data we need.
  • The JSON file is structured differently in each API methods, we need different functions to handle the JSON file.
  • We parse the JSON file and get the data we need and print it out.
JSON_File_example

Two User Interfaces:

  • We provide two user interfaces:
    1. General User Interface - guide you throughout the program, user can just follow the instruction.
    2. Natural Language User Interface - user can ask any questions, our system will analyze the question and give user the feedback!

- Example: "I like Justin Bieber", "Who is the top artist of 2020", "What is the top song of 2021"

  • We created a database that handle natural language, parse through the input (question) and create the tag for the URL.

What did we learn from doing this?

Learned about Logical Programming:

Compared to an OOP language, a logic programming language behaves differently because of the program structure, syntax, design and so on.

OOP language based on the concept of object, object design, and object oriented use the mutable data. But logical language largely based on formal logic, logical language is a set of sentences in logical form, expressing facts and rules about some problem domain. I like logical language because it's easy to read and understand, and you can use this language to construct a powerful program without using any complex syntax.


Using a web API with Prolog

We learned how to use a web API in Prolog, which includes using the appropriate Prolog libraries to handle HTTP requests. There's no main difference between using the API in prolog and modern OOP language, the only difference is the syntax. We also learned how to parse through the JSON object the Prolog HTTP functions return and extract the data needed to build a playlist (User readable format).


Is Logical Programming suitable for this program?

Logical Programming is suitable for this program because of the natural of logical language, we can just build a database which is a set of sentences in logical form, expressing the true facts and rules about the problem. It's very easy to read and understand the code, because each line of code is equivalent to a logical statement, and it's very easy to build up the program from the bottom, which makes prolog a powerful language. For example, building a database, communicates with API and parsing JSON file requires lots of tools, library, complex syntax in modern OOP language, but you can do that easily in logical language, because it's like writing a set of rules in sentences.

Links to code etc.

github


Example of our prolog program