Python数据科学 专题
您的位置:python > Python数据科学专题 > Python单词标记化
Python单词标记化
作者:--    发布时间:2019-11-20

单词标记是将大量文本样本分解为单词的过程。 这是自然语言处理任务中的一项要求,每个单词需要被捕获并进行进一步的分析,如对特定情感进行分类和计数等。自然语言工具包(nltk)是用于实现这一目的的库。 在继续使用python程序进行字词标记之前,先安装nltk。

conda install -c anaconda 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', 'new', 'skills', 'from', 'the',
'comforts', 'of', 'their', 'drawing', 'rooms']

标记句子

还可以在段落中标记句子,就像标记单词一样。使用send_tokenize方法来实现这一点。 下面是一个例子。

import nltk
sentence_data = "sun rises in the east. sun sets in the west."
nltk_tokens = nltk.sent_tokenize(sentence_data)
print (nltk_tokens)

当我们执行上面的代码时,它会产生以下结果。

['sun rises in the east.', 'sun sets in the west.']

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册