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

ลบ Stop Words ด้วย NLTK ใน Python

เมื่อคอมพิวเตอร์ประมวลผลภาษาธรรมชาติ คำที่พบบ่อยมากซึ่งดูไม่น่ามีค่าในการช่วยเลือกเอกสารตรงความต้องการผู้ใช้ จะถูกตัดออกจากคำศัพท์ทั้งหมด เรียกว่า stop words

เช่น ประโยคอินพุท

John is a person who takes care of the people around him.

หลังลบ stop words จะได้

['John', 'person', 'takes', 'care', 'people', 'around', '.']

NLTK มีคอลเลกชัน stopwords ใช้ลบจากประโยคได้ อยู่ใน NLTK.corpus

ตัวอย่าง

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

my_sent = "John is a person who takes care of people around him."
tokens = word_tokenize(my_sent)
filtered_sentence = [w for w in tokens if not w in stopwords.words()]
print(filtered_sentence)

ผลลัพธ์

['John', 'person', 'takes', 'care', 'people', 'around', '.']