【NLP】NLTK工具集使用

本文涉及的产品
NLP自然语言处理_高级版,每接口累计50万次
NLP自然语言处理_基础版,每接口每天50万次
NLP 自学习平台,3个模型定制额度 1个月
简介: 关于nltk的下载还是很多坑的,如果直接import nltk和nltk.download()下载失败,可参考:(1)nltk安装失败:由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。(2)直接下载github的nltk:https://github.com/nltk/nltk_data。我一开始就是一直报错For more information see:

一、Natural Language Toolkit

NLTK提供了多种语料库(Corpora)和词典(Lexicon)资源,如WordNet等,以及常用工具集,如分句、标记解析(Tokenization)、词干提取(Stemming)、词性标注(POS Taggin)和句法分析(Syntactic Parsing)等,用于英文文本数据处理。

关于nltk的下载还是很多坑的,如果直接import nltk和nltk.download()下载失败,可参考:

(1)nltk安装失败:由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

(2)直接下载github的nltk:https://github.com/nltk/nltk_data。我一开始就是一直报错For more information see: https://www.nltk.org/data.html. Attempted to load tokenizers/punkt/english.pickle,然而nltk_data确实已经解压了还放在正确的路径中了还不行,尝试了几个办法后报错OSError: No such file or directory: 'D:\\anaconda1\\envs\\tensorflow\\lib\\nltk_data\\tokenizers\\punkt\\PY3\\english.pickle'发现木有PY3文件,加了个PY3文件夹后还是不行,最后直接去github上重新下载一个nltk的punkt包直接解压就行了。。。

(3)如果还是不行,就绝对路径吧sent_detector = nltk.data.load('D:\local\Anaconda3\Lib\site-packages//nltk-data//tokenizers/punkt/english.pickle'),狗头滑稽。

注意:

nltk包放在的位置,可以通过如下代码查看:

import nltk
nltk.data.path

二、常用语料库和词典

常用语料库(文本数据集),如图书、电影评论和聊天记录等,分为未标注语料库和人工标注语料库。

NLP任务中可以将一些停用词(如冠词a、the,介词of、to等)删除,提升计算速度,它们含义也不太重要。英文的常用停用词:

from nltk.corpus import stopwords
print(stopwords.words('english'))
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 
'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 
'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 
'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now']

三、常用NLP工具集

3.1 分句

分句:将较长的文档切分为若干句子。

一个句子结尾一般有明显标志(如句号、问好、感叹号等)。

也有特殊情况,在英文中,句号不仅作为句尾标志,还可以作为单词的一部分,如Mr.

# 分句
from nltk.corpus import gutenberg
from nltk.tokenize import sent_tokenize 
text = gutenberg.raw("austen-emma.txt")
sentences = sent_tokenize(text) # 对Emma小说全文分句
print(sentences[100]) # 显示其中一个句子

image.png

其中一句的分句的结果为:

Mr. Knightley loves to find fault with me, you know--
in a joke--it is all a joke.

也可以自己写的句子试试,然后进行分句:

from nltk.tokenize import sent_tokenize
mytext = "Hello Adam, how are you? I hope everything is going well. Today is a good day, see you dude."
print(sent_tokenize(mytext))

分句的结果为:

['Hello Adam, how are you?', 'I hope everything is going well.', 'Today is a good day, see you dude.']

3.2 标记解析

NLP最基本的输入单元:标记Token,它可以是一个词或标点符号。

任务如,将句子结尾标点符号和前面的单词进行拆分。

可以使用nltk.tokenize.word_tokenize

这里接着上面的一个句子sentences[100]进行标记解析:

# 标记解析
from nltk.tokenize import word_tokenize 
print(word_tokenize(sentences[100]))

得到的该句子的每个token标记:

['Mr.', 'Knightley', 'loves', 'to', 'find', 'fault', 'with', 'me', ',', 'you', 'know', '--', 'in', 'a', 'joke', '--', 'it', 'is', 'all', 'a', 'joke', '.']

