Emotions and Music 8
2022. 7. 5. 23:49ㆍProject/감정분석과 노래 추천
728x90
반응형
코드
- 코사인 유사도로 상황과 노래가사가 가장 비슷한 노래 추천
def cosine(name, music, sentences):
music = music.append({'artist_name':name, "song_name":name, "lyrics" : sentences}, ignore_index=True)
tfidf = TfidfVectorizer(analyzer='char', ngram_range=(1,3), smooth_idf=True, sublinear_tf=True)
tfidf_matrix = tfidf.fit_transform(music["lyrics"] )
cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)
title_to_index = dict(zip(music["song_name"], music.index))
def get_recommendations(title, cosine_sim=cosine_sim):
idx = title_to_index[title]
sim_scores = list(enumerate(cosine_sim[idx]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[1:6]
music_list = [(music['artist_name'].iloc[idx[0]], music['song_name'].iloc[idx[0]]) for idx in sim_scores]
return music_list
return get_recommendations(name)
참고
728x90
반응형
'Project > 감정분석과 노래 추천' 카테고리의 다른 글
Emotions and Music 10 (0) | 2022.07.07 |
---|---|
Emotions and Music 9 (1) | 2022.07.06 |
Emotions and Music 7 (0) | 2022.07.04 |
Emotions and Music 6 (0) | 2022.07.03 |
Emotions and Music 5 (0) | 2022.07.03 |