英文文本分词处理(NLTK)

英文文本分词处理(NLTK)1 NLTK 的安装首先 打开终端 AnacondaProm 安装 nltk pipinstallnl 打开 Python 终端或是 Anaconda 的 Spyder 并输入以下内容来安装 NLTK 包 importnltknl download 注意 详细操作或其他安装方式请查看 Anaconda3 安装 jieba 库和 NLTK 库 2 NLTK 分词和分句 由于英语的句子基

1、NLTK的安装

首先,打开终端(Anaconda Prompt)安装nltk:

pip install nltk 

打开Python终端或是Anaconda 的Spyder并输入以下内容来安装 NLTK 包

import nltk nltk.download() 

注意: 详细操作或其他安装方式请查看 Anaconda3安装jieba库和NLTK库。

2、NLTK分词和分句

from nltk import word_tokenize #以空格形式实现分词 paragraph = "The first time I heard that song was in Hawaii on radio. I was just a kid, and loved it very much! What a fantastic song!" words = word_tokenize(paragraph) print(words) 

运行结果:

['The', 'first', 'time', 'I', 'heard', 'that', 'song', 'was', 'in', 'Hawaii', 'on', 'radio', '.', 'I', 'was', 'just', 'a', 'kid', ',', 'and', 'loved', 'it', 'very', 'much', '!', 'What', 'a', 'fantastic', 'song', '!'] 

(2)分句:

from nltk import sent_tokenize #以符号形式实现分句 sentences = "The first time I heard that song was in Hawaii on radio. I was just a kid, and loved it very much! What a fantastic song!" sentence = sent_tokenize(sentences ) print(sentence) 

运行结果:

['The first time I heard that song was in Hawaii on radio.', 'I was just a kid, and loved it very much!', 'What a fantastic song!'] 

注意: NLTK分词或者分句以后,都会自动形成列表的形式

3、NLTK分词后去除标点符号

from nltk import word_tokenize paragraph = "The first time I heard that song was in Hawaii on radio. I was just a kid, and loved it very much! What a fantastic song!".lower() cutwords1 = word_tokenize(paragraph) #分词 print('【NLTK分词结果:】') print(cutwords1) interpunctuations = [',', '.', ':', ';', '?', '(', ')', '[', ']', '&', '!', '*', '@', '#', '$', '%'] #定义标点符号列表 cutwords2 = [word for word in cutwords1 if word not in interpunctuations] #去除标点符号 print('\n【NLTK分词后去除符号结果:】') print(cutwords2) 

运行结果:

【NLTK分词结果:】 ['the', 'first', 'time', 'i', 'heard', 'that', 'song', 'was', 'in', 'hawaii', 'on', 'radio', '.', 'i', 'was', 'just', 'a', 'kid', ',', 'and', 'loved', 'it', 'very', 'much', '!', 'what', 'a', 'fantastic', 'song', '!'] 【NLTK分词后去除符号结果:】 ['the', 'first', 'time', 'i', 'heard', 'that', 'song', 'was', 'in', 'hawaii', 'on', 'radio', 'i', 'was', 'just', 'a', 'kid', 'and', 'loved', 'it', 'very', 'much', 'what', 'a', 'fantastic', 'song'] 

4、NLTK分词后去除停用词

from nltk import word_tokenize from nltk.corpus import stopwords paragraph = "The first time I heard that song was in Hawaii on radio. I was just a kid, and loved it very much! What a fantastic song!".lower() cutwords1 = word_tokenize(paragraph) #分词 print('【NLTK分词结果:】') print(cutwords1) interpunctuations = [',', '.', ':', ';', '?', '(', ')', '[', ']', '&', '!', '*', '@', '#', '$', '%'] #定义符号列表 cutwords2 = [word for word in cutwords1 if word not in interpunctuations] #去除标点符号 print('\n【NLTK分词后去除符号结果:】') print(cutwords2) stops = set(stopwords.words("english")) cutwords3 = [word for word in cutwords2 if word not in stops] print('\n【NLTK分词后去除停用词结果:】') print(cutwords3) 

运行结果:

