Skip to content

Instantly share code, notes, and snippets.

@emrepun
Last active December 5, 2019 22:59
Show Gist options
  • Save emrepun/9d946bbdbc58e3bc93bb79bc9c3572d6 to your computer and use it in GitHub Desktop.
Save emrepun/9d946bbdbc58e3bc93bb79bc9c3572d6 to your computer and use it in GitHub Desktop.
engine_gist_3.py
import numpy as np
import pandas as pd
from cosine_similarity import CosineSimilarity
import operator
import json
class RecommenderEngine:
def __init__(self):
print("engine initialized")
def get_recommendations(keywords):
df = pd.read_csv('city_data_cleared.csv')
score_dict = {}
for index, row in df.iterrows():
score_dict[index] = CosineSimilarity.cosine_similarity_of(row['description'], keywords)
#sort cities by score and index.
sorted_scores = sorted(score_dict.items(), key=operator.itemgetter(1), reverse=True)
counter = 0
#create an empty results data frame.
resultDF = pd.DataFrame(columns=('city', 'popularity', 'description', 'score'))
#get highest scored 5 cities.
for i in sorted_scores:
#print index and score of the city.
#print(i[0], i[1])
resultDF = resultDF.append({'city': df.iloc[i[0]]['city'], 'popularity': df.iloc[i[0]]['popularity'], 'description': df.iloc[i[0]]['description'], 'score': i[1]}, ignore_index=True)
counter += 1
if counter>4:
break
#convert DF to json.
json_result = json.dumps(resultDF.to_dict('records'))
return json_result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment