Emotions and Music 7

2022. 7. 4. 20:45Project/감정분석과 노래 추천

728x90
반응형

코드

- 감정분석 모델 Flask에 연결

# Tfidf를 활용한 padding 작업

def Tfidf_padding(sentence):
    def clean_text(d):
        pattern = r'[^가-힣0-9\s]'
        text = re.sub(pattern, '', d)
        text = re.sub("\n", ' ', text)
        text = re.sub("  +", " ", text)
        return text
        
    documents = [clean_text(sentence)]
    def tokenizer (sentences):
        kkma = Kkma()
        error_list = []
        #불용어처리
        current_path = os.getcwd()
        f = open(current_path + "/stop_words.txt", 'r', encoding='utf-8')
        stop_words= [line.replace('\n','') for line in f.readlines()]

        #영어로 Nan된 document 삭제
        tokens = []
        for i, sentence in enumerate(sentences) :
            try : 
                tokens.append([word[0] for word in kkma.pos(sentence) if len(word) > 1 
                                and (word[0] not in stop_words)
                                and (not word[1].startswith('J'))
                                and (not word[1].startswith('E'))])
            except Exception:
                print('error', sentence, i)
                error_list.append(i)
                tokens.append("error")
        tokens = pd.Series(tokens).drop(index = error_list)
        
        return tokens
    tfidf = TfidfVectorizer(tokenizer=tokenizer, analyzer='char', ngram_range=(1,3),
                    smooth_idf=True, sublinear_tf=True, max_features=460)
    vectors = tfidf.fit_transform(documents)
    vectors = np.array(vectors.todense())
    padded = np.array([np.append(np.array(vector), np.array([0])) for vector in vectors])
    return padded
# 모델 예측 

def model_prediect(padded):
    model = keras.models.load_model('model.h5')
    try:
        score = model.predict(padded)
        index = np.argmax(score, axis=1)[0]
        return index
    except:
        print("다시 작성해주세요")
@app.route('/result', methods=["GET","POST"])
def result():
    song = request.form.get('song')
    print(song)
    name = session.get('username')
    text = request.form.get('text')
    padded = Tfidf_padding(text)
    index = model_prediect(padded)
    conn = mysql.connect()
    cursor = conn.cursor()
    cursor.execute("SELECT DISTINCT * FROM Music.music AS m ;")
    music = cursor.fetchall()
    music = pd.DataFrame(music, columns=["artist_name", "song_name", "lyrics"])
    music_list = cosine(name, music, text)
    return render_template('result.html', name=name, index=index, music_list=music_list), 200

 

728x90
반응형

'Project > 감정분석과 노래 추천' 카테고리의 다른 글

Emotions and Music 9  (1) 2022.07.06
Emotions and Music 8  (0) 2022.07.05
Emotions and Music 6  (0) 2022.07.03
Emotions and Music 5  (0) 2022.07.03
Emotions and Music 4  (0) 2022.07.03