【NLTK分词结果:】 ['the', 'first', 'time', 'i', 'heard', 'that', 'song', 'was', 'in', 'hawaii', 'on', 'radio', '.', 'i', 'was', 'just', 'a', 'kid', ',', 'and', 'loved', 'it', 'very', 'much', '!', 'what', 'a', 'fantastic', 'song', '!'] 【NLTK分词后去除符号结果:】 ['the', 'first', 'time', 'i', 'heard', 'that', 'song', 'was', 'in', 'hawaii', 'on', 'radio', 'i', 'was', 'just', 'a', 'kid', 'and', 'loved', 'it', 'very', 'much', 'what', 'a', 'fantastic', 'song'] 【NLTK分词后去除停用词结果:】 ['first', 'time', 'heard', 'song', 'hawaii', 'radio', 'kid', 'loved', 'much', 'fantastic', 'song'] 

5、NLTK分词后进行词性标注

from nltk import word_tokenize,pos_tag from nltk.corpus import stopwords paragraph = "The first time I heard that song was in Hawaii on radio. I was just a kid, and loved it very much! What a fantastic song!".lower() cutwords1 = word_tokenize(paragraph) #分词 print('【NLTK分词结果:】') print(cutwords1) interpunctuations = [',', '.', ':', ';', '?', '(', ')', '[', ']', '&', '!', '*', '@', '#', '$', '%'] #定义符号列表 cutwords2 = [word for word in cutwords1 if word not in interpunctuations] #去除标点符号 print('\n【NLTK分词后去除符号结果:】') print(cutwords2) stops = set(stopwords.words("english")) cutwords3 = [word for word in cutwords2 if word not in stops] print('\n【NLTK分词后去除停用词结果:】') print(cutwords3) print('\n【NLTK分词去除停用词后进行词性标注:】') print(pos_tag(cutwords3)) 

运行结果:

【NLTK分词结果:】 ['the', 'first', 'time', 'i', 'heard', 'that', 'song', 'was', 'in', 'hawaii', 'on', 'radio', '.', 'i', 'was', 'just', 'a', 'kid', ',', 'and', 'loved', 'it', 'very', 'much', '!', 'what', 'a', 'fantastic', 'song', '!'] 【NLTK分词后去除符号结果:】 ['the', 'first', 'time', 'i', 'heard', 'that', 'song', 'was', 'in', 'hawaii', 'on', 'radio', 'i', 'was', 'just', 'a', 'kid', 'and', 'loved', 'it', 'very', 'much', 'what', 'a', 'fantastic', 'song'] 【NLTK分词后去除停用词结果:】 ['first', 'time', 'heard', 'song', 'hawaii', 'radio', 'kid', 'loved', 'much', 'fantastic', 'song'] 【NLTK分词去除停用词后进行词性标注:】 [('first', 'JJ'), ('time', 'NN'), ('heard', 'NN'), ('song', 'NN'), ('hawaii', 'NN'), ('radio', 'NN'), ('kid', 'NN'), ('loved', 'VBD'), ('much', 'JJ'), ('fantastic', 'NN'), ('song', 'NN')] 

说明: 列表中每个元组第二个元素显示为该词的词性,具体每个词性注释可运行代码”nltk.help.upenn_tagset()“或参看说明文档:NLTK词性标注说明

6、NLTK分词后进行词干提取

# 基于Porter词干提取算法 from nltk.stem.porter import PorterStemmer print(PorterStemmer().stem('leaves')) # 基于Lancaster 词干提取算法 from nltk.stem.lancaster import LancasterStemmer print(LancasterStemmer().stem('leaves')) # 基于Snowball 词干提取算法 from nltk.stem import SnowballStemmer print(SnowballStemmer('english').stem('leaves')) 运行结果: leav leav leav 

 我们最常用的算法是 Porter 提取算法。NLTK 有一个 PorterStemmer 类,使用的就是 Porter 提取算法:

