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)
相关推荐
慧都小妮子7 小时前
C# 实现AI合同审查:从读取、风险标注到批量签发
ai·自然语言处理·c#·.net·办公自动化·ai合同审查·文档ai代理
阿标在干嘛11 小时前
3种主流NLP模型在招标信息匹配中的效果实测
人工智能·自然语言处理
徐且行12 小时前
DeepSeek Harness初体验:入门、安装与自定义插件
深度学习·自然语言处理·llm·agents·deepseek·harness
Q463913491 天前
线下销售复盘难落地,AI 会话设备能帮上啥忙
人工智能·自然语言处理
墨心@1 天前
阶段 1:一次模型调用
自然语言处理·nlp·agent·codex
JAI科研1 天前
Deepseek Agent Harness教程(二) | DeepSeek Harness 设计思路
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·vllm
zhenaibo5212 天前
摘要、研究背景、文献综述AI风险高,怎么优化?
人工智能·深度学习·自然语言处理
男孩李3 天前
浅谈JiuwenSwarm安装
人工智能·语言模型·自然语言处理
寥落半伤感4 天前
codex接入deepseek+VLM视觉语言模型教程
人工智能·语言模型·自然语言处理·codex·deepseek
测开小菜鸟4 天前
从AI概念到LLM评估:全面解析大型语言模型的能力与评价
人工智能·语言模型·自然语言处理