NLTK使用教程(持续更新ing...)

诸神缄默不语-个人CSDN博文目录

NLTK是自然语言处理中常用的Python包,本文是NLTK包的一个简单的使用教程。
NLTK API文档:NLTK :: nltk package

1. tokenize:分词,分句

在我下载的punkt文件夹里没有chinese.pickle文件(我在网上看到一些GitHub issue和google group里面有,我很迷惑,反正我没有),所以我认为应该不能实现中文操作。
语言可以通过函数的language入参调整,但是反正默认是英文,不能用中文的,那别的我也不会……所以我没有对此作尝试。
tokenize文档:NLTK :: nltk.tokenize package
punkt文档:NLTK :: nltk.tokenize.punkt module
punkt源码:NLTK :: nltk.tokenize.punkt

英文分词(需要安装Punkt sentence tokenization模型):

from nltk.tokenize import word_tokenize

sentence="We trained a large, deep convolutional neural network to classify the 1.2 million high-resolution images in the ImageNet LSVRC-2010 contest into the 1000 different classes."
tokenized_result=word_tokenize(sentence)
print(tokenized_result)

输出结果:
['We', 'trained', 'a', 'large', ',', 'deep', 'convolutional', 'neural', 'network', 'to', 'classify', 'the', '1.2', 'million', 'high-resolution', 'images', 'in', 'the', 'ImageNet', 'LSVRC-2010', 'contest', 'into', 'the', '1000', 'different', 'classes', '.']


英文简单分词(仅使用规则,即空格和标点符号实现分词):

from nltk.tokenize import wordpunct_tokenize

sentence="We trained a large, deep convolutional neural network to classify the 1.2 million high-resolution images in the ImageNet LSVRC-2010 contest into the 1000 different classes."
tokenized_result=wordpunct_tokenize(sentence)
print(tokenized_result)

输出结果:
['We', 'trained', 'a', 'large', ',', 'deep', 'convolutional', 'neural', 'network', 'to', 'classify', 'the', '1', '.', '2', 'million', 'high', '-', 'resolution', 'images', 'in', 'the', 'ImageNet', 'LSVRC', '-', '2010', 'contest', 'into', 'the', '1000', 'different', 'classes', '.']

英文分句,英文分句+分词:

>>> from nltk.tokenize import sent_tokenize, word_tokenize
>>>> s = '''Good muffins cost $3.88\nin New York.  Please buy me
... two of them.\n\nThanks.'''
>>> sent_tokenize(s)
['Good muffins cost $3.88\nin New York.', 'Please buy me\ntwo of them.', 'Thanks.']
>>> [word_tokenize(t) for t in sent_tokenize(s)] 
[['Good', 'muffins', 'cost', '$', '3.88', 'in', 'New', 'York', '.'],
['Please', 'buy', 'me', 'two', 'of', 'them', '.'], ['Thanks', '.']]

2. stem

stem文档:NLTK :: nltk.stem package

2.1 nltk.stem.wordnet

nltk.stem.wordnet模块官网:NLTK :: nltk.stem.wordnet module

英文,使用WordNet Lemmatizer实现lemmatize:
使用WordNet内置的morphy函数来实现lemmatize,该函数官网:morphy(7WN) | WordNet

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值