DEV Community

Cover image for การจำแนกอารมณ์ของข้อความ ด้วย Machine Learning โดยใช้ Python
Temimeno
Temimeno

Posted on

การจำแนกอารมณ์ของข้อความ ด้วย Machine Learning โดยใช้ Python

การจำแนกอารมณ์ของข้อความ (Text Emotions Classification) เป็นปัญหาด้านการประมวลผลภาษาธรรมชาติหรือ Natural Language Processing (NLP) และการจำแนกข้อความหรือ Text Classification โดยเราจะต้องทำการฝึกโมเดล Text Classification เพื่อแบ่งแยกอารมณ์ของข้อความ

ในการแก้ปัญหานี้ เราจะต้องกำหนดข้อมูลของข้อความและอารมณ์ของข้อความนั้นๆ ก่อน โดยเราจะใช้ชุดข้อมูลจากเว็บไซต์ Kaggle ในการแก้ปัญหานี้ คุณสามารถไปดาวน์โหลดชุดข้อมูลได้จาก ที่นี่

ในบทความนี้เราจะแสดงวิธีฝึกโมเดล Text Classification สำหรับการจำแนกอารมณ์ของข้อความ โดยใช้ Machine Learning และ Python


ขั้นตอนการเตรียมการ

ขั้นที่ 1: เริ่มต้นจากการ import python libraries ต้องใช้ก่อน

import pandas as pd import numpy as np import keras import tensorflow from keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Embedding, Flatten, Dense 
Enter fullscreen mode Exit fullscreen mode

ขั้นที่ 2: ทำการ Upload ชุดข้อมูลที่เราได้ดาวน์โหลดมาลงใน Google Colab โดย...

  1. คลิ๊กที่รูป Folder บริเวณแถบซ้าย
  2. คลิ๊กที่ปุ่ม Upload บริเวณด้ายซ้ายบนหลังจากที่ Folder เปิดขึ้นมาแล้ว
  3. เลือกไฟล์ที่ต้องการอัพโหลด

Image description

ขั้นที่ 3: ลองทดสอบว่าทำการ Upload ชุดข้อมูลสำเร็จหรือไม่โดยการลอง Print ชุดข้อมูล 5 ตัวแรก

data = pd.read_csv("train.txt", sep=';') data.columns = ["Text", "Emotions"] print(data.head()) 
Enter fullscreen mode Exit fullscreen mode

ผลลัพธ์ที่ได้:
Image description

ขั้นที่ 4: เนื่องจากนี่เป็นปัญหาที่เกี่ยวข้องกับ Natural Language Processing (NLP) เราจึงต้องป้องกันข้อมูลส่วนบุคคล เช่น เลขบัญชี โดยใช้ Data Tokenization

texts = data["Text"].tolist() labels = data["Emotions"].tolist() # Tokenize the text data tokenizer = Tokenizer() tokenizer.fit_on_texts(texts) 
Enter fullscreen mode Exit fullscreen mode

ขั้นที่ 5: จัดวางลำดับข้อความใหม่เพื่อให้มีความยาวเท่ากับข้อความต้นฉบับเพื่อป้อนข้อมูลให้ Neural Network

sequences = tokenizer.texts_to_sequences(texts) max_length = max([len(seq) for seq in sequences]) padded_sequences = pad_sequences(sequences, maxlen=max_length) 
Enter fullscreen mode Exit fullscreen mode

ขั้นที่ 6: ใช้ Label Encoder Method เพื่อเปลี่ยนข้อมูลตัวอักษรให้เป็นข้อมูลตัวเลข

# Encode the string labels to integers label_encoder = LabelEncoder() labels = label_encoder.fit_transform(labels) 
Enter fullscreen mode Exit fullscreen mode

ขั้นที่ 7: เราจะทำการ One-Hot Encode หรือก็คือการเปลี่ยนข้อมูลที่ถูกเก็บในลักษณะเป็น Categorical ให้เป็นเลขฐาน 2 ขั้นตอนนี้เป็นขั้นตอนที่จำเป็นเพราะ Machine Learning Algorithms จะทำงานกับข้อมูลที่เป็นตัวเลข

# One-hot encode the labels one_hot_labels = keras.utils.to_categorical(labels) 
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนการฝึกโมเดล Text Emotions Classification

