自然语言处理(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实战案例,可访问:自然语言处理入门教程 📚