-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetdistance.py
64 lines (54 loc) · 1.85 KB
/
getdistance.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
from pymongo import MongoClient
import sys
def directordistance(a, b, dbh):
if a['director'] == b['director']:
return 0
else:
return 1
def actordistance(a, b, dbh):
return 1
def comparegenre(a, b, dbh):#get genre b's distance from a
total = 0
mindist = 100
refgenre = dbh.genrecounts.find_one({'type': a})
for key in refgenre:
if key != '_id' and key != 'type':
total += refgenre[key]
if refgenre[b] != 0:
mindist = refgenre[b] / total
# print(a, b, 1 / (mindist * 100))
return 5 / (mindist * 100)#return the percentage of ratio
def genredistance(a, b, dbh):#movie A movie B
totaldist = 0
try:
for genre in b['genre']:
totaldistance = 0
count = 0
if genre not in a['genre']:
for ref in a['genre']:
totaldistance += comparegenre(ref, genre, dbh)#genre a to genre b
count += 1
if count != 0:
totaldist += (totaldistance / count)
except:
return 100000
return totaldist
def getratingdistance(a, dbh):
return 30 / a['rating']
def totaldistance(a, b, dbh):
return genredistance(a, b, dbh) + getratingdistance(b, dbh)
def main():
try:
c = MongoClient(host='localhost', port=27018)
dbh = c['moviedb']
except:
print("Failed to connect to database!")
comparegenre('드라마', '멜로/로맨스', dbh)
genredistance(dbh.movielist.find_one({'code': '74866'}), dbh.movielist.find_one({'code': '142822'}), dbh)
checklist = list(dbh.movielist.find({'valid': True, 'code': {'$ne': '127459'}}))
moviea = dbh.movielist.find_one({'code': '127459'})
checklist.sort(key=lambda x: totaldistance(moviea, x, dbh))
for movie in checklist[:20]:
print(movie['title'])
if __name__ == '__main__':
main()