def test_run_constant_predictor(self):
     """ Tests the input and output formats of run()"""
     reader = StringIO("1:\n1\n2\n3\n")
     writer = StringIO()
     predictor = ConstantPredictor(1)
     answer = ConstantPredictor(2)
     Netflix.run(reader, writer, predictor, answer)
     self.assertEqual(writer.getvalue(), "1:\n1.0\n1.0\n1.0\nRMSE: 1.00\n")
 def test_solve(self):
     """
     test solve
     """
     read = StringIO("10089:\n662823\n1693997\n1985602\n2345955\n1205751\n")
     word = StringIO()
     Netflix.netflix_solve(read, word)
     self.assertEqual(word.getvalue(),
                      "10089:\n2.4\n4.4\n2.8\n3.0\n5.0\nRMSE: 0.59\n")
    def test_run_math_predictor(self):
        """ Tests run() using the MathPredictor """
        reader = StringIO("1:\n1\n2\n3\n")
        writer = StringIO()
        predictor = Netflix.MathPredictor(
            {1: 2.5}, {1: 1.0}, {1: 0, 2: 1, 3: -1})
        answer = ConstantPredictor(3)

        Netflix.run(reader, writer, predictor, answer)
        self.assertEqual(writer.getvalue(), "1:\n2.5\n3.5\n1.5\nRMSE: 0.96\n")
Ejemplo n.º 4
0
	def test_parse_training_data_2 (self):
		reader = StringIO.StringIO('1:\n1,1,1111-11-11\n2,2,2222-22-22\n')
		customers = {}
		movie, average = Netflix.parse_training_data(reader, customers)
		self.assert_(movie == '1')
		self.assert_(average == 1.5)
		self.assert_(customers == {'1' : [1, 1], '2' : [2, 1]})
    def test_math_predictor_no_movie(self):
        """ Tests handling invalid movie """
        predictor = Netflix.MathPredictor(
            {1: 2.5}, {1: 1.0}, {1: 0, 2: 1, 3: -1})

        with self.assertRaises(KeyError):
            predictor.predict(1, 2)
    def test_answer_cache(self):
        """ Tests that the AnswerCache returns the actual ratings"""
        cache = Netflix.AnswerCache(answers())

        self.assertEqual(cache.predict(1, 1), 4)
        self.assertEqual(cache.predict(1, 2), 5)
        self.assertEqual(cache.predict(2, 2), 4)
Ejemplo n.º 7
0
def update_recommendations(data, entity, old_prefs):
	REC_LEN = 100
	ACTOR_VAL = 15
	GENRE_VAL = 20
	DIRECTOR_VAL = 25
	WRITER_VAL = 30
	movies_to_add = []
	# preferences
	# clear old prefs from the new pref list
	if entity in old_prefs[0]:
		data['pos'].remove(entity)
	if entity in old_prefs[1]:
		data['neg'].remove(entity)
	
	if entity[0] == 'PERSON':
		person = update_actor(data['imdbi'], entity, data['recs'], ACTOR_VAL)
		# add movies to rec list
		for movieID in person['actor']:
			movie = data['imdbi'].get_movie(movieID, cast_info=False, aka_info=False)
			#print movie['id'],'\t',movie['title']
			if movie['id'] == data['recs'][1][0]:
				break
			movies_to_add.append([movie['id'], movie['title'], ACTOR_VAL])
			####
			# How do I tell between a director and a writer or actor?
			####
		if 'director' in person:
			print person['name'],' as a director!'
		if 'writer' in person:
			print person['name'],' as a writer!'
	elif entity[0] == 'GENRE':
		for movie in data['recs']:
			m = data['imdbi'].get_movie(movie[0], cast_info=False, aka_info=False)
			if 'genres' in m and entity[1] in m['genres']:
				movie[2] += GENRE_VAL
	elif entity[0] == 'MOVIE':
		print 'Movie update start'
		movieEntity = data['imdbi'].get_movie(entity[1])#, cast_info=False, aka_info=False)
		new_movies = Netflix.similar_movies(movieEntity['title'], data['imdbi'])	
		new_movies = rec_get_diff.get_top_x(movieEntity,new_movies)
		for movie,title,rating in data['recs']:
			if movie in new_movies[0]:
				data['recs'].remove([movie,title,rating])
		movies_to_add.extend(new_movies)
				
	# update new stuff with old prefs
	for pref in old_prefs[0]:
		if pref[0] == 'PERSON':
			update_actor(data['imdbi'], pref, movies_to_add, ACTOR_VAL)
		elif pref[0] == 'GENRE':
			for movie in data['recs']:
				m = data['imdbi'].get_movie(movie[0], cast_info=False, aka_info=False)
				if 'genres' in m and entity[1] in m['genres']:
					movie[2] += GENRE_VAL
				
	# CULL
	data['recs'].extend(movies_to_add)
	data['recs'].sort(key=lambda x: x[2], reverse=True)
	del data['recs'][REC_LEN:]
