Computer >> Máy Tính >  >> Lập trình >> Python

Một phần của Gắn thẻ bằng giọng nói với Từ dừng sử dụng NLTK trong python?

Ý tưởng chính đằng sau Xử lý ngôn ngữ tự nhiên là máy có thể thực hiện một số hình thức phân tích hoặc xử lý mà không cần sự can thiệp của con người ít nhất ở một mức độ nào đó như hiểu một phần nào đó ý nghĩa của văn bản hoặc đang cố gắng nói.

Trong khi cố gắng xử lý văn bản, máy tính cần lọc ra những dữ liệu (từ) vô dụng hoặc ít quan trọng hơn khỏi văn bản. Trong NLTK, các từ vô ích (dữ liệu) được gọi là các từ dừng.

Cài đặt thư viện Bắt buộc

Trước tiên, bạn cần thư viện nltk, chỉ cần chạy lệnh dưới đây trong thiết bị đầu cuối của bạn:

$pip install nltk

Vì vậy, chúng tôi sẽ xóa những từ dừng này để chúng không chiếm dung lượng trong cơ sở dữ liệu của chúng tôi hoặc chiếm thời gian xử lý quý giá.

Bạn có thể tạo danh sách các từ của riêng mình mà bạn có thể coi là từ dừng. Theo mặc định, NLTK chứa một số từ mà họ coi là từ dừng, bạn có thể truy cập nó qua kho ngữ liệu NLTK với:

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

Đây là danh sách các từ dừng 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'}

Dưới đây là một chương trình hoàn chỉnh sẽ phân tích cách sử dụng từ dừng để xóa các từ dừng khỏi văn bản của bạn:

Mã mẫu

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)

Đầu ra

Đầu ra văn bản:Không có bộ lọc (có từ dừng)

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

Đầu ra văn bản:Với bộ lọc (xóa từ dừng)

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