import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel


    
#print('done!')

def item(id):
    return ds.loc[ds['id'] == id]['description'].tolist()[0].split(' - ')[0]

# Just reads the results out of the dictionary. No real logic here.
    

def recommend():
    ds = pd.read_csv("myReco.csv")

    tf = TfidfVectorizer(analyzer='word', ngram_range=(1, 3), min_df=0, stop_words='english')
    tfidf_matrix = tf.fit_transform(ds['description'])

    cosine_similarities = linear_kernel(tfidf_matrix, tfidf_matrix)
    item_id = ds['id'][len(ds)-1]
    num =5
    results = {}

    for idx, row in ds.iterrows():
        similar_indices = cosine_similarities[idx].argsort()[:-100:-1]
        similar_items = [(cosine_similarities[idx][i], ds['id'][i]) for i in similar_indices]

    # First item is the item itself, so remove it.
    # Each dictionary entry is like: [(1,2), (3,4)], with each tuple being (score, item_id)
    results[row['id']] = similar_items[1:]
    #    print("Recommending " + str(num) + " products similar to " + item(item_id) + "...")
    #    print("-------")
    recs = results[item_id][:num]
    ans=""
    
    for rec in recs:
    #        print("Recommended: " + item(rec[1]) + " (score:" + str(rec[0]) + ")")
        ans = ans + rec[1] + " " 
    #    print ( ans )
    
    return ans 

# Just plug in any item id here (1-500), and the number of recommendations you want (1-99)
# You can get a list of valid item IDs by evaluating the variable 'ds', or a few are listed below

#print(ds['id'][len(ds) - 1] )
#recommend(item_id=ds['id'][len(ds)-1], num=5)