3.3 词性标注

根据词语上下文,确定具体词性。

They sat by the fireThey fire a gun的fire意思不同,前者是名词,后者是动词。

# 词性标记
from nltk import pos_tag 
# 对句子标记解析后再进行词性标注
In [3]:pos_tag(word_tokenize("They sat by the fire."))
Out[3]: 
[('They', 'PRP'),
 ('sat', 'VBP'),
 ('by', 'IN'),
 ('the', 'DT'),
 ('fire', 'NN'),
 ('.', '.')]
In [4]:pos_tag(word_tokenize("They fire a gun."))
Out[4]: [('They', 'PRP'), 
('fire', 'VBP'), 
('a', 'DT'), 
('gun', 'NN'), 
('.', '.')]

从上面词性标注的结果看出,前者句子的fire被标注为名词(NN),后者被标注为动词(VBP),如果不知道词性单词的含义,可以help查询:

nltk.help.upenn_tagset('NN')
相关文章
|
自然语言处理 网络安全 Python
【Python】已解决:nltk.download(‘punkt’) [nltk_data] Error loading punkt: [WinError 10060] [nltk_data]
【Python】已解决:nltk.download(‘punkt’) [nltk_data] Error loading punkt: [WinError 10060] [nltk_data]
3495 1
|
SQL Oracle 关系型数据库
FastAPI数据库系列(一) MySQL数据库操作 一、简介
FastAPI中你可以使用任何关系型数据库,可以通过SQLAlchemy将其轻松的适应于任何的数据库,比如: PostgreSQL MySQL SQLite Oracle Microsoft SQL Server ...
|
前端开发
分享一个滑动注册和登录页面,效果非常赞,值得收藏
分享一个滑动注册和登录页面,效果非常赞,值得收藏
457 0
|
Ubuntu 开发工具
Ubuntu更换阿里云软件源
Ubuntu更换阿里云软件源
141271 0
|
11月前
|
自然语言处理 算法 搜索推荐
NLTK模块使用详解
NLTK(Natural Language Toolkit)是基于Python的自然语言处理工具集,提供了丰富的功能和语料库。本文详细介绍了NLTK的安装、基本功能、语料库加载、词频统计、停用词去除、分词分句、词干提取、词形还原、词性标注以及WordNet的使用方法。通过示例代码,帮助读者快速掌握NLTK的核心功能。
1950 1
|
Docker 容器 数据格式
Docker 修改镜像源地址
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34173549/article/details/80417198 我的Docker 版本为 1.
42263 0
Debian 官方源换为国内的源的操作方法
apt-get update 报错,采用更换源的方式解决问题。
55939 0
|
监控 物联网 区块链
新技术趋势与应用:新兴技术的发展前景与实践探索
【5月更文挑战第25天】在21世纪的科技浪潮中,新兴技术如区块链、物联网、虚拟现实等正以前所未有的速度发展,引领着全球的创新潮流。这些技术不仅改变了我们的生活方式,也正在重塑着社会的运行模式。本文将深入探讨这些新兴技术的发展趋势和应用场景,以及它们如何影响我们的生活和社会。
|
域名解析 自然语言处理 网络协议
【Python】已解决:nltk.download(‘averaged_perceptron_tagger’) [nltk_data] Error loading averaged_perceptro
【Python】已解决:nltk.download(‘averaged_perceptron_tagger’) [nltk_data] Error loading averaged_perceptro
2154 1
|
文字识别 自然语言处理 数据可视化
Qwen2.5 全链路模型体验、下载、推理、微调、部署实战!
在 Qwen2 发布后的过去三个月里,许多开发者基于 Qwen2 语言模型构建了新的模型,并提供了宝贵的反馈。在这段时间里,通义千问团队专注于创建更智能、更博学的语言模型。今天,Qwen 家族的最新成员:Qwen2.5系列正式开源
Qwen2.5 全链路模型体验、下载、推理、微调、部署实战!