Beispiel #1
0
 def test_restaurant_ratings(self):
     soda_reviews = [make_review('Soda', 4.5), make_review('Soda', 4)]
     soda = make_restaurant('Soda', [127.0, 0.1],
                            ['Restaurants', 'Breakfast & Brunch'], 1,
                            soda_reviews)
     val = restaurant_ratings(soda)
     self.assertEqual(val, [4.5, 4])
Beispiel #2
0
 def test_find_centroid(self):
     cluster1 = [
         make_restaurant('A', [-3, -4], [], 3, [make_review('A', 2)]),
         make_restaurant('B', [1, -1], [], 1, [make_review('B', 1)]),
         make_restaurant('C', [2, -4], [], 1, [make_review('C', 5)]),
     ]
     val = find_centroid(cluster1)  # should be a pair of decimals
     self.assertEqual(val, [0.0, -3.0])
Beispiel #3
0
 def test_restaurant_mean_rating(self):
     woz_reviews = [
         make_review('Wozniak Lounge', 4),
         make_review('Wozniak Lounge', 3),
         make_review('Wozniak Lounge', 5)
     ]
     woz = make_restaurant('Wozniak Lounge', [127.0, 0.1],
                           ['Restaurants', 'Pizza'], 1, woz_reviews)
     val = restaurant_mean_rating(woz)
     self.assertEqual(val, 4.0)
Beispiel #4
0
 def test_find_predictor(self):
     user = make_user('John D.', [
         make_review('A', 1),
         make_review('B', 5),
         make_review('C', 2),
         make_review('D', 2.5),
     ])
     restaurant = make_restaurant('New', [-10, 2], [], 2, [
         make_review('New', 4),
     ])
     cluster = [
         make_restaurant('B', [4, 2], [], 1, [make_review('B', 5)]),
         make_restaurant('C', [-2, 6], [], 4, [make_review('C', 2)]),
         make_restaurant('D', [4, 2], [], 3.5, [
             make_review('D', 2.5),
             make_review('D', 3),
         ]),
     ]
     pred, r_squared = find_predictor(user, cluster, restaurant_mean_rating)
     val1 = round(pred(restaurant), 5)
     print("val1 is: " + str(val1))
     self.assertAlmostEqual(val1, 3.9359, 4)
     val2 = round(r_squared, 5)
     print("val2 is: " + str(val2))
     self.assertAlmostEqual(val2, 0.99256, 4)
Beispiel #5
0
    def test_rate_all(self):
        user = make_user('Mr. Mean Rating Minus One', [
            make_review('A', 3),
            make_review('B', 4),
            make_review('C', 1),
        ])

        cluster = [
            make_restaurant(
                'A', [1, 2], [], 4,
                [make_review('A', 4), make_review('A', 4)]),
            make_restaurant('B', [4, 2], [], 3, [make_review('B', 5)]),
            make_restaurant('C', [-2, 6], [], 4, [make_review('C', 2)]),
            make_restaurant('D', [4, 4], [], 3.5, [
                make_review('D', 2.5),
                make_review('D', 3.5),
            ]),
        ]

        restaurants = {restaurant_name(r): r for r in cluster}

        ALL_RESTAURANTS = cluster

        to_rate = cluster[2:]

        fns = [restaurant_price, restaurant_mean_rating]

        ratings = rate_all(user, to_rate, fns, ALL_RESTAURANTS)
        print(type(ratings), "Should be ", "dict")

        print(len(ratings), "Should be ", 2)

        print(ratings['C'], "Should be", 1)
        self.assertEqual(ratings['C'], 1)
        print(round(ratings['D'], 5), "Should be ", 2.0)
        self.assertEqual(ratings['D'], 2.0)
Beispiel #6
0
    def test_best_predictor(self):
        user = make_user('Cheapskate', [
            make_review('A', 2),
            make_review('B', 5),
            make_review('C', 2),
            make_review('D', 5),
        ])

        cluster = [
            make_restaurant('A', [5, 2], [], 4, [make_review('A', 5)]),
            make_restaurant('B', [3, 2], [], 2, [make_review('B', 5)]),
            make_restaurant('C', [-2, 6], [], 4, [make_review('C', 4)]),
        ]

        fns = [restaurant_price, restaurant_mean_rating]

        pred = best_predictor(user, cluster, fns)
        print([round(pred(r), 5) for r in cluster], "SHOULD =",
              [2.0, 5.0, 2.0])
        self.assertEqual([round(pred(r), 5) for r in cluster], [2.0, 5.0, 2.0])
Beispiel #7
0
    def setUpClass(cls):
        print("setUpClass")
        cls.r1 = make_restaurant('A', [-10, 2], ['Fast Food', 'Thai'], 2, [
            make_review('A', 4),
        ])
        cls.r2 = make_restaurant('B', [-9, 1], ['Fast Food', 'American'], 3, [
            make_review('B', 5),
            make_review('B', 3.5),
        ])
        cls.r3 = make_restaurant('C', [4, 2], [
            'Fast Food',
        ], 1, [make_review('C', 5)])
        cls.r4 = make_restaurant('D', [-2, 6], ['Sit Down', 'Thai'], 4,
                                 [make_review('D', 2)])
        cls.r5 = make_restaurant('E', [4, 2], ['Italian', 'German'], 3.5, [
            make_review('E', 2.5),
            make_review('E', 3),
        ])
        cls.c1 = [0, 0]
        cls.c2 = [3, 4]
        cls.restaurants1 = [
            make_restaurant('A', [-3, -4], [], 3, [make_review('A', 2)]),
            make_restaurant('B', [1, -1], [], 1, [make_review('B', 1)]),
            make_restaurant('C', [2, -4], [], 1, [make_review('C', 5)])
        ]

        cls.restaurants2 = [
            make_restaurant('D', [2, 3], [], 2, [make_review('D', 2)]),
            make_restaurant('E', [0, 3], [], 3, [make_review('E', 1)])
        ]