Ejemplo n.º 8
0
def main():
    # Get the path to Netflix data from the command line
    try:
        directory = sys.argv[1]
        directory = os.path.abspath(directory)
    except IndexError:
        print 'Usage: RunNetflix.py path_to_netflix_data'
        exit(1)

    customers = {}
    movies = {}
    precision = 10

    # Get all the files in the training set
    training_set = os.path.join(directory, 'training_set/')
    files = os.listdir(training_set)

    # Parse all the training data
    for file in files:
        file = os.path.join(training_set, file)
        reader = open(file, 'r')
        movie, average = Netflix.parse_training_data(reader, customers)
        reader.close()

        movies[movie] = average
    average = Netflix.average_customer_rating(customers)

    file = os.path.join(directory, 'probe.txt')
    reader = open(file, 'r')
    for line in reader:
        if line[-2] == ':':
            movie = line[:-2]
            print movie + ':'
        else:
            customer = line[:-1]
            rating = Netflix.predict_rating(average, movies[movie],
                                            customers[customer])
            print '{0:.{1}f}'.format(rating, precision)
    reader.close()
Ejemplo n.º 9
0
def main ():
	# Get the path to Netflix data from the command line
	try:
		directory = sys.argv[1]
		directory = os.path.abspath(directory)
	except IndexError:
		print 'Usage: RunNetflix.py path_to_netflix_data'
		exit(1)
	
	customers = {}
	movies = {}
	precision = 10
	
	# Get all the files in the training set
	training_set = os.path.join(directory, 'training_set/')
	files = os.listdir(training_set)

	# Parse all the training data
	for file in files:
		file = os.path.join(training_set, file)
		reader = open(file, 'r')
		movie, average = Netflix.parse_training_data(reader, customers)
		reader.close()

		movies[movie] = average
	average = Netflix.average_customer_rating(customers)

	file = os.path.join(directory, 'probe.txt')
	reader = open(file, 'r')
	for line in reader:
		if line[-2] == ':':
			movie = line[:-2]
			print movie + ':'
		else:
			customer = line[:-1]
			rating = Netflix.predict_rating(average, movies[movie], customers[customer])
			print '{0:.{1}f}'.format(rating, precision)
	reader.close()
Ejemplo n.º 10
0
def main () :
    """
    runs the program. Cache used is determined by flags below
    Default (no flags): use training set to create internal cache (no cache file read or write)
    @flag   -cw     Cache Write: process training set to write cache to external file
    @flag   -cr     Cache Read:  use external cache file (no training set processing)
    @flag   -v      Verbose:  use external cache file (no training set processing)
    """
    
    Netflix.verbose = "-v" in sys.argv
    Netflix.toFile = "-cw" in sys.argv
    
    if "-cr" not in sys.argv :
        Netflix.netflix_learn()
    else :
        Netflix.netflix_get_cache()
    
    Netflix.netflix_eval()
Ejemplo n.º 11
0
	def test_average_customer_rating_3 (self):
		customers = {'1' : [5, 1], '2' : [2, 1], '3' : [3, 1], '4' : [4, 1], '5' : [1, 1]}
		Netflix.average_customer_rating(customers)
		self.assert_(len(customers) == 5)
		self.assert_(customers == {'1' : 5.0, '2' : 2.0, '3' : 3.0, '4' : 4.0, '5': 1.0})
 def test_load_average(self):
     """Test total_average pulls int"""
     Netflix.load_files()
     self.assertNotEqual(Netflix.TOTAL_AVERAGE, 0.0)
Ejemplo n.º 13
0
 def test_eval_3(self):
     Netflix.current_movie = 0
     v = Netflix.netflix_eval(2)
     self.assertEqual(v, 2)
Ejemplo n.º 14
0
 def test_print_2(self):
     w = StringIO()
     Netflix.netflix_print(w, 'abcdefg')
     self.assertEqual(w.getvalue(), "abcdefg\n")
Ejemplo n.º 15
0
 def test_solve_1(self):
     r = StringIO("0:\n0\n")
     w = StringIO()
     Netflix.netflix_solve(r, w, False)
     self.assertEqual(w.getvalue(), "0:\n1.3\nRMSE: 1.7\n")
 def test_load_customer_id(self):
     """ customer_id should still be 0 here"""
     Netflix.load_files()
     self.assertEqual(Netflix.CUSTOMER_ID, 0)
