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

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

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', '.']


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

相关文章
|
机器学习/深度学习 缓存 Shell
VSCode上的Git使用手记(持续更新ing...)
本笔记是我想要学习如何将本地文件发布到GitHub上时开始看廖雪峰的Git教程,然后打开了VSCode,发现VSCode上面集成的Git辅助使用功能真的很好用…… 基本上到了不用看教程都可以猜懂的地步。 为了整理、规范使用技巧,在经过了一番学习和试验之后,觉得以这样一篇使用手记的形式发布使用技巧相关的博文,以记录和沉淀经验,并帮助更多Git和VSCode初学者少踩坑。 本文参考的教程、文档等内容见本文末尾。
VSCode上的Git使用手记(持续更新ing...)
|
11月前
|
编译器 C语言 C++
【C++系列P1】带上这篇基础小宝典,进发C++!(持续更新ing~)
【C++系列P1】带上这篇基础小宝典,进发C++!(持续更新ing~)
|
机器学习/深度学习 缓存 数据可视化
wandb使用教程(持续更新ing...)
wandb使用教程(持续更新ing...)
wandb使用教程(持续更新ing...)
|
缓存 Linux PyTorch
Anaconda教程(持续更新ing...)
Anaconda是一个Python和R语言的工具,专注于数据科学领域。由于我只使用Python,因此本文只会介绍Python相关的内容。 本文介绍使用Anaconda相关使用教程。
Anaconda教程(持续更新ing...)
|
机器学习/深度学习 并行计算 PyTorch
PyTorch笔记详解大全(持续更新ing...)
PyTorch笔记详解大全(持续更新ing...)
|
Python Windows
pip详解(持续更新ing...)
pip详解(持续更新ing...)
|
TensorFlow 网络安全 开发工具
VSCode编程小技巧集锦(持续更新ing...)
VSCode编程小技巧集锦(持续更新ing...)
VSCode编程小技巧集锦(持续更新ing...)
|
API 索引 Python
Python3常用其他API速查手册(持续更新ing...)
Python3常用其他API速查手册(持续更新ing...)
Python3常用其他API速查手册(持续更新ing...)
|
缓存 Java Shell
Linux常用命令行集锦(持续更新ing...)
本文介绍Linux中常用的命令行。
Linux常用命令行集锦(持续更新ing...)
|
自然语言处理 API Docker
spacy教程(持续更新ing...)
本文介绍spacy模型的使用方式,即spacy的API使用教程。spacy包的API基本都要靠特定模型(trained pipeline)来使用,本文主要用英文(en_core_web_sm)和中文(zh_core_web_sm)来做示例,毕竟我就只会这两种语言。 spacy模型官网:Trained Models & Pipelines · spaCy Models Documentation

相关实验场景

更多