from nltk import word_tokenize,pos_tag from nltk.corpus import stopwords from nltk.stem import PorterStemmer paragraph = "The first time I heard that song was in Hawaii on radio. I was just a kid, and loved it very much! What a fantastic song!".lower() cutwords1 = word_tokenize(paragraph) #分词 print('【NLTK分词结果:】') print(cutwords1) interpunctuations = [',', '.', ':', ';', '?', '(', ')', '[', ']', '&', '!', '*', '@', '#', '$', '%'] #定义符号列表 cutwords2 = [word for word in cutwords1 if word not in interpunctuations] #去除标点符号 print('\n【NLTK分词后去除符号结果:】') print(cutwords2) stops = set(stopwords.words("english")) cutwords3 = [word for word in cutwords2 if word not in stops] #判断分词在不在停用词列表内 print('\n【NLTK分词后去除停用词结果:】') print(cutwords3) print('\n【NLTK分词去除停用词后进行词性标注:】') print(pos_tag(cutwords3)) #词性标注 print('\n【NLTK分词进行词干提取:】') cutwords4 = [] for cutword in cutwords3: cutwords4.append(PorterStemmer().stem(cutword)) #词干提取 print(cutwords4) 

运行结果:

【NLTK分词结果:】 ['the', 'first', 'time', 'i', 'heard', 'that', 'song', 'was', 'in', 'hawaii', 'on', 'radio', '.', 'i', 'was', 'just', 'a', 'kid', ',', 'and', 'loved', 'it', 'very', 'much', '!', 'what', 'a', 'fantastic', 'song', '!'] 【NLTK分词后去除符号结果:】 ['the', 'first', 'time', 'i', 'heard', 'that', 'song', 'was', 'in', 'hawaii', 'on', 'radio', 'i', 'was', 'just', 'a', 'kid', 'and', 'loved', 'it', 'very', 'much', 'what', 'a', 'fantastic', 'song'] 【NLTK分词后去除停用词结果:】 ['first', 'time', 'heard', 'song', 'hawaii', 'radio', 'kid', 'loved', 'much', 'fantastic', 'song'] 【NLTK分词去除停用词后进行词性标注:】 [('first', 'JJ'), ('time', 'NN'), ('heard', 'NN'), ('song', 'NN'), ('hawaii', 'NN'), ('radio', 'NN'), ('kid', 'NN'), ('loved', 'VBD'), ('much', 'JJ'), ('fantastic', 'NN'), ('song', 'NN')] 【NLTK分词进行词干提取:】 ['first', 'time', 'heard', 'song', 'hawaii', 'radio', 'kid', 'love', 'much', 'fantast', 'song'] 

7、NLTK分词后进行词性还原

 词形还原与词干提取类似, 但不同之处在于词干提取经常可能创造出不存在的词汇,词形还原的结果是一个真正的词汇。

from nltk.stem import WordNetLemmatizer lemmatizer = WordNetLemmatizer() print(lemmatizer.lemmatize('playing')) 运行结果: playing 

 NLTK词形还原时默认还原的结果是名词,如果你想得到动词,可以通过以下的方式指定:

from nltk.stem import WordNetLemmatizer #词性还原 lemmatizer = WordNetLemmatizer() print(lemmatizer.lemmatize('playing', pos="v")) #指定还原词性为动词 运行结果: play 

注意: 面对不同的词语,我们需要其不同的词性才能更好的提起单词的原型,所以建议在词性还原时指定还原的词性。

from nltk import word_tokenize,pos_tag #分词、词性标注 from nltk.corpus import stopwords #停用词 from nltk.stem import PorterStemmer #词干提取 from nltk.stem import WordNetLemmatizer #词性还原 paragraph = "I went to the gymnasium yesterday , when I had finished my homework !".lower() cutwords1 = word_tokenize(paragraph) #分词 print('【NLTK分词结果:】') print(cutwords1) interpunctuations = [',', ' ','.', ':', ';', '?', '(', ')', '[', ']', '&', '!', '*', '@', '#', '$', '%'] #定义符号列表 cutwords2 = [word for word in cutwords1 if word not in interpunctuations] #去除标点符号 print('\n【NLTK分词后去除符号结果:】') print(cutwords2) stops = set(stopwords.words("english")) cutwords3 = [word for word in cutwords2 if word not in stops] #判断分词在不在停用词列表内 print('\n【NLTK分词后去除停用词结果:】') print(cutwords3) print('\n【NLTK分词去除停用词后进行词性标注:】') print(pos_tag(cutwords3)) #词性标注 print('\n【NLTK分词进行词干提取:】') cutwords4 = [] for cutword1 in cutwords3: cutwords4.append(PorterStemmer().stem(cutword1)) #词干提取 print(cutwords4) print('\n【NLTK分词进行词形还原:】') cutwords5 = [] for cutword2 in cutwords4: cutwords5.append(WordNetLemmatizer().lemmatize(cutword2,pos='v')) #指定还原词性为名词 print(cutwords5) 

