Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Part-of-Speech Tagging และการลบ Stop Words ด้วย NLTK ใน Python

แนวคิดหลักของ Natural Language Processing (NLP) คือให้เครื่องทำการวิเคราะห์หรือประมวลผลข้อความโดยไม่ต้องมีการแทรกแซงจากมนุษย์ ระดับหนึ่ง เช่น เข้าใจความหมายบางส่วนของข้อความ

Stop Words คืออะไร?

ขณะประมวลผลข้อความ คอมพิวเตอร์ต้องกรองคำที่ไม่สำคัญหรือมีประโยชน์น้อยออกไป ใน NLTK เรียกคำเหล่านี้ว่า stop words เช่น "และ", "ใน", "ของ", "the", "is", "a" เป็นต้น

การติดตั้ง

$ pip install nltk

รายการ Stop Words เริ่มต้นของ NLTK (ภาษาอังกฤษ)

>>> import nltk
>>> from nltk.corpus import stopwords
>>> set(stopwords.words('english'))
{'not', 'other', 'shan', "hadn't", 'she', 'did', 'through', 'and', 'does', "that'll", ...}

โค้ดตัวอย่าง: ลบ Stop Words

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

example_sent = ("Python is a powerful high-level, object-oriented programming language created by Guido van Rossum. "
                "It has simple easy-to-use syntax, making it the perfect language for someone trying to learn computer programming for the first time. "
                "This is a comprehensive guide on how to get started in Python, why you should learn it and how you can learn it. However, if you knowledge "
                "of other programming languages and want to quickly get started with Python.")

stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example_sent)

# วิธี 1: List comprehension
filtered_sentence = [w for w in word_tokens if not w in stop_words]

# วิธี 2: Loop ปกติ
filtered_sentence = []
for w in word_tokens:
    if w not in stop_words:
        filtered_sentence.append(w)

print("Token ทั้งหมด (รวม stop words):")
print(word_tokens)

print("\nToken หลังลบ stop words:")
print(filtered_sentence)

ผลลัพธ์

ก่อนลบ Stop Words

['Python', 'is', 'a', 'powerful', 'high-level', ',', 'object-oriented', 'programming', 'language', 'created', 'by', 'Guido', 'van', 'Rossum.It', 'has', 'simple', 'easy-to-use', 'syntax', ',', 'making', 'it', 'the', 'perfect', 'language', 'for', 'someone', 'trying', 'to', 'learn', 'computer', 'programming', 'for', 'the', 'first', 'time.This', 'is', 'a', 'comprehensive', 'guide', 'on', 'how', 'to', 'get', 'started', 'in', 'Python', ',', 'why', 'you', 'should', 'learn', 'it', 'and', 'how', 'you', 'can', 'learn', 'it', '.', 'However', ',', 'if', 'you', 'knowledge', 'of', 'other', 'programming', 'languages', 'and', 'want', 'to', 'quickly', 'get', 'started', 'with', 'Python', '.']

หลังลบ Stop Words

['Python', 'powerful', 'high-level', ',', 'object-oriented', 'programming', 'language', 'created', 'Guido', 'van', 'Rossum.It', 'simple', 'easy-to-use', 'syntax', ',', 'making', 'perfect', 'language', 'someone', 'trying', 'learn', 'computer', 'programming', 'first', 'time.This', 'comprehensive', 'guide', 'get', 'started', 'Python', ',', 'learn', 'learn', '.', 'However', ',', 'knowledge', 'programming', 'languages', 'want', 'quickly', 'get', 'started', 'Python', '.']

ประโยชน์

  • ลดขนาดข้อมูล ลดเวลาประมวลผล
  • เพิ่มความแม่นยำในการวิเคราะห์ความรู้สึก จำแนกเอกสาร หัวข้อ
  • สร้างฟีเจอร์สำหรับ Machine Learning ได้ดีขึ้น

สามารถสร้างรายการ stop words เองได้ตามโดเมนเฉพาะ เช่น คำเฉพาะทางการแพทย์ การเงิน ฯลฯ