Berikut adalah kode Analisis sentimen LSTM menggunakan embedding FastText.

import pickle
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.preprocessing.text import Tokenizer
from keras.utils import pad_sequences
from keras.models import Sequential
from keras.layers import LSTM, Dense, Embedding, SpatialDropout1D, Dropout
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from sklearn.metrics import classification_report
import fasttext

# Load model FastText Bahasa Indonesia
path_to_fasttext_model = ‘cc.id.300.bin’
ft_model = fasttext.load_model(path_to_fasttext_model)

# Membaca dataset
data_list = pickle.load(open(‘pre_processed_beritaFIX_joined.pkl’, ‘rb’))
data = pd.DataFrame(data_list, columns=[‘judul’, ‘isi’, ‘pre_processed’, ‘Label’])
data[‘Isi Berita’] = data[‘pre_processed’]

# Mengkodekan label
le = LabelEncoder()
data[‘label_encoded’] = le.fit_transform(data[‘Label’])

# Memisahkan data latih dan data uji
X_train, X_test, y_train, y_test = train_test_split(data[‘Isi Berita’], data[‘label_encoded’], test_size=0.25, random_state=42)

# Tokenisasi dan padding
max_vocab_size = 10000
max_sequence_length = 250

tokenizer = Tokenizer(num_words=max_vocab_size)
tokenizer.fit_on_texts(X_train)

X_train_sequences = tokenizer.texts_to_sequences(X_train)
X_test_sequences = tokenizer.texts_to_sequences(X_test)

X_train_padded = pad_sequences(X_train_sequences, maxlen=max_sequence_length)
X_test_padded = pad_sequences(X_test_sequences, maxlen=max_sequence_length)

# Membuat matriks embedding
embedding_dim = ft_model.get_dimension()
embedding_matrix = np.zeros((max_vocab_size, embedding_dim))

for word, index in tokenizer.word_index.items():
    if index >= max_vocab_size:
        break
    embedding_vector = ft_model.get_word_vector(word)
    embedding_matrix[index] = embedding_vector

# Membangun model LSTM
lstm_units = 64

model = Sequential()
model.add(Embedding(max_vocab_size, embedding_dim, input_length=max_sequence_length, weights=[embedding_matrix], trainable=False))
model.add(SpatialDropout1D(0.2))
model.add(LSTM(lstm_units, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(3, activation=‘softmax’))

optimizer = Adam(lr=0.001)

model.compile(loss=‘sparse_categorical_crossentropy’, optimizer=optimizer, metrics=[‘accuracy’])
print(model.summary())

# Melatih model dengan data latih
epochs = 10
batch_size = 64

history = model.fit(X_train_padded, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[EarlyStopping(monitor=‘val_loss’, patience=3, min_delta=0.0001)])

# Menghitung akurasi pada data uji
acc = model.evaluate(X_test_padded, y_test)[1]
print(‘Akurasi pada data uji:’, acc)

# Menampilkan classification report
# Menggunakan model untuk prediksi kelas
y_pred_proba = model.predict(X_test_padded)

# Mengonversi probabilitas ke kelas
y_pred = np.argmax(y_pred_proba, axis=1)

# Menampilkan classification report
print(classification_report(y_test, y_pred, target_names=le.classes_))

   ft_model = fasttext.load_model(path_to_fasttext_model)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "fasttext\fasttext.pyx", line 154, in fasttext.fasttext.load_model
Exception: fastText: Cannot load cc.id.300.bin due to C++ extension failed to allocate the memory