Restaurant Recommendations

Summary of Deadlines

This assignment's practice questions on PrairieLearn are due by Tue, Oct 13 at 11:59pm.

Your solution to the project portion of this assignment is due to Gradescope by Mon, Oct 19 at 11:59 pm.

Presentations for this assignment will take place on Wed, Oct 21 and Thu, Oct 22.

Practice

This assignment features a number of practice questions available on PrairieLearn under the assignment called "HW3: Restaurant Recommendations".

These questions will help you practice skills that are relevant to this assignment before you get started on the project. You must earn points on every question to receive full credit, although you are permitted to try each question as many times as you need. Some questions are static, meaning that they will not change when you retry them. Others have elements of random generation, meaning that you will get a new variant of the question on each attempt. As long as you earn points on one variant of a question, you get full credit for that question.

You are welcome to use whatever resources you want to complete these practice questions, but you should make sure you understand what you're doing. This is important for learning, and we'll also ask you to solve a variant of one of these questions as part of your Presentation in the final section of the project.

This assignment's practice questions on PrairieLearn are due by Tue, Oct 13 at 11:59pm.


Project

Navigate to HW3: Restaurant Recommendations on Codio to get started.

Where: Complete on Codio, Submit on Gradescope

Summary of Deliverables

By the end of this, here's what you'll need to submit to Gradescope:

  • food_recommender.py
  • test_food_recommender_student.py
  • readme_food.txt

0. Getting Started

All files should be available to you in the "Food Recommender" assignment on Codio. If you need to download the starter files again, you can do so here.

A. Background & Goals

How do you decide where to eat when you eat at a restaurant? Example criteria might be: how far away the restaurants are, what cuisine they serve, how expensive they are, or whether they are recommended by others. These criteria can heavily influence which restaurants we go to, and thus can affect the viability of a restaurant as a business. This data may also be imperfect and lead us to false conclusions about places we don't know much about.

We have gathered some data about restaurants near Penn for you to analyze, but it is not the same data that Yelp or Google Maps may use to recommend restaurants. In a future assignment you will handle "real" data, but for now we want to give you an entry point into the realm of data processing. The skills developed in this assignment (and the following "data homework") will probably be the ones most commonly used by anybody who does not become a pure software engineer or computer scientist in the future.

What we want you to do in this assignment is write code to help us parse restaurant data, and in the process gain familiarty with sets, maps, and unit testing.

B. What You Will Do

In this assignment you will start by filling out a function that reads a CSV into a dictionary structure. Afterwards you will write various functions that interact with that structure to extract information on the restaurants whose data are stored in that structure. While you are doing this you will also need to write some unit tests to test the code you write for food_recommender.py. Finally, you will write some tests to make sure that your code is working properly!

1. Reading Data

For the first part, you will implement load_restaurant_csv function. This function takes in a string with the file path of the CSV file we want to read. The CSV file should be laid out something like this:

Name,Cuisine,Price,Distance,1100 Staff Endorsements
Masala Kitchen,Indian,$,0.8,15
Hangry Joes,Chicken,$,0.4,12
# ... more rows with more restaurants

This is a CSV file (Comma, Separated, Values) and you can think of this almost as being a table. If we reason about this file like a table, it would look like:

Name Cuisine Price Distance 1100 Staff Endorsements
Masala Kitchen Indian $ 0.8 15
Hangry Joes Chicken $ 0.4 12

Note that the first line in our CSV file has a special value, it has the name of each column in the table. Every line after the first has a comma separated list describing a restaurant. The first line of our CSV files should always be the same, while the rest of the file can vary (but should still follow the format shown here). You can assume that all files you process will be in this format.

Your first job is to write the function load_restaurant_csv() so that it reads this file and store all of its values into a dict object properly.

The dictionary returned by this function must follow a specific structure. In particular, you should return a dictionary object where:

  • The keys are strings, each string is a different restaurant name
  • The values are tuples containing four different values in the following order:
    • A string containing the cuisine of that restaurant
    • An int representing the cost level of the restaurant. $ would become a "1" and refer to relatively cheap restaurants. $$ would become the integer 2. The higher the number (and thus the more $) the more expensive it is.
    • A float that represents the distance (in miles) the restaurant is from Levine Hall
    • An int that represents the number of endorsements it has from CIS 1100 Course Staff πŸ˜„

If we were to take the example data above, properly parse it into the desired structure, and print it we would get:

{'Masala Kitchen': ('Indian', 1, 0.8, 15), 'Hangry Joes': ('Chicken', 1, 0.4, 12)}

