自然语言处理(NLP)的预处理是构建高效模型的基础步骤,主要包含以下核心内容:
🧹 文本清洗
去除标点、特殊字符和HTML标签,例如:import re text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
🔍 分词与词干提取
将句子拆分为单词(分词)并还原词根(词干提取),例如:from nltk.stem import PorterStemmer ps = PorterStemmer() print(ps.stem("running"))
🚫 停用词过滤
移除无意义词汇(如“的”“是”“在”),例如:from nltk.corpus import stopwords stop_words = set(stopwords.words('chinese')) filtered_text = [word for word in text if word not in stop_words]
📊 词频统计
通过collections.Counter
分析高频词汇:from collections import Counter words = ['hello', 'world', 'hello', 'nlp'] print(Counter(words))
如需深入学习NLP实战案例,可访问:自然语言处理入门教程 📚