ขั้นที่ 1: ทำการแบ่งกลุ่มข้อมูลออกเป็น 2 กลุ่ม คือ...

  • กลุ่มข้อมูลสำหรับการฝึก
  • กลุ่มข้อมูลสำหรับการทดสอบ
# Split the data into training and testing sets xtrain, xtest, ytrain, ytest = train_test_split(padded_sequences, one_hot_labels, test_size=0.2) 
Enter fullscreen mode Exit fullscreen mode

ขั้นที่ 2: กำหนด Neural Network Architecture ของปัญหาที่เรายกมา และใช้มันในการฝึกโมเดลการจำแนกอารมณ์

# Define the model model = Sequential() model.add(Embedding(input_dim=len(tokenizer.word_index) + 1, output_dim=128, input_length=max_length)) model.add(Flatten()) model.add(Dense(units=128, activation="relu")) model.add(Dense(units=len(one_hot_labels[0]), activation="softmax")) model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) model.fit(xtrain, ytrain, epochs=10, batch_size=32, validation_data=(xtest, ytest)) 
Enter fullscreen mode Exit fullscreen mode

ผลลัพธ์ที่ได้:

Image description

ขั้นที่ 3: ทำการทดสอบโมเดลที่ฝึกมาโดยการลองพิมพ์ประโยคที่เราต้องการ เช่น

  • ตัวอย่างที่ 1
input_text = "She didn't come today because she lost her dog yestertay!" # Preprocess the input text input_sequence = tokenizer.texts_to_sequences([input_text]) padded_input_sequence = pad_sequences(input_sequence, maxlen=max_length) prediction = model.predict(padded_input_sequence) predicted_label = label_encoder.inverse_transform([np.argmax(prediction[0])]) print(predicted_label) 
Enter fullscreen mode Exit fullscreen mode
  • ตัวอย่างที่ 2
input_text = "I fear no man, but that thing... It scared me" # Preprocess the input text input_sequence = tokenizer.texts_to_sequences([input_text]) padded_input_sequence = pad_sequences(input_sequence, maxlen=max_length) prediction = model.predict(padded_input_sequence) predicted_label = label_encoder.inverse_transform([np.argmax(prediction[0])]) print(predicted_label) 
Enter fullscreen mode Exit fullscreen mode
  • ตัวอย่างที่ 3
input_text = "Remember last vacation? It was the best time of my life!" # Preprocess the input text input_sequence = tokenizer.texts_to_sequences([input_text]) padded_input_sequence = pad_sequences(input_sequence, maxlen=max_length) prediction = model.predict(padded_input_sequence) predicted_label = label_encoder.inverse_transform([np.argmax(prediction[0])]) print(predicted_label) 
Enter fullscreen mode Exit fullscreen mode

ผลลัพธ์ที่ได้ :

  • ตัวอย่างที่ 1
    Image description

  • ตัวอย่างที่ 2
    Image description

  • ตัวอย่างที่ 3
    Image description


สรุปผล

การจำแนกอารมณ์ของข้อความ (Text Emotions Classification) เป็นปัญหาด้านการกำหนดอารมณ์ที่ข้อความสื่อออกมาผ่านเนื้อความนั้นๆ โดยหนึ่งตัวอย่างที่ที่ใช้โมเดลตัวนี้ในชีวิตประจำวันนั้นคือ การที่เราพิมพ์ข้อความลงบนแป้นพิมพ์ของ iPhone และจะมี Emoji ที่เกี่ยวข้องกับเนื้อความแสดงขึ้นมาให้กดได้ ซึ่งทำให้เพิ่มความสะดวกให้กับผู้ใช้ได้โดยแทนที่จะต้องไปค้นหา Emoji นั้นๆ เอง การที่มี Emoji ขึ้นมาให้เลยจึงช่วยประหยัดเวลาของผู้ใช้เป็นอย่างมาก

ทั้งนี้ความแม่นยำของการฝึกโมเดลในบทความนี้ยังคงมีความคลาดเคลื่อนและผิดผลาดอยู่ หากต้องการให้มีความแม่นยำมากกว่านี้ จะต้องป้อนข้อมูลให้กับ AI มากกว่านี้เช่นกัน เพื่อที่ AI ตัวนี้จะสามารถทำงานได้อย่างสมบูรณ์แบบมากขึ้น


แหล่งอ้างอิง

Top comments (0)