If we indent this into a more readable state we get:

{
    'Masala Kitchen': ('Indian', 1, 0.8, 15),
    'Hangry Joes': ('Chicken', 1, 0.4, 12)
}

This may be a lot to do at once, so we have a suggested path to figuring out how to do this:

  • First: modify load_restaurant_csv() so that it reads all of the lines from the specified file, and printing out every line except for the first one.

  • Second: modify your code so that you split() every line read, identify the Name, Cuisine, Price, Distance and 1100 Staff Endorsements on each line, and print them. Remember that you can pass an argument into the split() function to specify what you want to split on. For example "aaBacBa".split("B") returns ["aa", "ac", "a"].

  • Finally: take each line you have read and store it in a dictionary object in the structure described above. Be sure you properly convert each item into the correct type!

Once you are sure that your dictionary works, you should try running:

pytest -k checkpoint_0

and make sure that it passes the tests. If it does not, then you should fix your code until it works properly.

2. Processing the Dictionary

You will now write 5 more functions, each of which will interact with the same dictionary structure that you built up when reading from a file.

a. Basic Processing

These first four functions are all required for you to implement and support the basic functionality of our food recommender. You may notice that some of them are very similar to each other.

Confirm the correctness of your your code along the way by writing unit tests before you implement the processing functions. See the section below on writing unit tests for more.

Note: For each of these functions, you will need to write a function header comment that gives an overview of what it does. The format should follow the one listed below, which is copied from the style guide.

"""
Description:

Input:

Output:
"""

get_cuisines

Takes in a dictionary in the same format as we built up in load_restaurant_csv() and returns a set of all the different cuisines that are available from restaurants in that dictionary.

Run all tests with the name checkpoint_1 in the name by running the following command:

pytest -k checkpoint_1

max_distance

Takes in a dictionary in the same format as we built up in load_restaurant_csv() and a float representing a distance in miles. The function returns a set of strings, where each string is the name of a restaurant in the input dictionary whose distance is less than or equal to the input distance.

Run all tests with the name checkpoint_2 in the name by running the following command.

pytest -k checkpoint_2

ta_endorsements

Takes in a dictionary in the same format as we built up in load_restaurant_csv() and an integer. The function returns a set of strings, where each string is the name of a restaurant in the input dictionary whose endorsement count is greater than or equal to the input endorsement requirement. This function should be VERY similar to the previous function.

Run all tests with checkpoint_3 in the name to test this function.

filter_cuisine

Takes in a dictionary in the same format as we built up in load_restaurant_csv() and a string representing a specific cuisine. The function returns a set of strings, where each string is the name of a restaurant in the input dictionary that serves the same cuisine as the one provided as input. Note that your code should match cuisines if they contain the same letters but are a different case (uppercase vs lowercase). For example, the cuisine chicken and CHIckeN should be considered the same. You may want to take advantage of str.upper() or str.lower() for this function.

Run all tests with checkpoint_4 in the name to test this function.

b. Advanced Processing

Remember that you will need to write a function header comment that gives an overview of what each function does.

Takes in a dictionary in the same format as we built up in load_restaurant_csv(). The function returns a set of strings, where each string is the name of the most endorsed restaurant in the dictionary for each cuisine present in the dictionary. This means the set will contain as many restaurant names as there are cuisines in the dictionary.

You can assume that no restaurant has a recommendation score less than or equal to -10.

If there are two restaurants in a specific cuisine that tie for most endorsements, choose the one that your program found in the dictionary first.

Run all tests with checkpoint_5 in the name to test this function.

Takes in a dictionary in the same format as we built up in load_restaurant_csv(). The function returns a string that represents the cuisine with the highest average endorsements across its restaurants.

You can assume that no restaurant has a recommendation score less than or equal to -10.

If there is a tie for which cuisine has the highest average endorsements, you may return any of the cuisines that tie.

Run all tests with checkpoint_6 in the name to test this function.

3. Writing Unit Tests

For each of the 5 functions you completed in the previous section (This does NOT include load_restaurant_csv), you will write one test case to test your code. These test cases should be written in test_food_recommender_student.py.

a. Motivation

While we've been using print statements in previous homeworks, for this homework we're going to write actual unit test with pytest, which is a framework used by companies like Amazon and Google to test their code. pytest is valuable because it lets us maintain enormous test suites that can run automatically and let us know if changes to our code breaks any of our functionality - this becomes super crucial as the projects you'll be making in your CIS career will grow in size and complexity. In other words, testing is actually very very very important for CS, even outside of academics.

b. pytest Reminders

