NLP---文本前期预处理的几个步骤

1、读取文本

python 复制代码
text1 ="""
Football is a family of team sports that involve, to varying degrees, kicking a ball to score a goal. 
Unqualified, the word football is understood to refer to whichever form of football is the most popular 
in the regional context in which the word appears. Sports commonly called football in certain places 
include association football (known as soccer in some countries); gridiron football (specifically American 
football or Canadian football); Australian rules football; rugby football (either rugby league or rugby union); 
and Gaelic football. These different variations of football are known as football codes.
"""
print("原文:\n", text1)

2、去除换行符

python 复制代码
text = text1.replace("\n", "")
print("去除原文中的换行符:\n", text)

3、分句

python 复制代码
import nltk
sents = nltk.sent_tokenize(text)
print("将文本进行分句:\n", sents)

4、分词

python 复制代码
import string
punctuation_tokens = []
for sent in sents:
    for word in nltk.word_tokenize(sent):
        punctuation_tokens.append(word)
print("将每个句子进行分词:\n", punctuation_tokens)

5、过滤标点符号

python 复制代码
tokens = []
for word in punctuation_tokens:
    if word not in string.punctuation:
        tokens.append(word)
print("将分词结果去除标点符号:\n", tokens)

6、过滤停用词

python 复制代码
from nltk.corpus import stopwords
fltered = [w for w in tokens if w not in stopwords.words("english")]
print("过滤完停用词之后:\n", fltered)

7、剩下有用的单词进行计数

python 复制代码
from collections import Counter
count = Counter(fltered)
print("对最终清洗好的单词进行计数:\n", count)
相关推荐
ToTensor13 小时前
Tree of Thoughts:让大语言模型像人类一样思考
人工智能·语言模型·自然语言处理
黑客思维者1 天前
为什么大语言模型需要海量训练数据?
人工智能·语言模型·自然语言处理
AI大模型学徒1 天前
NLP基础(九)_N-gram模型
人工智能·自然语言处理·nlp·n-gram
8***B1 天前
Java自然语言处理
java·开发语言·自然语言处理
walnut_oyb1 天前
arXiv|SARLANG-1M:用于 SAR 图像理解的视觉-语言建模基准
论文阅读·人工智能·机器学习·计算机视觉·语言模型·自然语言处理
jieshenai2 天前
5090显卡,基于vllm完成大模型推理
人工智能·自然语言处理
没有梦想的咸鱼185-1037-16632 天前
最新“科研创新与智能化转型“暨AI 智能体(Agent)开发、大语言模型(LLM)本地化部署与RAG/微调优化技术
人工智能·语言模型·自然语言处理·chatgpt·数据分析
AI大模型学徒2 天前
NLP基础(八)_马尔可夫模型
算法·机器学习·自然语言处理·nlp·概率论·马尔可夫模型
HyperAI超神经2 天前
【TVM 教程】优化大语言模型
人工智能·语言模型·自然语言处理·cpu·gpu·编程语言·tvm
musk12122 天前
文本分析与挖掘,nlp,中文产品评论情感分析最佳实践方案
人工智能·自然语言处理