Python 中的自然语言处理(NLP)
Fouad Trad
Machine Learning Engineer

理解文本主题

理解文本主题

需要保留文本中每个词的任务

NLTK 提供多种语言的停用词列表
from nltk.corpus import stopwords nltk.download('stopwords')stop_words = stopwords.words('english')print(stop_words[:10])
['a', 'about', 'above', 'after', 'again', 'against', 'ain', 'all', 'am', 'an']
from nltk.tokenize import word_tokenizetext = "This is an example to demonstrate removing stop words."tokens = word_tokenize(text)# The .lower() method helps with case sensitivity filtered_tokens = [word for word in tokens if word.lower() not in stop_words]print(filtered_tokens)
['example', 'demonstrate', 'removing', 'stop', 'words', '.']

需要在文档中找出常见或重要词的任务

需要在文档中找出常见或重要词的任务

需保留句子结构以保持清晰度的任务

import string
print(string.punctuation)
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
text = "This is an example to demonstrate removing stop words." tokens = word_tokenize(text) filtered_tokens = [word for word in tokens if word.lower() not in stop_words]clean_tokens = [word for word in filtered_tokens if word not in string.punctuation]print(clean_tokens)
['example', 'demonstrate', 'removing', 'stop', 'words']
Python 中的自然语言处理(NLP)