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

เป็นส่วนหนึ่งของการติดแท็กคำพูดด้วยคำหยุดโดยใช้ NLTK ใน python หรือไม่


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

ขณะพยายามประมวลผลข้อความ คอมพิวเตอร์จำเป็นต้องกรองข้อมูล (คำ) ที่ไร้ประโยชน์หรือมีความสำคัญน้อยกว่าออกจากข้อความ ใน NLTK คำที่ไร้ประโยชน์ (ข้อมูล) จะเรียกว่าคำหยุด

การติดตั้งไลบรารีที่จำเป็น

ก่อนอื่นคุณต้องมีไลบรารี nltk เพียงเรียกใช้คำสั่งด้านล่างในเทอร์มินัลของคุณ:

$pip install nltk

ดังนั้น เราจะลบคำหยุดเหล่านี้ออก เพื่อไม่ให้ใช้พื้นที่ในฐานข้อมูลของเราหรือใช้เวลาในการประมวลผลอันมีค่า

คุณสามารถสร้างรายการคำที่คุณอาจพิจารณาว่าเป็นคำหยุด โดยค่าเริ่มต้น NLTK มีกลุ่มคำบางคำที่พวกเขาพิจารณาว่าเป็นคำหยุด คุณสามารถเข้าถึงได้ผ่านคลังข้อมูล NLTK ด้วย:

>>> import nltk
>>> from nltk.corpus import stopwords

นี่คือรายการคำหยุด NLTK:

>>> set(stopwords.words('english'))
{'not', 'other', 'shan', "hadn't", 'she', 'did', 'through', 'and', 'does', "that'll", "weren't", 'your', "should've", "hasn't", 'myself', 'should', 'because', 'wasn', 'what', 'to', 'this', 'was', 'more', 'y', 'again', "needn't", 'into', 'above', 'themselves', 'd', "won't", 'during', 'haven', 'both', "shan't", 'their', 'on', 'hadn', 'up', 'once', 'its', 'against', 'before', 't', 'while', 'needn', 'doing', "don't", 'yourselves', 'until', 'is', 'all', 's', 'will', "you've", 'being', 'under', 'they', 'ours', 'wouldn', 'of', 'didn', 'below', 'just', 'ma', 'yours', "you'll", 'mightn', 'where', 'are', 'that', 'those', 'most', 'them', 'if', 'you', "shouldn't", 'off', 'for', 'her', 'such', 'now', 'than', 're', 'no', 'm', 'or', "aren't", 'further', 'here', "wasn't", 'after', "haven't", 'my', 'himself', 'at', 'had', 'yourself', 'by', 'weren', 'only', 'have', 'we', 'do', 'same', "isn't", 'herself', 'll', 'down', 'then', 'why', 'own', 'him', 'so', 'having', 'nor', 'isn', 'few', 'how', 'each', 'there', 'with', 'couldn', 'about', 'very', 'am', 'me', "didn't", "doesn't", 'which', "she's", 'doesn', 'were', 'he', 'in', "mightn't", 'when', 'our', 'who', 'his', "couldn't", 'the', "you'd", 'be', 'hers', 'hasn', 'between', 'it', 'mustn', 'but', 'out', 'can', "wouldn't", 'ourselves', 'whom', 'been', 'these', 'aren', 'over', 'itself', 'a', 'i', 'too', 'theirs', 'some', "you're", 'as', 'won', "it's", 'from', 'o', 'don', 'any', 've', 'ain', 'has', 'an', "mustn't", 'shouldn'}

ด้านล่างนี้เป็นโปรแกรมที่สมบูรณ์ซึ่งจะสาธิตวิธีการใช้คำหยุดเพื่อลบคำหยุดออกจากข้อความของคุณ:

โค้ดตัวอย่าง

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)

filtered_sentence = [w for w in word_tokens if not w in stop_words]

filtered_sentence = []

for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)

print(word_tokens)
print(filtered_sentence)

ผลลัพธ์

เอาต์พุตข้อความ:ไม่มีตัวกรอง (มีคำหยุด)

['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', '.']

ข้อความออก:มีตัวกรอง (ลบคำหยุด)

['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', '.']