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)
相关推荐
GIOTTO情1 小时前
智能舆情处置系统技术方案:基于NLP语义算法的全链路风险处置落地
人工智能·算法·自然语言处理
m沐沐2 小时前
【机器学习】NLP---用 Python+TF-IDF 给《红楼梦》自动提取关键词
人工智能·python·机器学习·自然语言处理·nlp·中文分词·tf-idf
yuanyuan2o214 小时前
模型预训练:Hugging Face Transformers 基础
算法·ai·语言模型·自然语言处理·nlp·深度优先
瑶总迷弟17 小时前
使用 mis-tei 在昇腾310P上部署 bge-m3模型
pytorch·python·华为·语言模型·自然语言处理·cnn·unix
Chunyyyen19 小时前
【第四十七周】自然语言处理课程作业记录
人工智能·自然语言处理
tzc_fly20 小时前
ELF:连续扩散语言模型
人工智能·语言模型·自然语言处理
啦啦啦_999921 小时前
4. 注意力机制介绍
自然语言处理
lzp07911 天前
从机器翻译到智驾:规则派的黄昏与数据革命的终局 (伍)
人工智能·自然语言处理·机器翻译
lzp07911 天前
从机器翻译到智驾:规则派的黄昏与数据革命的终局 (叁)
人工智能·自然语言处理·机器翻译
huan1991101 天前
从机器翻译到智驾:规则派的黄昏与数据革命的终局 (七)
人工智能·自然语言处理·机器翻译