diff --git a/engine.py b/engine.py index 9292ef6..43289c1 100644 --- a/engine.py +++ b/engine.py @@ -4,45 +4,45 @@ FS = FuzzySystem() # Define fuzzy sets for the variable # RELEASE_YEAR -release_year_newer = TriangleFuzzySet(-68, -68, 0, term="newer") -release_year_similar = TriangleFuzzySet(-68, 0, 68, term="similar") -release_year_older = TriangleFuzzySet(0, 68, 68, term="older") +release_year_newer = TriangleFuzzySet(-20, -20, 0, term="newer") +release_year_similar = TriangleFuzzySet(-20, 0, 20, term="similar") +release_year_older = TriangleFuzzySet(0, 20, 20, term="older") FS.add_linguistic_variable("RELEASE_YEAR", LinguisticVariable([release_year_newer, release_year_similar, release_year_older], universe_of_discourse=[-136, 136])) # RUNTIME -runtime_shorter = TriangleFuzzySet(-238, -238, 0, term="shorter") -runtime_similar = TriangleFuzzySet(-238, 0, 238, term="similar") -runtime_longer = TriangleFuzzySet(0, 238, 238, term="longer") +runtime_shorter = TriangleFuzzySet(-90, -90, 0, term="shorter") +runtime_similar = TriangleFuzzySet(-90, 0, 90, term="similar") +runtime_longer = TriangleFuzzySet(0, 90, 90, term="longer") FS.add_linguistic_variable("RUNTIME", LinguisticVariable([runtime_shorter, runtime_similar, runtime_longer], universe_of_discourse=[-476, 476])) # SEASONS -seasons_less = TriangleFuzzySet(-42, -42, 0, term="less") -seasons_similar = TriangleFuzzySet(-42, 0, 42, term="similar") -seasons_more = TriangleFuzzySet(0, 42, 42, term="more") +seasons_less = TriangleFuzzySet(-5, -5, 0, term="less") +seasons_similar = TriangleFuzzySet(-5, 0, 5, term="similar") +seasons_more = TriangleFuzzySet(0, 5, 5, term="more") FS.add_linguistic_variable("SEASONS", LinguisticVariable([seasons_less, seasons_similar, seasons_more], universe_of_discourse=[-84, 84])) # GENRES -genres_different = TriangleFuzzySet(-100, -100, 0, term="different") -genres_similar = TriangleFuzzySet(-100, 0, 100, term="similar") -genres_same = TriangleFuzzySet(0, 100, 100, term="same") +genres_different = TriangleFuzzySet(0, 0, 0.5, term="different") +genres_similar = TriangleFuzzySet(0, 0.5, 1, term="similar") +genres_same = TriangleFuzzySet(0.5, 1, 1, term="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_different = TriangleFuzzySet(-4, -4, 0, term="different") -emotions_similar = TriangleFuzzySet(-4, 0, 4, term="similar") -emotions_same = TriangleFuzzySet(0, 4, 4, term="same") +emotions_different = TriangleFuzzySet(0, 0, 0.5, term="different") +emotions_similar = TriangleFuzzySet(0, 0.5, 1, term="similar") +emotions_same = TriangleFuzzySet(0.5, 1, 1, term="same") FS.add_linguistic_variable("EMOTIONS", LinguisticVariable([emotions_different, emotions_similar, emotions_same], - universe_of_discourse=[-8, 8])) + universe_of_discourse=[0, 1])) # RECOMMENDATION low_recommendation = TriangleFuzzySet(0, 0, 50, term="low_recommendation") diff --git a/main.py b/main.py index 711f70f..5fa6e27 100644 --- a/main.py +++ b/main.py @@ -18,6 +18,32 @@ app = FastAPI() 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') async def startup_event(): global data @@ -40,22 +66,23 @@ def rec_score(first_id: str, second_id: str): except KeyError: return {'error': f'{second_id} is not a valid id'} - year_diff = int(first['release_year'] - second['release_year']) - FS.set_variable('RELEASE_YEAR', year_diff) + return inference(first_id, second_id) - 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) +@app.get('/recs/{production_id}') +async def recs(production_id: str, count: int | None): + try: + first = data.loc[production_id] + except KeyError: + return {'error': f'{production_id} is not a valid id'} - genre_diff = 1 - cosine(first['genres'], second['genres']) - FS.set_variable('GENRES', genre_diff) + scores = [] - emotion_diff = 1 - cosine(first['emotions'], second['emotions']) - FS.set_variable('EMOTIONS', emotion_diff) + for index, row in data.iterrows(): + 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)