单词标记是将大量文本样本分解为单词的过程。 这是自然语言处理任务中的一项要求,每个单词需要被捕获并进行进一步的分析,如对特定情感进行分类和计数等。自然语言工具包(NLTK)是用于实现这一目的的库。 在继续使用python程序进行字词标记之前,先安装NLTK。
命令:
conda install -c anaconda nltk
nltk.download('punkt')
当出现下面这种,表示安装nltk成功,
下面就可以使用word_tokenize方法将段落拆分为单个单词,代码:
#拆分单词
import nltk
word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms"
nltk_tokens = nltk.word_tokenize(word_data)
print (nltk_tokens)
结果:
['It', 'originated', 'from', 'the', 'idea', 'that', 'there', 'are', 'readers',
'who', 'prefer', 'learning'