recs
This commit is contained in:
parent
dd63ddc9ce
commit
38578e007f
34
engine.py
34
engine.py
@ -4,45 +4,45 @@ FS = FuzzySystem()
|
|||||||
|
|
||||||
# Define fuzzy sets for the variable
|
# Define fuzzy sets for the variable
|
||||||
# RELEASE_YEAR
|
# RELEASE_YEAR
|
||||||
release_year_newer = TriangleFuzzySet(-68, -68, 0, term="newer")
|
release_year_newer = TriangleFuzzySet(-20, -20, 0, term="newer")
|
||||||
release_year_similar = TriangleFuzzySet(-68, 0, 68, term="similar")
|
release_year_similar = TriangleFuzzySet(-20, 0, 20, term="similar")
|
||||||
release_year_older = TriangleFuzzySet(0, 68, 68, term="older")
|
release_year_older = TriangleFuzzySet(0, 20, 20, term="older")
|
||||||
|
|
||||||
FS.add_linguistic_variable("RELEASE_YEAR",
|
FS.add_linguistic_variable("RELEASE_YEAR",
|
||||||
LinguisticVariable([release_year_newer, release_year_similar, release_year_older],
|
LinguisticVariable([release_year_newer, release_year_similar, release_year_older],
|
||||||
universe_of_discourse=[-136, 136]))
|
universe_of_discourse=[-136, 136]))
|
||||||
|
|
||||||
# RUNTIME
|
# RUNTIME
|
||||||
runtime_shorter = TriangleFuzzySet(-238, -238, 0, term="shorter")
|
runtime_shorter = TriangleFuzzySet(-90, -90, 0, term="shorter")
|
||||||
runtime_similar = TriangleFuzzySet(-238, 0, 238, term="similar")
|
runtime_similar = TriangleFuzzySet(-90, 0, 90, term="similar")
|
||||||
runtime_longer = TriangleFuzzySet(0, 238, 238, term="longer")
|
runtime_longer = TriangleFuzzySet(0, 90, 90, term="longer")
|
||||||
|
|
||||||
FS.add_linguistic_variable("RUNTIME", LinguisticVariable([runtime_shorter, runtime_similar, runtime_longer],
|
FS.add_linguistic_variable("RUNTIME", LinguisticVariable([runtime_shorter, runtime_similar, runtime_longer],
|
||||||
universe_of_discourse=[-476, 476]))
|
universe_of_discourse=[-476, 476]))
|
||||||
|
|
||||||
# SEASONS
|
# SEASONS
|
||||||
seasons_less = TriangleFuzzySet(-42, -42, 0, term="less")
|
seasons_less = TriangleFuzzySet(-5, -5, 0, term="less")
|
||||||
seasons_similar = TriangleFuzzySet(-42, 0, 42, term="similar")
|
seasons_similar = TriangleFuzzySet(-5, 0, 5, term="similar")
|
||||||
seasons_more = TriangleFuzzySet(0, 42, 42, term="more")
|
seasons_more = TriangleFuzzySet(0, 5, 5, term="more")
|
||||||
|
|
||||||
FS.add_linguistic_variable("SEASONS", LinguisticVariable([seasons_less, seasons_similar, seasons_more],
|
FS.add_linguistic_variable("SEASONS", LinguisticVariable([seasons_less, seasons_similar, seasons_more],
|
||||||
universe_of_discourse=[-84, 84]))
|
universe_of_discourse=[-84, 84]))
|
||||||
|
|
||||||
# GENRES
|
# GENRES
|
||||||
genres_different = TriangleFuzzySet(-100, -100, 0, term="different")
|
genres_different = TriangleFuzzySet(0, 0, 0.5, term="different")
|
||||||
genres_similar = TriangleFuzzySet(-100, 0, 100, term="similar")
|
genres_similar = TriangleFuzzySet(0, 0.5, 1, term="similar")
|
||||||
genres_same = TriangleFuzzySet(0, 100, 100, term="same")
|
genres_same = TriangleFuzzySet(0.5, 1, 1, term="same")
|
||||||
|
|
||||||
FS.add_linguistic_variable("GENRES", LinguisticVariable([genres_different, genres_similar, genres_same],
|
FS.add_linguistic_variable("GENRES", LinguisticVariable([genres_different, genres_similar, genres_same],
|
||||||
universe_of_discourse=[-200, 200]))
|
universe_of_discourse=[0, 1]))
|
||||||
|
|
||||||
# EMOTIONS
|
# EMOTIONS
|
||||||
emotions_different = TriangleFuzzySet(-4, -4, 0, term="different")
|
emotions_different = TriangleFuzzySet(0, 0, 0.5, term="different")
|
||||||
emotions_similar = TriangleFuzzySet(-4, 0, 4, term="similar")
|
emotions_similar = TriangleFuzzySet(0, 0.5, 1, term="similar")
|
||||||
emotions_same = TriangleFuzzySet(0, 4, 4, term="same")
|
emotions_same = TriangleFuzzySet(0.5, 1, 1, term="same")
|
||||||
|
|
||||||
FS.add_linguistic_variable("EMOTIONS", LinguisticVariable([emotions_different, emotions_similar, emotions_same],
|
FS.add_linguistic_variable("EMOTIONS", LinguisticVariable([emotions_different, emotions_similar, emotions_same],
|
||||||
universe_of_discourse=[-8, 8]))
|
universe_of_discourse=[0, 1]))
|
||||||
|
|
||||||
# RECOMMENDATION
|
# RECOMMENDATION
|
||||||
low_recommendation = TriangleFuzzySet(0, 0, 50, term="low_recommendation")
|
low_recommendation = TriangleFuzzySet(0, 0, 50, term="low_recommendation")
|
||||||
|
55
main.py
55
main.py
@ -18,6 +18,32 @@ app = FastAPI()
|
|||||||
data = pd.DataFrame()
|
data = pd.DataFrame()
|
||||||
|
|
||||||
|
|
||||||
|
def inference(first_id: str, second_id: str):
|
||||||
|
first = data.loc[first_id]
|
||||||
|
second = data.loc[second_id]
|
||||||
|
|
||||||
|
year_diff = int(first['release_year'] - second['release_year'])
|
||||||
|
FS.set_variable('RELEASE_YEAR', year_diff)
|
||||||
|
|
||||||
|
runtime_diff = int(first['runtime'] - second['runtime'])
|
||||||
|
FS.set_variable('RUNTIME', runtime_diff)
|
||||||
|
|
||||||
|
if not (np.isnan(first['seasons']) or np.isnan(second['seasons'])):
|
||||||
|
season_diff = int(first['seasons'] - second['seasons'])
|
||||||
|
FS.set_variable('SEASONS', season_diff)
|
||||||
|
else:
|
||||||
|
FS.set_variable('SEASONS', 0)
|
||||||
|
|
||||||
|
genre_diff = 1 - cosine(first['genres'], second['genres'])
|
||||||
|
FS.set_variable('GENRES', genre_diff)
|
||||||
|
|
||||||
|
emotion_diff = 1 - cosine(first['emotions'], second['emotions'])
|
||||||
|
FS.set_variable('EMOTIONS', emotion_diff)
|
||||||
|
|
||||||
|
return FS.inference(['RECOMMENDATION'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.on_event('startup')
|
@app.on_event('startup')
|
||||||
async def startup_event():
|
async def startup_event():
|
||||||
global data
|
global data
|
||||||
@ -40,22 +66,23 @@ def rec_score(first_id: str, second_id: str):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
return {'error': f'{second_id} is not a valid id'}
|
return {'error': f'{second_id} is not a valid id'}
|
||||||
|
|
||||||
year_diff = int(first['release_year'] - second['release_year'])
|
return inference(first_id, second_id)
|
||||||
FS.set_variable('RELEASE_YEAR', year_diff)
|
|
||||||
|
|
||||||
runtime_diff = int(first['runtime'] - second['runtime'])
|
|
||||||
FS.set_variable('RUNTIME', runtime_diff)
|
|
||||||
|
|
||||||
if not (np.isnan(first['seasons']) or np.isnan(second['seasons'])):
|
@app.get('/recs/{production_id}')
|
||||||
season_diff = int(first['seasons'] - second['seasons'])
|
async def recs(production_id: str, count: int | None):
|
||||||
FS.set_variable('SEASONS', season_diff)
|
try:
|
||||||
else:
|
first = data.loc[production_id]
|
||||||
FS.set_variable('SEASONS', 0)
|
except KeyError:
|
||||||
|
return {'error': f'{production_id} is not a valid id'}
|
||||||
|
|
||||||
genre_diff = 1 - cosine(first['genres'], second['genres'])
|
scores = []
|
||||||
FS.set_variable('GENRES', genre_diff)
|
|
||||||
|
|
||||||
emotion_diff = 1 - cosine(first['emotions'], second['emotions'])
|
for index, row in data.iterrows():
|
||||||
FS.set_variable('EMOTIONS', emotion_diff)
|
if str(index) == production_id:
|
||||||
|
continue
|
||||||
|
scores.append((index, inference(production_id, str(index))['RECOMMENDATION']))
|
||||||
|
|
||||||
return FS.inference(['RECOMMENDATION'])
|
scores = [idx[0] for idx in sorted(scores, key=lambda x: x[1], reverse=True)[:count]]
|
||||||
|
|
||||||
|
return list(scores)
|
||||||
|
Loading…
Reference in New Issue
Block a user