运行结果:

【NLTK分词结果:】 ['i', 'went', 'to', 'the', 'gymnasium', 'yesterday', ',', 'when', 'i', 'had', 'finished', 'my', 'homework', '!'] 【NLTK分词后去除符号结果:】 ['i', 'went', 'to', 'the', 'gymnasium', 'yesterday', 'when', 'i', 'had', 'finished', 'my', 'homework'] 【NLTK分词后去除停用词结果:】 ['went', 'gymnasium', 'yesterday', 'finished', 'homework'] 【NLTK分词去除停用词后进行词性标注:】 [('went', 'VBD'), ('gymnasium', 'NN'), ('yesterday', 'NN'), ('finished', 'VBD'), ('homework', 'NN')] 【NLTK分词进行词干提取:】 ['went', 'gymnasium', 'yesterday', 'finish', 'homework'] 【NLTK分词进行词形还原:】 ['go', 'gymnasium', 'yesterday', 'finish', 'homework'] 

 到这里,英文文本处理就基本结束了,后续进行英文关键词的提取和分析过程,谢谢你的阅读!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/205387.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午6:08
下一篇 2026年3月19日 下午6:09


相关推荐

  • Mybaits-plus生成工具类,很详细

    Mybaits-plus生成工具类,很详细不熟悉配置文件就多生成几次自然就会了注意配置里面的输出路径,默认包名!<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependen

    2022年6月26日
    28
  • OpenClaw 安装与使用完全指南:打造你的个人 AI 助手

    OpenClaw 安装与使用完全指南:打造你的个人 AI 助手

    2026年3月13日
    3
  • Postman汉化补丁

    Postman汉化补丁版本 9 0 5 更新时间 2021 10 10 系统 macOSBigSur1 2 3 Intel 汉化包地址 链接 https pan baidu com s 16x zpkyFxY4rYrd 提取码 6p87 操作方式 下载链接 https pan baidu com s 16x zpkyFxY4rYrd 提取码 6p87 解压 app zip 进入访达 应用程序 Postman app Contents Resourc

    2026年3月16日
    1
  • @CacheEvict清除指定下所有缓存

    @CacheEvict清除指定下所有缓存CacheEvict cacheNames parts grid allEntries true 此注解会清除 part grid 下所有缓存 CacheEvict 要求指定一个或多个缓存 使之都受影响 此外 还提供了一个额外的参数 allEntries 表示是否需要清除缓存中的所有元素 默认为 false 表示不需要 当指定了 allEntries 为 true 时 SpringCac

    2026年3月17日
    3
  • 详解数据存储芯片AT24C02的应用及编程

    详解数据存储芯片AT24C02的应用及编程一 芯片简介 AT24C02 是一个 2K 位串行 CMOSE2PROM 内部含有 256 个 8 位字节 采用先进 CMOS 技术实质上减少了器件的功耗 AT24C02 有一个 8 字节页写缓冲器 该器件通过 IIC 总线接口进行操作 有一个专门的写保护功能 二 芯片参数 1 特点工作电压 1 8V 5 5V 低功耗 CMOS 技术 工作电流 1mA 待机电流 1uA 应用在内部结构 128×8 1K 256×8 2K

    2026年3月20日
    2
  • 正弦波放大电路与移相电路设计

    正弦波放大电路与移相电路设计原文地址 http www docin com p 613242880 html 相关文章 1 正弦波放大电路 http zhidao baidu com link url lE4Ox8xWoSal 7C9ZIbVzmGJ4 j2efLU2 Z

    2025年9月6日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号