【NLP】NLTK工具集使用

本文介绍了Python的Natural Language Toolkit(NLTK)库,包括其提供的语料库和词典资源,如WordNet。讨论了分句、标记解析、词性标注等基本NLP任务,并分享了在安装和使用nltk过程中遇到的问题及解决办法。同时,提到了如何构建自定义词表,并展示了词频统计的步骤。内容涵盖了NLTK在文本处理中的核心功能,适合初学者入门。
摘要由CSDN通过智能技术生成

学习总结

一、Natural Language Toolkit

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

关于nltk的下载还是很多坑的,如果直接import nltknltk.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]) # 显示其中一个句子

在这里插入图片描述
其中一句的分句的结果为:

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')

3.4 根据自己的语料库构建词表

import nltk
nltk.download('punkt') # 下载分词器所需的Punkt package

# 1. 读取语料库,将其转换为标记化的单词序列
with open('corpus.txt', 'r') as f:
    corpus = f.read()
tokens = nltk.word_tokenize(corpus)

# 2. 构建词频字典
freq_dict = {}
for token in tokens:
    if token in freq_dict:
        freq_dict[token] += 1
    else:
        freq_dict[token] = 1

# 3. 选择根据某个特定的阈值过滤掉低频词,以减小词典的大小
min_freq = 5 # 过滤出现次数小于5的单词
vocab = [word for word, freq in freq_dict.items() if freq >= min_freq]

# 4. 将单词映射到整数索引
word2idx = {word: idx for idx, word in enumerate(vocab)}

# 5.利用生成的字典,可以将任何文本数据集中的单词表示为对应的整数索引
indexed_tokens = [word2idx[word] for word in tokens if word in word2idx]

Reference

(1)NLTK官网:https://www.nltk.org/
(2)https://github.com/nltk/nltk_data

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山顶夕景

小哥哥给我买个零食可好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值