目录

自然语言处理NLP入门 -- 第二节预处理文本数据

在自然语言处理(NLP)中,数据的质量直接影响模型的表现。文本预处理的目标是清理和标准化文本数据,使其适合机器学习或深度学习模型处理。本章介绍几种常见的文本预处理方法,并通过 Python 代码进行示例。


2.1 文本清理

文本数据往往包含各种噪音,例如 HTML 标签、特殊字符、空格、数字等。清理文本可以提高模型的准确性。

常见的清理步骤

  • 去除 HTML 标签
  • 移除特殊字符(如 @#%$&
  • 移除数字
  • 统一大小写(通常转换为小写)
  • 去除多余的空格

Python 示例

python 复制代码
import re  # 正则表达式库,用于文本匹配和替换

text = "Hello, <b>world</b>! Visit us at https://example.com or call 123-456-7890."

# 1. 去除HTML标签
text = re.sub(r'<.*?>', '', text)

# 2. 去除特殊字符(保留字母和空格)
text = re.sub(r'[^a-zA-Z\s]', '', text)

# 3. 转换为小写
text = text.lower()

# 4. 去除多余空格
text = " ".join(text.split())

print(text)

输出:

复制代码
hello world visit us at httpsexamplecom or call

2.2 分词(Tokenization)

分词是将文本拆分成单个的单词或子词,是 NLP 任务的基础。

常见分词方法

  • 按空格拆分(适用于英文)
  • NLTK 分词(更精准)
  • spaCy 分词(高效处理大规模数据)

Python 示例

python 复制代码
import nltk  # 自然语言处理库,提供分词、词性标注、停用词等功能
from nltk.tokenize import word_tokenize, sent_tokenize
import spacy  # 现代 NLP 库,优化分词、词性标注等任务

nltk.download('punkt_tab')  # punkt_tab 是 NLTK 中的分词模型

text = "Hello world! This is an NLP tutorial."

# 1. 基础空格分词
tokens_space = text.split()
print("空格分词:", tokens_space)

# 2. 使用 NLTK 进行分词
tokens_nltk = word_tokenize(text)
print("NLTK 分词:", tokens_nltk)

# 3. 使用 spaCy 进行分词
nlp = spacy.load("en_core_web_sm")  # 加载预训练的小型英文模型
doc = nlp(text)
tokens_spacy = [token.text for token in doc]
print("spaCy 分词:", tokens_spacy)

输出:

复制代码
空格分词: ['Hello', 'world!', 'This', 'is', 'an', 'NLP', 'tutorial.']
NLTK 分词: ['Hello', 'world', '!', 'This', 'is', 'an', 'NLP', 'tutorial', '.']
spaCy 分词: ['Hello', 'world', '!', 'This', 'is', 'an', 'NLP', 'tutorial', '.']

注意:

  • 空格分词简单但容易出错,如 "NLP tutorial." 仍包含标点。
  • NLTK 和 spaCy 处理得更精准,分离了标点符号。

2.3 词干提取(Stemming)和词形还原(Lemmatization)

在 NLP 任务中,单词的不同形式可能具有相同的含义,例如:

  • runningrun
  • bettergood

词干提取和词形还原可以将单词标准化,从而提高模型的泛化能力。

词干提取(Stemming)

词干提取是基于规则的词形归一化方法,会粗暴地去掉单词的后缀。

python 复制代码
from nltk.stem import PorterStemmer, SnowballStemmer  # 词干提取工具

stemmer = PorterStemmer()  # PorterStemmer 是常用的词干提取方法
words = ["running", "flies", "easily", "studies"]

stemmed_words = [stemmer.stem(word) for word in words]
print("Porter Stemmer:", stemmed_words)

输出:

复制代码
Porter Stemmer: ['run', 'fli', 'easili', 'studi']

缺点:

  • flies 变成了 fli
  • easily 变成了 easili
  • 可能导致含义丢失

词形还原(Lemmatization)

Lemmatization 通过查找词典将单词转换为其词根形式,更加精确。

python 复制代码
from nltk.stem import WordNetLemmatizer
import nltk

nltk.download('wordnet')  # 下载 WordNet 语料库

lemmatizer = WordNetLemmatizer()
words = ["running", "flies", "easily", "studies", "better"]

lemmatized_words = [lemmatizer.lemmatize(word, pos="v") for word in words]
print("Lemmatization:", lemmatized_words)

输出:

复制代码
Lemmatization: ['run', 'fly', 'easily', 'study', 'better']

优点:

  • flies 被正确地还原为 fly
  • studies 被正确地还原为 study
  • better 仍保持其正确形式

2.4 停用词(Stopwords)处理

停用词(Stopwords)是指在文本处理中不重要的高频词,如 is, the, and,可以去除以减少模型计算量。

Python 示例

python 复制代码
from nltk import word_tokenize
from nltk.corpus import stopwords  # NLTK 提供的停用词库
import nltk
nltk.download('stopwords')  # 下载停用词列表

text = "This is a simple NLP example demonstrating stopwords removal."

words = word_tokenize(text)

filtered_words = [word for word in words if word.lower() not in stopwords.words('english')]
print("去除停用词后:", filtered_words)

输出:

复制代码
去除停用词后: ['simple', 'NLP', 'example', 'demonstrating', 'stopwords', 'removal', '.']

注意:

  • is, a, this 被去掉
  • NLP 等关键词被保留

2.5 难点总结

  • 分词的不同方法:空格分词 vs. NLTK vs. spaCy,适用于不同场景。
  • 词干提取 vs. 词形还原:Stemming 可能会导致错误,而 Lemmatization 更精确但需要额外的词性信息。
  • 停用词的处理:某些 NLP 任务(如情感分析)可能需要保留停用词。

2.6 课后练习

练习 1:文本清理

清理以下文本,去掉 HTML 标签、特殊字符、数字,并转换为小写:

python 复制代码
text = "Visit our <b>website</b>: https://example.com!!! Call us at 987-654-3210."

练习 2:使用 spaCy 进行分词

使用 spaCy 对以下文本进行分词:

python 复制代码
text = "Natural Language Processing is fun and useful!"

练习 3:词形还原

使用 Lemmatization 处理以下单词:

python 复制代码
words = ["running", "mice", "better", "studying"]

练习 4:去除停用词

从以下文本中去除停用词:

python 复制代码
text = "This is an example sentence demonstrating stopwords removal."
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
wgc2k18 分钟前
吴恩达深度学习复盘(6)神经网络的矢量化原理
python·深度学习·矩阵
jndingxin22 分钟前
OpenCV 图形API(16)将极坐标(magnitude 和 angle)转换为笛卡尔坐标(x 和 y)函数polarToCart()
人工智能·opencv·计算机视觉
?Agony33 分钟前
P17_ResNeXt-50
人工智能·pytorch·python·算法
Ronin-Lotus35 分钟前
深度学习篇---模型训练早停机制
人工智能·pytorch·深度学习·模型训练·过拟合·早停
鲲志说1 小时前
本地化部署DeepSeek-R1蒸馏大模型:基于飞桨PaddleNLP 3.0的实战指南
人工智能·nlp·aigc·paddlepaddle·飞桨·paddle·deepseek
浪淘沙jkp1 小时前
大模型学习四:‌DeepSeek Janus-Pro 多模态理解和生成模型 本地部署指南(折腾版)
python·学习·deepseek
hello_ejb31 小时前
聊聊Spring AI的MilvusVectorStore
java·人工智能·spring
HR Zhou1 小时前
群体智能优化算法-算术优化算法(Arithmetic Optimization Algorithm, AOA,含Matlab源代码)
人工智能·算法·数学建模·matlab·优化·智能优化算法
yolo大师兄1 小时前
【YOLO系列(V5-V12)通用数据集-火灾烟雾检测数据集】
人工智能·深度学习·yolo·目标检测·机器学习
jndingxin1 小时前
OpenCV 图形API(15)计算两个矩阵(通常代表二维向量的X和Y分量)每个对应元素之间的相位角(即角度)函数phase()
人工智能·opencv