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)
相关推荐
AI人工智能+8 小时前
银行回单识别技术通过AI驱动的智能文档理解方案,实现财务处理的革命性升级
深度学习·计算机视觉·自然语言处理·ocr·银行回单识别
Tbisnic9 小时前
28.NLP 三大预训练模型 ELMo/BERT/GPT + HuggingFace Transformers
gpt·自然语言处理·大模型·nlp·bert·预训练模型
Summer-Bright10 小时前
深度 | Kimi K3 48 小时设计芯片:EDA 行业真的会被 AI 颠覆吗?
人工智能·ai·自然语言处理·agi
卖微积分的男孩1 天前
【论文精读】Tapered Language Models|锥形语言模型
人工智能·语言模型·自然语言处理
大模型任我行1 天前
腾讯:预测散度掩码提升LLM强化学习
人工智能·语言模型·自然语言处理·论文笔记
解局易否结局1 天前
鸿蒙端侧 NLP 实战:分词器 + TF-IDF + 朴素贝叶斯分类 + 文本相似度
华为·自然语言处理·分类·harmonyos·tf-idf
wish3662 天前
使用 Chainlit 调用 TextGen API + 本地 LLM 实现 RAG Chatbot 应用
人工智能·语言模型·自然语言处理·local llm
春波petal2 天前
大模型底层逻辑:优势局限与天生短板
人工智能·自然语言处理·chatgpt·大模型·多模态·语音大模型
lilihuigz3 天前
大型语言模型(LLMs)如何解读网站
人工智能·语言模型·自然语言处理
有Li3 天前
用于肝细胞癌个体化临床决策的多模态大语言模型智能体框架文献速递/医学智能体前沿
人工智能·深度学习·语言模型·自然语言处理·文献·医学生