Python数据处理
Python数据可视化
Python统计分析

Python单词标记化

Python单词标记化详细操作教程
单词标记是将大量文本样本分解为单词的过程。 这是自然语言处理任务中的一项要求,每个单词需要被捕获并进行进一步的分析,如对特定情感进行分类和计数等。自然语言工具包(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.']
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4