Kotlin
Java

MP2: Related Restaurants
Kotlin

Created By: Geoffrey Challen
/ Updated: 2021-11-20

To wrap up MP2 we'll use the preference data that we can access to identify relationships between difference restaurants. This will lead us in the direction of being able to make recommendations.

Today's lesson is short and sweet. Good luck wrapping up MP2!

Restaurant Relationships

At this point you've moved the restaurant preferences data to the client, and so now it's time to use it to identify relationships between restaurants! Let's briefly review what you need to do.

As a more complete description of your task, you should complete the RelatedRestaurants as follows. You should provide a primary constructor accepting a List<Restaurant> and a List<Preference>, in that order. Your class should also provide a method getRelated that takes a String containing a restaurant ID and returns a Map<String, Integer>. Keys in the map should be IDs of restaurants that are related to the passed restaurant. The values in the map should reflect the strength of the relationship.

Two restaurants are related if they appear in the same user's list of restaurant preferences. For example, if I like both Thara Thai and Black Dog, then those two restaurants are related. The strength of the relationship measures how many times two restaurants appear in the same list. So, if Colleen also likes Thara Thai, Black Dog, and Sakanaya, then the strength of the relationship between Thara Thai and Black Dog is 2 but between Sakanaya and Black Dog only 1. (Note that, while we're using restaurant names here, you'll be working with their unique IDs.)

A few important notes.

  1. A restaurant is not related to itself.
  2. You should return a valid Map for all restaurant IDs in the passed List<Restaurant>. So if a restaurant is not related to any others, or is not a valid restaurant ID, you should still return an empty Map, not null.
  3. The list of restaurants contained in the preferences objects will contain invalid restaurant IDs, meaning restaurant IDs that are not in the passed List<Restaurant>. You should ignore these invalid IDs and not include them in your map as keys in either the top-level map or in the secondary maps.
  4. You may realize at this point that you have not yet finished your Preference model! Nothing here will work until you do. Remember that you will need an empty constructor.

Our suggestion is that you do most of the work in an init block, which should build a Map<String, Map<String, Integer>> that is then used by getRelated, which will be simple.

The assumption behind this algorithm is that a user's restaurant preferences are themselves related, and can be used in this way to generate recommendations. When you are done you may want to examine the output of this approach and see if you think that it is valid or not. Admittedly, this is a fairly small data set to train it on.

No Homework Today

As a reminder, on lessons where we focus on the machine project we will not assign a homework problem! However, the lesson will usually focus on helping you complete a particular part of the MP test suite, and so we encourage you to spend time on that in lieu of a homework problem.

Today your goal should be to finish MP2 by completing RelatedRestaurants. Once you do, you're done. Congratulations! As always, if you get stuck, find us for help on the help site or forum.