-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.py
More file actions
112 lines (89 loc) · 3.76 KB
/
Copy pathalgorithm.py
File metadata and controls
112 lines (89 loc) · 3.76 KB
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
import json
import random
import streamlit as st
from itertools import cycle
import login
import animeInfo
# given the user's anime list, choose a random anime to pull the recommendations from
def randomChooseAnime(animeList):
animeArray = []
for x in animeList.keys():
animeArray.append(x)
chosenAnime = random.choice(animeArray)
return chosenAnime
# with the chosen anime, go to that genre and pull the number of anime the user wants recommended
def getAnimeFromGenre(animeList, chosenAnime, numberOfAnime):
genres = animeList[chosenAnime][1]
chosenGenre = random.choice(genres)
animeByGenre = open("get_anime_query.json")
data = json.load(animeByGenre)
listOfAnime = data[chosenGenre.lower()]
listOfAnime = random.sample(listOfAnime, numberOfAnime)
return listOfAnime
# the algorithm that determines what anime to recommend based on a points system
def recommendationAlgorithm(listOfAnime, username):
top10Anime = listOfAnime
animeStudio = dict()
# getting the studios from each anime and putting into a dictionary
# dict() for animeID -> num of Studios overlap
studiosOverlap = dict()
for studio in top10Anime:
try:
entry = animeInfo.getAnimeTag(studio)
except TypeError:
return
animeStudio[studio] = entry
# initialize overlaps to 0
studiosOverlap[studio] = 0
# collecting the tags of each anime
tags = dict()
for x in top10Anime:
try:
animeTags = animeInfo.getAnimeTag(x)
except TypeError:
return
tagArray = []
for tag in animeTags:
tagArray.append(tag["name"])
tags[x] = tagArray
# gets all the studios and tags from the animeList of the user
userStudios = animeInfo.getStudio(login.getUserAnimeList(login.getUserID(username)))
usersTags = animeInfo.getTags(login.getUserStats(username))
# algorithm: animeScore = studioMatching + tagMatching
# tagMatching = tagNum / 2
# initalizing the dicionary
animeRanking = dict()
for x in animeStudio.keys():
animeRanking[x] = 0
# get the number of same studios
for studio in userStudios:
for key, value in animeStudio.items():
if studio in value:
animeRanking[key] = animeRanking[key] + 1
# get the number of same tags
for x in usersTags.keys():
for key, value in tags.items():
if x in value:
importance = usersTags[x]
currentRanking = animeRanking[key] + importance
animeRanking[key] = currentRanking
file = open("anime_by_id.json")
animeName = json.load(file)
animeRanking = sorted(animeRanking.items(), key=lambda x: x[1], reverse=True)
# loads the images with the anime name
animeImagewWithName = dict()
for anime in animeRanking:
image = animeName[anime[0]]["cover_image"]
if animeName[anime[0]]["name_english"] is None:
animeImagewWithName[animeName[anime[0]]["name_romaji"]] = image
else:
animeImagewWithName[animeName[anime[0]]["name_english"]] = image
# loads the image in streamlit front end
filteredImages = list(animeImagewWithName.values())
caption = list(animeImagewWithName.keys())
cols = cycle(st.columns(4))
for idx, filteredImage in enumerate(filteredImages):
next(cols).image(filteredImage, width=150, caption=caption[idx])
def algo(username, numOfRecs):
animeList = animeInfo.getUserAnime(login.getUserAnimeList(login.getUserID(username)))
recommendationAlgorithm(getAnimeFromGenre(animeList, randomChooseAnime(animeList), int(numOfRecs)), username)