-
Notifications
You must be signed in to change notification settings - Fork 0
/
datainteraction.py
171 lines (153 loc) · 6.16 KB
/
datainteraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
This database interaction function has been written by:
Lucas Fijen and Arie Soeteman
As part of an assignment for fuzzy logic at the University of Amsterdam
"""
import data as d
class Datainteraction:
""" Interaction class for data between validation and training"""
def __init__(self):
""" Inits the datainteraction class, makes a validation and training
set """
self.val = d.Moviedata('valcomposers',
'valdirectors',
'valactors',
'valbusiness',
'valspecial-effects-companies',
'valratings')
self.train = d.Moviedata('composers',
'directors',
'actors',
'business',
'special-effects-companies',
'ratings')
self.errorvalue = -1
def actor_values(self, movie, ranging=0):
""" returns average amount of movies starred by the actors,
and the average ratings of these movies """
actors = self.val.movie_actor(movie)
results = []
for actor in actors:
movies = self.train.actor_movie(actor)
if len(movies) == 0:
continue
avgrating = 0
amount = 0
for movie in movies:
temprate = self.train.movie_rating(movie)
if temprate >= 1:
avgrating += temprate
amount += 1
if amount > 0:
results.append((avgrating / amount, amount))
if len(results) == 0:
return self.errorvalue, self.errorvalue
if ranging == 0 or ranging > len(results):
ranging = len(results)
amounts = [i[1] for i in results]
amounts.sort(reverse=True)
ratings = [i[0] for i in results]
ratings.sort(reverse=True)
rating = sum(ratings[:ranging]) / ranging
amount = sum(amounts[:ranging]) / ranging
return rating, amount
def special_values(self, movie, ranging=0):
""" returns average amount of movies done by special effect studio,
and the average ratings of these movies """
specials = self.val.movie_special(movie)
results = []
for special in specials:
movies = self.train.special_movie(special)
if len(movies) == 0:
continue
avgrating = 0
amount = 0
for movie in movies:
temprate = self.train.movie_rating(movie)
if temprate >= 1:
avgrating += temprate
amount += 1
if amount > 0:
results.append((avgrating / amount, amount))
if len(results) == 0:
return self.errorvalue, self.errorvalue
if ranging == 0 or ranging > len(results):
ranging = len(results)
amounts = [i[1] for i in results]
amounts.sort(reverse=True)
ratings = [i[0] for i in results]
ratings.sort(reverse=True)
rating = sum(ratings[:ranging]) / ranging
amount = sum(amounts[:ranging]) / ranging
return rating, amount
def director_values(self, movie, ranging=0):
""" returns average amount of movies created by director
and the average ratings of these movies """
directors = self.val.movie_director(movie)
results = []
for director in directors:
movies = self.train.director_movie(director)
if len(movies) == 0:
continue
avgrating = 0
amount = 0
for movie in movies:
temprate = self.train.movie_rating(movie)
if temprate >= 1:
avgrating += temprate
amount += 1
if amount > 0:
results.append((avgrating / amount, amount))
if len(results) == 0:
return self.errorvalue, self.errorvalue
if ranging == 0 or ranging > len(results):
ranging = len(results)
amounts = [i[1] for i in results]
amounts.sort(reverse=True)
ratings = [i[0] for i in results]
ratings.sort(reverse=True)
rating = sum(ratings[:ranging]) / ranging
amount = sum(amounts[:ranging]) / ranging
return rating, amount
def composer_values(self, movie, ranging=0):
""" returns average amount of movies with music created by composer
and the average ratings of these movies """
composers = self.val.movie_composer(movie)
results = []
for composer in composers:
movies = self.train.composer_movie(composer)
if len(movies) == 0:
continue
avgrating = 0
amount = 0
for movie in movies:
temprate = self.train.movie_rating(movie)
if temprate >= 1:
avgrating += temprate
amount += 1
if amount > 0:
results.append((avgrating / amount, amount))
if len(results) == 0:
return self.errorvalue, self.errorvalue
if ranging == 0 or ranging > len(results):
ranging = len(results)
amounts = [i[1] for i in results]
amounts.sort(reverse=True)
ratings = [i[0] for i in results]
ratings.sort(reverse=True)
rating = sum(ratings[:ranging]) / ranging
amount = sum(amounts[:ranging]) / ranging
return rating, amount
def get_budget(self, movie):
""" returns budget of a movie in milions"""
budget = self.val.movie_budget(movie)
return budget
def get_rating(self, movie):
""" returns the rating of a movie """
return self.val.movie_rating(movie)
def get_all_movies(self):
""" returns all movies that appear in the validation data """
return self.val.get_movies()
#datainter = Datainteraction()
#print(datainter.special_values('Avengers: Age of Ultron', ranging=3))
#print(datainter.get_budget('Avengers: Age of Ultron'))