import pandas as pd from konlpy.tag import Okt import gradio as gr # 감성 사전 로딩 knu_lex = pd.read_csv('SentiWord_Dict.txt', sep='\t', names=['word', 'score', 'desc']) # 형태소 분석기 okt = Okt() # 감정 점수 계산 함수 def get_sentiment_score(text): tokens = okt.morphs(text) score = 0 matched_words = [] for token in tokens: matched = knu_lex[knu_lex['word'] == token] if not matched.empty: token_score = int(matched.iloc[0]['score']) score += token_score matched_words.append(f"{token}({token_score})") interpretation = "" if score >= 2: interpretation = "😊 긍정적인 문장입니다!" elif score <= -2: interpretation = "☹️ 부정적인 문장입니다." else: interpretation = "😐 중립적인 문장입니다." return f"▶ 감정 점수: {score}\n▶ 감정 단어: {', '.join(matched_words)}\n\n{interpretation}" # Gradio 인터페이스 iface = gr.Interface( fn=get_sentiment_score, inputs=gr.Textbox(lines=3, placeholder="카카오톡 메시지를 입력하세요"), outputs="text", title="KNU 감성사전 기반 감정 분석기", description="카카오톡 등 구어체 문장을 입력하면 감정 점수를 계산해줍니다. (사전 기반 분석)" ) if __name__ == "__main__": iface.launch()