Ejemplo n.º 17
0
 def test_solve_3(self):
     r = StringIO("0:\n2\n")
     w = StringIO()
     Netflix.netflix_solve(r, w, False)
     self.assertEqual(w.getvalue(), "0:\n2.0\nRMSE: 2.0\n")
 def test_load_movie_averages(self):
     """ test movie_year set is modified"""
     Netflix.load_files()
     self.assertNotEqual(Netflix.MOVIE_AVERAGES, {})
 def test_load_movie_id(self):
     """movie_id should still be 0 here"""
     Netflix.load_files()
     self.assertEqual(Netflix.MOVIE_ID, 0)
 def test_load_customer_averages(self):
     """ test movie_year set is modified"""
     Netflix.load_files()
     self.assertNotEqual(Netflix.CUSTOMER_AVERAGES, {})
 def test_load_actual_rating(self):
     """Test total_average pulls int"""
     Netflix.load_files()
     self.assertNotEqual(Netflix.ACTUAL_RATING, {})
 def test_load_movie_years(self):
     """ test movie_year set is modified"""
     Netflix.load_files()
     self.assertNotEqual(Netflix.MOVIE_YEARS, {})
Ejemplo n.º 23
0
	def test_average_customer_rating_4 (self):
		customers = {'1' : [10, 2], '2' : [4, 2], '3' : [6, 2], '4' : [8, 2], '5' : [2, 2]}
		Netflix.average_customer_rating(customers)
		self.assert_(len(customers) == 5)
		self.assert_(customers == {'1' : 5.0, '2' : 2.0, '3' : 3.0, '4' : 4.0, '5': 1.0})
 def test_math_predictor_1(self):
     """ Tests that the MathPredictor does the correct math """
     predictor = Netflix.MathPredictor(
         {1: 2.5}, {1: 1.0}, {1: 0, 2: 1, 3: -1})
     self.assertEqual(predictor.predict(1, 1), 2.5)
Ejemplo n.º 25
0
	def test_average_customer_rating_5 (self):
		customers = {'1' : [7, 2]}
		Netflix.average_customer_rating(customers)
		self.assert_(len(customers) == 1)
		self.assert_(customers == {'1' : 3.5})
Ejemplo n.º 26
0
 def test_eval_1(self):
     Netflix.current_movie = 0
     v = Netflix.netflix_eval(0)
     self.assertEqual(round(v, 2), 1.3)
Ejemplo n.º 27
0
	def test_average_customer_rating_1 (self):
		customers = {'1' : [3, 1]}
		Netflix.average_customer_rating(customers)
		self.assert_(len(customers) == 1)
		self.assert_(customers == {'1' : 3.0})
 def test_print_2(self):
     """Test print2"""
     write = StringIO()
     Netflix.netflix_print(write, 1.3)
     self.assertNotEqual(write.getvalue(), ".3\n")
Ejemplo n.º 29
0
 def test_print_3(self):
     w = StringIO()
     Netflix.netflix_print(w, '\n')
     self.assertEqual(w.getvalue(), "\n\n")
 def test_print_3(self):
     """Test print3"""
     write = StringIO()
     Netflix.netflix_print(write, 1000)
     self.assertEqual(write.getvalue(), "1000\n")
    def test_answer_cache_invalid_user(self):
        """ Tests handling invalid user """
        cache = Netflix.AnswerCache(answers())

        with self.assertRaises(KeyError):
            cache.predict(5, 1)
 def test_print_4(self):
     """Test print4"""
     write = StringIO()
     Netflix.netflix_print(write, 189)
     self.assertNotEqual(write.getvalue(), "9\n")
 def test_read_movie3(self):
     """Test read movie_id"""
     string = "00"
     result = Netflix.netflix_read(string)
     self.assertNotEqual(result, 0)
Ejemplo n.º 34
0
import Netflix
import YouTube
import GMedia
import Outside
import Mainland
import Apple
import ad_lite

print("去广告工作开始")
os.system('python ./ad.py')
print("去广告精简版工作开始")
os.system('python ./ad_lite.py')
print("CMedia工作开始")
CMedia.mainchange()
print("Microsoft工作开始")
Microsoft.change()
print("Netflix工作开始")
Netflix.change()
print("YouTube工作开始")
YouTube.change()
print("GMedia工作开始")
GMedia.mainchange()
print("Outside工作开始")
Outside.mainchange()
print("Mainland工作开始")
Mainland.mainchange()
print("Apple工作开始")
Apple.mainchange()

print("所有工作都完成")
 def test_math_predictor_2(self):
     """ Tests that the MathPredictor does the correct math with adjustments """
     predictor = Netflix.MathPredictor(
         {1: 2.5}, {1: 1.0}, {1: 0, 2: 1, 3: -1})
     self.assertEqual(predictor.predict(2, 1), 3.5)
 def test_read_customer4(self):
     """Test read customer_id"""
     string = "10\n"
     result = Netflix.netflix_read(string)
     self.assertNotEqual(result, 0)