|
|
import tensorflow as tf
|
|
|
from tensorflow import keras
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_path = "my_rnn_model_imdb.keras"
|
|
|
try:
|
|
|
loaded_model = keras.models.load_model(model_path)
|
|
|
print(f"'{model_path}' 모델을 성공적으로 불러왔습니다.")
|
|
|
except Exception as e:
|
|
|
print(f"모델 로딩 중 오류 발생: {e}")
|
|
|
exit()
|
|
|
|
|
|
|
|
|
word_index = keras.datasets.imdb.get_word_index()
|
|
|
|
|
|
|
|
|
word_index = {k: (v + 3) for k, v in word_index.items()}
|
|
|
word_index["<pad>"] = 0
|
|
|
word_index["<start>"] = 1
|
|
|
word_index["<unk>"] = 2
|
|
|
word_index["<unused>"] = 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MAX_LEN = 256
|
|
|
|
|
|
def preprocess_text(text):
|
|
|
"""
|
|
|
새로운 텍스트를 모델 입력 형식에 맞게 전처리합니다.
|
|
|
"""
|
|
|
|
|
|
tokens = [word_index.get(word, 2) for word in text.lower().split()]
|
|
|
|
|
|
|
|
|
tokens = [word_index["<start>"]] + tokens
|
|
|
|
|
|
|
|
|
padded_sequence = keras.preprocessing.sequence.pad_sequences(
|
|
|
[tokens], maxlen=MAX_LEN, padding='pre'
|
|
|
)
|
|
|
|
|
|
return padded_sequence
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("\n영화 리뷰 감성 분석기 (종료하려면 'exit'를 입력하세요)")
|
|
|
print("-" * 50)
|
|
|
|
|
|
while True:
|
|
|
|
|
|
review_text = input("리뷰를 입력하세요: ")
|
|
|
|
|
|
if review_text.lower() == 'exit':
|
|
|
print("프로그램을 종료합니다.")
|
|
|
break
|
|
|
|
|
|
if not review_text.strip():
|
|
|
print("입력된 내용이 없습니다. 다시 시도해주세요.")
|
|
|
continue
|
|
|
|
|
|
|
|
|
processed_input = preprocess_text(review_text)
|
|
|
|
|
|
|
|
|
prediction = loaded_model.predict(processed_input)
|
|
|
|
|
|
|
|
|
score = prediction[0][0]
|
|
|
sentiment = "긍정 (Positive)" if score > 0.5 else "부정 (Negative)"
|
|
|
|
|
|
print(f"결과: {sentiment} (예측 점수: {score:.4f})")
|
|
|
print("-" * 50) |