-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.py
166 lines (131 loc) · 4.27 KB
/
Model.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
import random
class ModelMeta(type):
def __new__(cls, name, bases, attrs):
new_class = super(ModelMeta, cls).__new__(cls, name, bases, attrs)
new_class.all_items = []
return new_class
class Model(metaclass=ModelMeta):
def __init__(self, iid):
self.iid = iid
self.contains = []
@classmethod
def create(cls):
count = len(cls.all_items)
i = cls(count)
cls.all_items.append(i)
return i
@classmethod
def counts(cls):
return len(cls.all_items)
def tracks(self):
return self.contains
@classmethod
def print_hypergraph(cls):
file = open(cls.__name__+"_hypergraph.txt", 'w')
# for i in cls.all_items:
# print(i.iid, end="\t", file=file)
# for t in i.tracks():
# print(t.iid, end=",", file=file)
# print(file=file)
for i in cls.all_items:
for t in i.tracks():
print(t.iid, i.iid, sep="\t", file=file)
# def print_edge_pair(self):
# with open(self.__class__.__name__+"_edgepair.txt", 'a') as f_out:
# for t in self.tracks():
# f_out.write(str(self.iid))
# f_out.write("\t")
# f_out.write(str(t.iid))
# f_out.write("\n")
# # print(self.pid, t.tid, sep="\t")
#
# def print_hypergraph(self):
# with open(self.__class__.__name__+"_hypergraph.txt", 'a') as f_out:
# # print(self.pid, end=":\t")
# f_out.write(str(self.iid))
# f_out.write("\t")
# for t in self.tracks():
# f_out.write(str(t.iid))
# f_out.write(",")
# f_out.write("\n")
class Genre(Model):
def tracks(self):
result = []
for album in self.contains:
result += album.contains
return result
class Artist(Model):
def tracks(self):
result = []
for album in self.contains:
result += album.contains
return result
class Album(Model):
def __init__(self, album_id):
super().__init__(album_id)
self.artist = None
self.genre = None
def added_to_artist(self, artist):
self.artist = artist
artist.contains.append(self)
def added_to_genre(self, genre):
self.genre = genre
genre.contains.append(self)
class Track(Model):
def __init__(self, iid):
super().__init__(iid)
self.album = None
self.playlists = []
self.f1 = None
self.f2 = None
self.f3 = None
def added_to_album(self, album):
self.album = album
album.contains.append(self)
self.f1 = Feature1.chooseRandom() # feature value is arbitrarily random
self.f1.contains.append(self)
self.f2 = Feature2.chooseRandom() # feature value is arbitrarily random
self.f2.contains.append(self)
self.f3 = Feature3.chooseRandom() # feature value is arbitrarily random
self.f3.contains.append(self)
def added_to_playlist(self, p):
self.playlists.append(p)
p.contains.append(self)
def number_of_appearance(self):
return len(self.playlists)
@classmethod
def print_playlist(cls):
file = open("Track_playlistList.txt", 'w')
for t in cls.all_items:
print(t.iid, end="\t", file=file)
for p in t.playlists:
print(p.iid, end=",", file=file)
print(file=file)
class Playlist(Model):
def shuffle(self):
random.shuffle(self.contains)
def add_track(self, track):
if track in self.contains:
return False
else:
track.added_to_playlist(self)
return True
class Feature(Model):
@classmethod
def createFeatures(cls):
for _ in range(random.choice(range(3, 10))):
cls.create()
@classmethod
def chooseRandom(cls):
return random.choice(cls.all_items)
@classmethod
def print_result(cls, file):
print(cls.__name__, file=file)
for f in cls.all_items:
print("\tvalue {}:\t{} tracks".format(f.iid, len(f.tracks())), file=file)
class Feature1(Feature):
pass
class Feature2(Feature):
pass
class Feature3(Feature):
pass