def test_movies_given_year(self): g = Graph.Graph() movie1 = Graph.Movie("a", 1990, 10.0, ["A"], 0) g.add_vertex(movie1) movie2 = Graph.Movie("b", 1930, 20.0, ["A"], 1) g.add_vertex(movie2) movie3 = Graph.Movie("c", 1960, 5.0, ["B"], 2) g.add_vertex(movie3) self.assertEqual(g.list_movies_given_year(1990), ["a"])
def test_oldest_actors(self): g = Graph.Graph() actor1 = Graph.Actor("A", 55, ["a", "b"], 0) g.add_vertex(actor1) actor2 = Graph.Actor("B", 44, ["c", "d"], 1) g.add_vertex(actor2) actor3 = Graph.Actor("C", 33, ["a", "b"], 2) g.add_vertex(actor3) actor4 = Graph.Actor("D", 22, ["c", "d"], 3) g.add_vertex(actor4) self.assertEqual(g.list_oldest_actors(3), [("A", 55), ("B", 44), ("C", 33)])
def parse_json_to_graph(file): """ Convert json file into a graph :param file: String of file name :return: graph data structure """ data = read_json(file) actors = data[0] movies = data[1] g = Graph.Graph() g.adj_matrix = np.zeros((len(actors), len(movies))) actor_count = 0 for actor_name in actors: a = actors[actor_name] actor = Graph.Actor(actor_name, a['age'], a['total_gross'], a['movies'], actor_count) g.add_vertex(actor) actor_count += 1 movie_count = 0 for movie_name in movies: m = movies[movie_name] movie = Graph.Movie(movie_name, m['year'], m['box_office'], m['actors'], movie_count) g.add_vertex(movie) movie_count += 1 for v in g.vertices: vertex = g.vertices.get(v) if isinstance(vertex, Graph.Actor): a = vertex.id for f in vertex.films: movie = g.vertices.get(f) if movie is not None and isinstance(movie, Graph.Movie): m = movie.id if len(movie.cast) is not 0: g.adj_matrix[a][m] = int(movie.grossing / len(movie.cast)) else: g.adj_matrix[a][m] = 0 return g
def create_movie(): try: name = request.json['name'] if movies.get(name) is not None: return Response("Already Exists\n", status=400) year = request.json['year'] box_office = request.json['box_office'] casts = request.json['actors'] movie = Graph.Movie(name, year, box_office, casts, len(movies)) except KeyError: return Response("Missing Arguments\n", status=400) graph.add_vertex(movie) movies[name] = movie.serialize() return Response("Movie Created\n", status=201)
def create_actor(): try: name = request.json['name'] if actors.get(name) is not None: return Response("Already Exists\n", status=400) age = request.json['age'] total_gross = request.json['total_gross'] films = request.json['movies'] actor = Graph.Actor(name, age, total_gross, films, len(actors)) except KeyError: return Response("Missing Arguments\n", status=400) graph.add_vertex(actor) actors[name] = actor.serialize() return Response("Actor Created\n", status=201)
def test_actors_given_year(self): g = Graph.Graph() actor1 = Graph.Actor("A", 55, ["a", "b"], 0) g.add_vertex(actor1) actor2 = Graph.Actor("B", 44, ["c", "d"], 1) g.add_vertex(actor2) actor3 = Graph.Actor("C", 33, ["a", "b"], 2) g.add_vertex(actor3) movie1 = Graph.Movie("a", 1991, 10.0, ["A"], 3) g.add_vertex(movie1) movie2 = Graph.Movie("b", 1990, 20.0, ["C"], 4) g.add_vertex(movie2) movie3 = Graph.Movie("c", 1960, 5.0, ["B"], 5) g.add_vertex(movie3) movie4 = Graph.Movie("d", 1970, 10.0, ["B"], 6) g.add_vertex(movie4) self.assertEqual(g.list_actors_given_year(1990), ["C"])
def test_top_grossing_actors(self): g = Graph.Graph() actor1 = Graph.Actor("A", 28, ["a", "b"], 0) g.add_vertex(actor1) actor2 = Graph.Actor("B", 28, ["c", "d"], 1) g.add_vertex(actor2) movie1 = Graph.Movie("a", 1960, 10.0, ["A"], 2) g.add_vertex(movie1) movie2 = Graph.Movie("b", 1960, 20.0, ["A"], 3) g.add_vertex(movie2) movie3 = Graph.Movie("c", 1960, 5.0, ["B"], 4) g.add_vertex(movie3) movie4 = Graph.Movie("d", 1960, 10.0, ["B"], 5) g.add_vertex(movie4) self.assertEqual(g.list_top_grossing_actors(2), [("A", 30.0), ("B", 15.0)])
def test_add_vertex(self): g = Graph.Graph() actor = Graph.Actor("Jeremy", 28, ["a", "b"], 0) g.add_vertex(actor) self.assertEqual(g.vertices.get("Jeremy"), actor)
def test_get_film_cast(self): g = Graph.Graph() movie = Graph.Movie("Gone with the wind", 1960, 20.0, ["a", "b"], 0) g.add_vertex(movie) self.assertEqual(g.get_film_cast("Gone with the wind"), ["a", "b"])
def test_get_actor_filmography(self): g = Graph.Graph() actor = Graph.Actor("Jeremy", 28, ["a", "b"], 0) g.add_vertex(actor) self.assertEqual(g.get_actor_filmography("Jeremy"), ["a", "b"])
def test_get_movie_grossing(self): g = Graph.Graph() movie = Graph.Movie("Gone with the wind", 1960, 20.0,["a", "b"], 0) g.add_vertex(movie) self.assertEqual(g.get_movie_grossing("Gone with the wind"), 20.0)
def main(): g = Graph.Graph() with open('actor_output.json') as json_data: actors = json.load(json_data) with open('movie_output.json') as json_data: movies = json.load(json_data) for a in actors: json_a = json.loads(a) digitized_age = None if json_a['age'] is not None: digitized_age = int(json_a['age']) actor = Graph.Actor(json_a['name'], digitized_age, json_a['films'], g.num_vertices) g.add_vertex(actor) if g.num_vertices >= 250: break for m in movies: json_m = json.loads(m) digitized_year = None if json_m['year'] is not None: match = re.search('\d{4}', json_m['year']) if match: digitized_year = int(match.group(0)) digitized_grossing = None if json_m['Grossing'] is not None: match = re.search('million', json_m['Grossing']) if match: temp1 = json_m['Grossing'].replace("million", "") digitized_grossing = temp1.replace("$", "").split()[0] movie = Graph.Movie(json_m['title'], digitized_year, digitized_grossing, json_m['cast'], g.num_vertices) g.add_vertex(movie) if g.num_vertices >= 375: break for v in g.vertices: vertex = g.vertices.get(v) if isinstance(vertex, Graph.Actor): r = vertex.id for f in vertex.films: if g.vertices.get(f) is not None: c = g.vertices.get(f).id g.adj_matrix[r][c] = 1 else: r = vertex.id for c in vertex.cast: if g.vertices.get(c) is not None: c = g.vertices.get(c).id g.adj_matrix[r][c] = 1 # g.get_movie_grossing("Sleepers") # g.get_actor_filmography("Morgan Freeman") # g.get_film_cast("Sleepers") # g.list_oldest_actors(3) # g.list_movies_given_year(2005) # g.list_actors_given_year(2005) # g.list_top_grossing_actors(5) # for i in range(0, 275): # total = sum(g.adj_matrix[i]) # if total > 0: # print("sum of column", i, "is", total) print("Please enter the number of queries you want to run(1~7):") user_input = input() while user_input != 'exit': if user_input == '1': print( "To find how much a movie has grossed, please type the title:") user_input = input() g.get_movie_grossing(user_input) if user_input == '2': print( "To find which movies an actor has worked in, please type the name of an actor:" ) user_input = input() g.get_actor_filmography(user_input) if user_input == '3': print( "To find which actors worked in a movie, please type the title:" ) user_input = input() g.get_film_cast(user_input) if user_input == '4': print( "To find the top X actors with the most total grossing value, please type the number:" ) user_input = input() g.list_top_grossing_actors(int(user_input)) if user_input == '5': print("To find the oldest X actors, please type the number:") user_input = input() g.list_oldest_actors(int(user_input)) if user_input == '6': print( "To find all the movies for a given year, please type the year:" ) user_input = input() g.list_movies_given_year(int(user_input)) if user_input == '7': print( "To find all the actors for a given year, please type the year:" ) user_input = input() g.list_actors_given_year(int(user_input)) print("Please enter the number of queries you want to run(1~7):") user_input = input()