As a review, pytest works by comparing what is actually returned by your functions and object state. Here is an example:

import food_recommender 
# above line imports the code we want to test
def test_get_cuisine_example():
    """
    Test that get_cuisines called on a dictionary with just one
    restaurant returns a set with just that restaurant's cuisine.
    """
    # declare what we will use as input for the test
    test_input = {
        "Harry and Travis's Chicken Tendie Surprise": ("Chicken", 1, 0.005, -8),
    }

    # get what the actual output is by calling the code we want to test
    actual = food_recommender.get_cuisines(test_input)

    # declare the expected output:
    expected = { "Chicken" }

    assert actual == expected

While you can put multiple assert statements in one testβ€”and there are times where this can be appropriateβ€”we consider it to be bad practice here. That's because when a single one of them fails, the whole test will fail. This makes it hard to track the source of a fault. For this assignment, you will receive a style deduction if your tests have more than one assert in them.

c. Writing Tests

To start, open test_food_recommender.py and take a look at the test cases that have already been given to you. The test cases that you write yourself must be different than the given cases - credit will not be given to a repeated case.

You might notice that the filenames written in these tests look a little different: 'test_files/empty.csv' or 'test_files/spec_example.csv', for example. These filenames with the / character inside of them are considered file paths that can navigate to a file inside of another folder. In this case, test_files is a folder that you see in Codio. Inside of that, we find CSVs like empty.csv or spec_example.csv. To write a file path, you "navigate" into folders by writing their names and separate them using the \ character, called a separator.

Write your test cases in test_food_recommender_student.py. You are only required to write one new test per function, but we encourage you to write as many tests as you need to feel confident that your code works. Once you've written these, run the tests (directions described below) and make sure they pass. We recommend testing your functions with multiple edge cases and varied input arguments to ensure that your code works for all possible scenarios.

Instead of a detailed function header comment, please write a short (about one-line) summary of what is being tested in each case. You can see we do something like this in the provided test_food_recommender.py

d. Running your Tests

To run your tests, all you need to do is go to the terminal where we would normally run our programs and run the following command:

pytest test_food_recommender_student.py

If you want to run the test functions that we give you, all you need to do is replace one part of the command. Instead of test_food_recommender_student it would just be test_food_recommender:

pytest test_food_recommender.py

If you want to run only some specific tests you can do that by specified further in the command. For example, if I want to run all tests that have some substring in their name, like all tests on an "empty_dataset", then you can use the -k flag:

pytest -k empty_dataset

4. Using the whole program

Once everything is done and working, you can now use the main of our food recommender program! The program is designed to analyze the restaurants that CIS 1100 course staff recommend :) To run it, all you need to do is do:

python food_recommender_main.py restaurants.csv

From here it should start running the application. Try typing "help" and then hitting enter for a list of commands. Once you do this, try running some of the commands to have some restaurants recommended to you. We will ask you in the readme for what recommendations you got. 🍲

Hopefully you get a good restaurant recommendation from it!

5. Readme & Submission

A. Readme

Complete readme_food.txt in the same way that you have done for previous assignments.

B. Submission

Important: Don't forget to write comments for your function headers and test cases as mentioned earlier in the specification.

Important: Remember to delete any print statements you added to your code before submitting.

Submit food_recommender.py, test_food_recommender_student.py and readme_food.txt on Gradescope.

Your code will be tested for compilation and checkstyle errors upon submission.

If you encounter any autograder-related issues, please make a private post on Ed.


Presentation

You will be required to meet with a TA for fifteen minutes outside of class to present your work on this assignment. Presentations for this assignment will take place on Wed, Oct 21 and Thu, Oct 22. Sign up for a presentation time slot with the link posted on Ed.

As a reminder, you'll start the presentation with ten minutes spent on reviewing and discussing your project submission.

Then, your TA will ask you one or two questions about the behavior of your code to judge your understanding.

Finally, you'll be given a variant of a question from the PrairieLearn Practice portion of this assignment and asked to solve it.

You will not have access to any resources during this part of the presentation.

Grading

The grading rubric required to achieve each criterion will be different for each question, but the way that we approximately convert answers to points is displayed in the following scheme. Note that answers need not be perfectly correct to receive a high score on this portion of the presentation.

Criterion Grade for Question
Fully correct answer with ability to justify A+
Partially correct answer with a reasonable approach and the ability to justify A
Mostly incorrect/incomplete answer with ability to reason aloud about the steps taken B
Mostly incorrect/incomplete answer with partial ability to reason aloud about the steps taken B-
Incorrect or incomplete answers with no reasonable justification Participation credit only